Design Patterns Video Tutorial 2

Design Patterns Video Tutorial 2Here I continue my design patterns video tutorial. If you missed part 1, definitely watch it first here Design Patterns Video Tutorial.

In this part of the tutorial I cover polymorphism, abstract classes, abstract methods, interfaces, using static methods and variables and a whole bunch more.

All of the code follows the video to help you learn.

If you like videos like this, telling Google helps

Sharing is nice

Code from the Video

Animal.java

public class Animal {
	
	private String name;
	private double height;
	private int weight;
	private String favFood;
	private double speed;
	private String sound;
	
	public void setName(String newName){ name = newName; }
	public String getName(){ return name; }
	
	public void setHeight(double newHeight){ height = newHeight; }
	public double getHeight(){ return height; }
	
	public void setWeight(int newWeight){ 
		if (newWeight > 0){
			weight = newWeight; 
		} else {
			System.out.println("Weight must be bigger than 0");
		}
	}
	public double getWeight(){ return weight; }
	
	public void setFavFood(String newFavFood){ favFood = newFavFood; }
	public String getFavFood(){ return favFood; }
	
	public void setSpeed(double newSpeed){ speed = newSpeed; }
	public double getSpeed(){ return speed; }
	
	public void setSound(String newSound){ sound = newSound; }
	public String getSound(){ return sound; }
	
	// A private method can only be accessed by other public methods
	// that are in the same class
	
	private void bePrivate(){
		System.out.println("I'm a private method");
	}
	
	public static void main(String[] args){
		
		Animal dog = new Animal();
		
		dog.setName("Grover");
		
		System.out.println(dog.getName());
		
	}
	
}

Dog.java

public class Dog extends Animal{
	
	public void digHole(){
		
		System.out.println("Dug a hole");
		
	}
	
	public void changeVar(int randNum){
		
		randNum = 12;
		
		System.out.println("randNum in method value: " + randNum);
		
	}
	
	
	/* This private method can only be accessed through using other 
	 * methods in the class */
	
	private void bePrivate(){
		System.out.println("In a private method");
	} 
	
	public void accessPrivate(){
		bePrivate();
	}
	
	// The constructor initializes all objects
	
	public Dog(){
		
		// Executes the parents constructor
		// Every class has a constructor whether you make it or not
		
		super();
		
		// Sets bark for all Dog objects by default
		
		setSound("Bark");
		
	}
	
}

Cat.java

public class Cat extends Animal{
	
	// The constructor initializes all objects
	
	public Cat(){
		
		// Executes the parents constructor
		// Every class has a constructor whether you make it or not
		
		super();
		
		// Sets bark for all Dog objects by default
		
		setSound("Meow");
		
	}
	
	// If you want to make sure a method isn't overridden mark it as Final
	
	final void attack(){
		// Do stuff that can never change
	}
	
	// A field marked with final can't be changed
	
	public static final double FAVNUMBER = 3.14;
	
	// A class labeled as final can't be extended
	
}

WorkWithAnimals.java

public class WorkWithAnimals{
	
	int justANum = 10;
	
	public static void main(String[] args){
		
		Dog fido = new Dog();
		
		fido.setName("Fido");
		System.out.println(fido.getName());
		
		fido.digHole();
		
		fido.setWeight(-1);
		
		// Everything is pass by value
		// The original is not effected by changes in methods
		
		int randNum = 10;
		fido.changeVar(randNum);
		
		System.out.println("randNum after method call: " + randNum);
		
		// Objects are passed by reference to the original object
		// Changes in methods do effect the object
		
		changeObjectName(fido);
		
		System.out.println("Dog name after method call: " + fido.getName());
		
		System.out.println("Animal Sound: " + fido.getSound());
		
		// Create a Dog and Cat object with the super class
		// but the Dog and Cat reference type
		
		Animal doggy = new Dog();
		Animal kitty = new Cat();
		
		System.out.println("Doggy says: " + doggy.getSound());
		System.out.println("Kitty says: " + kitty.getSound() + "\n");
		
		// Now you can make arrays of Animals and everything just works
		
		Animal[] animals = new Animal[4];
		animals[0] = doggy;
		animals[1] = kitty;
		
		System.out.println("Doggy says: " +animals[0].getSound());
		System.out.println("Kitty says: " +animals[1].getSound() + "\n");
		
		// Sends Animal objects for processing in a method
		
		speakAnimal(doggy);
		
		// Polymorphism allows you to write methods that don't need to 
		// change if new subclasses are created.
		
		// You can't reference methods, or fields that aren't in Animal
		// if you do, you'll have to cast to the required object
		
		((Dog) doggy).digHole();
		
		// You can't use non-static variables or methods in a static function
		
		// System.out.println(justANum);
		
		// sayHello();
		
		// You can't call a private method even if you define it in
		// the subclass
		
		// fido.bePrivate();
		
		// You can execute a private method by using another public
		// method in the class
		
		fido.accessPrivate();
		
		// Creating a Giraffe from an abstract class
		
		Giraffe giraffe = new Giraffe();
		
		giraffe.setName("Frank");
		
		System.out.println(giraffe.getName());
		
	}
	
