Object InheritanceObject Inheritance
Lecturer: Lecturer: Kalamullah RamliKalamullah Ramli
Electrical Engineering Dept.Electrical Engineering Dept.University of IndonesiaUniversity of Indonesia
Session-4Session-4
Slide - Slide - 22OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Extending classes & inheriting Extending classes & inheriting fields/methodsfields/methods Using this and super Constructor chaining Single and multiple inheritance
Class Extension and Inheritance
Slide - Slide - 33OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Inheritance: Natural, Hierarchical Way of Organizing Things
Staff Member
Employee Volunteer
Hourly Salaried
Consultant
Think in terms of “is a” relationships:An Employee is a Staff Member, An Hourly worker is a Employee.A Consultant is a(n) Hourly employee.
(subclass of Hourly)
(subclass of Employee)
(subclass of Staff)
(superclass)
Slide - Slide - 44OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
class Animal { protected String strName = “”; protected String strNoise = “”; protected int iNumTimesPerformed = 0; // constructors, accessors & modifiers go here public void identifySelf( ) { System.out.println(“My name is “ + strName); } // of identifySelf public void perform ( ) { doYourThing( ); iNumTimesPerformed++; } // of perform public void doYourThing( ) { ; // ‘no-op’ method } // of doYourThing
} // of Animal
Example
Inheritance: SampleInheritance: Sample [1/4][1/4]
Slide - Slide - 55OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Subclasses(Dog extends Animal
i.e. “A dog is an animal” or“All dogs are animals”)
class Dog extends Animal { public Dog() { strNoise = “Woof”; } // of constructor
public void doYourThing ( ) { identifySelf(); System.out.println(“I am a dog”); System.out.println(strNoise); } // of doYourThing
} // of Dog
Recall: The Animal class had a no-op method for doYourThing()
Animal
Dog Cat Human
Inheritance: SampleInheritance: Sample [2/4][2/4]
Slide - Slide - 66OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Subclasses(Cat extends Animal
i.e. “A cat is an animal” or“All cats are animals”)
class Cat extends Animal { public Cat() { strNoise = “Miaow”; } // of constructor
public void doYourThing ( ) { identifySelf(); System.out.println(“I am a cat”); System.out.println(strNoise); } // of doYourThing
} // of Cat
Animal
Dog Cat Human
Inheritance: SampleInheritance: Sample [3/4][3/4]
Slide - Slide - 77OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Inheritance: SampleInheritance: Sample [4/4][4/4]
Dog pickles = new Dog();pickles.setName(“Pickles”);pickles.doYourThing();// output:// “My name is Pickles”// “I am a dog”// “Woof”
Cat abby = new Cat();abby.setName(“Abby”);abby.doYourThing();// output:// “My name is Abby”// “I am a cat”// “Miaow”
Slide - Slide - 88OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Inheritance and Hierarchy
Variables (e.g. strNoise):• Java first examines current method, looks for local variable or parameter;• Java then examines current class (e.g. Dog);• Java then examines superclass (e.g. Animal); • Java continues up the class hierarchy until no more superclasses to examine.
Methods (e.g. doYourThing() or identifySelf()):• Java first examines current class;• Java then examines superclass;• Java continues up inheritance hierarchy until no more superclasses to examine.
Slide - Slide - 99OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Specifying ScopeSpecifying Scope [1/2][1/2]
Java allows you to override the scope rules by saying which variable/method you’re referring to:
Keyword super:keyword for specifying method or variable from superclass, e.g., super.doYourThing( )
Keyword this:keyword for specifying method or variable in current object, e.g., this.doYourThing( )
Slide - Slide - 1010OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
class Dog extends Animal { public Dog() { super.strNoise = “Woof”; } // of constructor
public void doYourThing ( ) { super.identifySelf(); System.out.println(“I am a dog”); System.out.println(strNoise); } // of doYourThing
} // of Dog
Same (in this case) asstrNoise = “Woof”;andthis.strNoise = “Woof”;
Same (in this case) asidentifySelf();orthis.identifySelf();
Specifying ScopeSpecifying Scope [2/2] [2/2]
Slide - Slide - 1111OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Using Super
class Dog extends Animal { // constructor as before
public void doYourThing() {identifySelf();System.out.println(strNoise);
} // of doYourThing
public void identifySelf() { super.identifySelf();
System.out.println(“I am a dog”); } // of identifySelf
} // of Dog
Animal
Dog Cat
I.e.this.identifySelf()(newly defined below)
I.e. theidentifySelf()(defined in Animal)
Slide - Slide - 1212OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
class Shape {
public final double PI = 3.14159; protected String name; public String getName () { return (this.name); } // getName
public int area () { return (0); } // area
} // Shape
A Geometry ExampleA Geometry Example [1/2][1/2]
Shape
CircleRectangle
Slide - Slide - 1313OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
class Rectangle extends Shape { private int length, width; Rectangle () { this(0, 0); } // constructor
Rectangle (int l, int w) { this( l, w, “rectangle”); } // constructor
Rectangle (int l, int w, String n) { length = l; width = l; name = n; } // constructor
public int area () { return (length * width); } // area
public String getName () { if (length == width) return ("square"); else return (super.getName()); } // getName
public String toString () { String s; s = new String ("A " + getName() +
" with length " + length + " and width " + width);
return (s); } } // toString
} // Rectangle
A Geometry ExampleA Geometry Example [2/2][2/2]
Slide - Slide - 1414OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Java’s rule:
• If first line of constructor is not an explicit call to a superclass constructor, Java will implicitly put super( ) as the first line, calling the superclass default constructor.
public Dog() { strNoise = “Woof”; } // of constructor
• An exception to this rule: chained constructor call tothis(params)
will defer super( ) call
• To use superclass constructors with params, call them explicitly, e.g., super(strName)
Constructors and Inheritance
implied call to Animal() here
Slide - Slide - 1515OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Inheritance and Scoping Inheritance and Scoping [1/3][1/3]
Examples:super(xxx) // calls a superclass constructorsuper.xxx // accesses superclass’ variablesuper.xxx( ) // calls superclass’ method
this(xxx) // calls a current-class constructorthis.xxx // accesses current class’s variablethis.xxx( ) // calls current class’ method
Note: cannot do super.super<something>
(can achieve this effect via casting, but rarely should; details later...)
Slide - Slide - 1616OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Inheritance and Scoping [2/3][2/3]
class StaffMember { String strName;
public StaffMember( ) { System.out.println (“in default StaffMem constr; No Name”); setName(“No Name”); } // of constructor
public StaffMember(String strName) { System.out.println (“in 2nd StaffMem constructior; have a Name”); setName(strName); } // of constructor
public void setName(String strName) { this.strName = strName; } // of setName
} // of StaffMember
Slide - Slide - 1717OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
class Employee1 extends StaffMember { public Employee1(String strName) { setName(strName); } // of constructor } // of Employee1
What happens???
Inheritance and Scoping [3/3][3/3]
Note: Employee has no local setName() method
class Employee2 extends StaffMember { public Employee2(String strName) { setName(strName); } // of constructor
public void setName(String strName) { super.setName(strName); System.out.println (“Name set”); }} // of Employee2
Slide - Slide - 1818OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Class Object
• Java provides a base class, Object
• All classes that do not have an extends clause implicitly inherit directly fromclass java.lang.Object
Examples:
public boolean equals (Object o)
public boolean String toString ()
• When you create your own toString( ) method for a class, you are overriding the toString( ) provided by Object.
Slide - Slide - 1919OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
Object HierarchyObject Hierarchy
Animal
Dog Cat
Object
Employee
Salaried Hourly
class Object methods: String toString() boolean equals(Object obj) and a few others...
Animal
Dog Cat Human?
Object
Employee
Salaried Hourly
Or how about...
Slide - Slide - 2020OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.EngM.Eng
The EndThe End
QUESTIONS & QUESTIONS & COMMENTS ?COMMENTS ?