Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.

Similar presentations


Presentation on theme: "Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4."— Presentation transcript:

1 Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4

2 Slide - 2 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng  Extending classes & inheriting fields/methods  Using this and super  Constructor chaining  Single and multiple inheritance Class Extension and Inheritance

3 Slide - 3 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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)

4 Slide - 4 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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: Sample [1/4]

5 Slide - 5 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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: Sample [2/4]

6 Slide - 6 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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: Sample [3/4]

7 Slide - 7 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Inheritance: Sample [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”

8 Slide - 8 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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.

9 Slide - 9 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Specifying Scope [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( )

10 Slide - 10 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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) as strNoise = “Woof”; and this.strNoise = “Woof”; Same (in this case) as identifySelf(); or this.identifySelf(); Specifying Scope [2/2]

11 Slide - 11 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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. the identifySelf() (defined in Animal)

12 Slide - 12 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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 Example [1/2] Shape CircleRectangle

13 Slide - 13 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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 Example [2/2]

14 Slide - 14 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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 to this(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

15 Slide - 15 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Inheritance and Scoping [1/3] Examples: super(xxx) // calls a superclass constructor super.xxx // accesses superclass’ variable super.xxx( ) // calls superclass’ method this(xxx) // calls a current-class constructor this.xxx // accesses current class’s variable this.xxx( ) // calls current class’ method Note: cannot do super.super (can achieve this effect via casting, but rarely should; details later...)

16 Slide - 16 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng [2/3] Inheritance and Scoping [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

17 Slide - 17 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng class Employee1 extends StaffMember { public Employee1(String strName) { setName(strName); } // of constructor } // of Employee1 What happens ??? [3/3] Inheritance and Scoping [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

18 Slide - 18 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.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.

19 Slide - 19 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng Object 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...

20 Slide - 20 OOP Lecture 2004 Dr. –Ing. Ir. Kalamullah Ramli, M.Eng The End QUESTIONS & COMMENTS ?


Download ppt "Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4."

Similar presentations


Ads by Google