Unit 9 Inheritance
Protected variables: access modifier so attribute isn't affected by outside modifier
Subclass: Extends from a superclass
Overriding: You can override a method by using the @Override above the method you wish to override
Polymorphism: When a method has many forms (overriding)
Method Overloading: Two methods with same name but different arguments
//super class Dog
public class Dog {
//protected variables
protected String coatTexture;
protected String color;
protected String size;
public Dog(String coatTexutre, String color, String size){
this.coatTexture = coatTexture;
this.color = color;
this.size = size;
}
//bark method
public void bark(){
System.out.println("Bark!");
}
}
//sub class siberianHusky
public class SiberianHusky extends Dog {
protected String sheds;
public SiberianHusky (String coatTexture, String color, String size, String sheds){
super(coatTexture, color, size);
this.sheds = sheds;
}
//overriding the bark method
@Override
public void bark(){
System.out.println("Woof!");
}
public static void main(String[] args){
Dog husky = new SiberianHusky("Double Coat", "Black and White", "Large", "Heavy Shedder");
husky.bark();
}
}
SiberianHusky.main(null);
//sub class pug
public class Pug extends Dog {
public Pug (String coatTexture, String color, String size){
super(coatTexture, color, size);
}
//Runtime polymorphism (overriding again)
@Override
public void bark(){
System.out.println("Grrrr!");
}
public void xxx() {
System.out.println("xxx zero args");
}
//method overloading
public void toeBeans(int a) {
System.out.println("This pug has " + a + " toe beans");
}
//method overloading
public void toeBeans(int a, int b){
System.out.println("This pug has " + a + " toe beans on its " + b + " feet" );
}
public static void main(String[] args){
Pug pug = new Pug("Short Coat", "Any Color!", "Small");
pug.bark();
pug.toeBeans(4);
pug.toeBeans(4, 4);
}
}
Pug.main(null);