	// Any methods that are in a class and not tied to an object must
	// be labeled static. Every object created by this class will
	// share just one static method
	
	public static void changeObjectName(Dog fido){
		
		fido.setName("Marcus");
		
	}
	
	// Receives Animal objects and makes them speak
	
	public static void speakAnimal(Animal randAnimal){
		
		System.out.println("Animal says: " + randAnimal.getSound());
		
	}
	
	// This is a non-static method used to demonstrate that you can't
	// call a non-static method inside a static method
	
	public void sayHello(){
		
		System.out.println("Hello");
		
	}
	
}

Creature.java

//If you don't want the user to create objects from
//a class mark it as abstract.
//Subclasses can still extend it

abstract public class Creature{
	
	// protected fields are like private fields except 
	// subclasses can inherit them 
	
	protected String name;
	protected double height;
	protected int weight;
	protected String favFood;
	protected double speed;
	protected String sound;
	
	// There are no abstract fields in Java, but
	// there are abstract methods. Every method
	// marked abstract must be overridden
	// Not all methods must be abstract and you
	// can also use static methods
	
	public abstract void setName(String newName);
	public abstract String getName();
	
	public abstract void setHeight(double newheight);
	public abstract double getHeight();
	
	public abstract void setWeight(double newWeight);
	public abstract double getWeight();
	
	public abstract void setFavFood(String newFood);
	public abstract String getFavFood();
	
	public abstract void setSpeed(double newSpeed);
	public abstract double getSpeed();
	
	public abstract void setSound(String newSound);
	public abstract String getSound();
	
}

Giraffe.java

public class Giraffe extends Creature{

	private String name;
	
	@Override
	public void setName(String newName) {
		name = newName;
		
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public void setWeight(double newWeight) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public double getWeight() {
		// TODO Auto-generated method stub
		return 0;
	}
}

Living.java

public class Giraffe extends Creature{

	private String name;
	
	@Override
	public void setName(String newName) {
		name = newName;
		
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public void setWeight(double newWeight) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public double getWeight() {
		// TODO Auto-generated method stub
		return 0;
	}
}

14 Responses to “Design Patterns Video Tutorial 2”

  1. Dennis de Vink says:

    Hi Derek,

    In de Living.java is de giraffe-code. Is it possible for you to repair that?

    Thanx in advance.

    Yours,
    Dennis de Vink

  2. Stefan T says:

    Thanks for the awesome tutorial. Quick note: the last class “Living.java”, which is supposed to be the Living interface, has got some other code (from Girafe). This is what the guys had noted.

    I have a question, is it not possible to “implement” an abstract class?
    The way that I have seen is: (say I have your Creature class) and I have my own custom class Snake, I can then do something like

    Creature boaConstrictor = new Snake(); //then I also get to //inherit any method, which is not abstract and simply take the //functionality

    because my Snake already extends Animal, but I still want to use some functionality (supposedly) available in the Creature class.

    Sorry for the long question.

    Best Regards,

    • Derek Banas says:

      You’re very welcome 🙂 It definitely would make sense to turn Creature into an interface instead of an abstract class. That would actually be a better option. You normally use abstract when you must force some methods to not be abstract. Since that isn’t the case here it makes more sense for it to be an interface. i hope that helps

  3. Oliver says:

    I just watched the first two of your Design-Pattern tutorials and I wanna give u a BIG thumbs up! I just have one request: Why don’t you put a dl link for the code package below? Would make it much faster to actually work with the given code.

    kind regards

    • Derek Banas says:

      Thank you very much 🙂 Sorry about that. I now provide packages of code for download. I’m learning new things all of the time. Thank you for the input.

      • Tawny Pincus says:

        I am hoping to take advantage of the opportunity to download the “packages of code” you had refereed to. Is there a link or URL? Pls. could you provide it?

        • Derek Banas says:

          For all of my newer tutorials I provide the complete packages if I think it would help. I didn’t do that with the design patterns because I thought it would cause more confusion because there are so many files.

  4. michael says:

    Could you post another example of an abstract class that you might find in practice? I’m not sure why one would create an abstract class with those fields only to make the getters/setters abstract, that seems to be the perfect place to actually implement the methods and make other methods abstract, right? So while I understand what it is, I’m still not sure how to apply the abstract class concept. Thanks.

    • Derek Banas says:

      You use an abstract class any time when you need a class in which every method can’t be abstract. It doesn’t happen all the time, but you’ll know it when you need it.

Leave a Reply

Your email address will not be published.

Google+