Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance and Polymorphism

Similar presentations


Presentation on theme: "Inheritance and Polymorphism"— Presentation transcript:

1 Inheritance and Polymorphism

2 Inheritance One of the fundamental object-oriented design techniques.
Advantages: Code sharing. To minimize duplication. To maintain update consistency.

3 Example Person Name SSN Sex Date of birth Marital status
if married or widowed a. Date of marriage b. Number of children if divorced a. Date of divorce b. First divorce if single a. Independent residency

4 Use only one class Person Name SSN Sex Date of birth Marital status
Date of marriage Number of children Date of divorce First divorce Independent residency Some operations Waste memory space

5 Use 3 independent classes
Married Name SSN Sex Date of birth Marital status Date of marriage Number of children Some operations Divorced Name SSN Sex Date of birth Marital status Date of divorce First divorce Some operations Single Name SSN Sex Date of birth Marital status Independent residency Some operations Can’t share methods Any change must be done 3 times

6 Use inheritance Person Name SSN Sex Date of birth Marital status
Some operations Married Date of marriage Number of children Divorced Date of divorce First divorce Single Independent residency

7 Deriving Classes class classname extends parent-class {
[variable declaration;] [method declaration;] } Deriving Classes Superclass (Base/parent class) subclass extends superclass A subclass is A superclass Subclass (Derived class) Inheritance is used to model the Is-A relationship. Inheritance - inherits fields and methods of its superclass. Advantage of inheritance: Code reuse

8 Overriding Methods A child class can override the definition of an inherited method in favor of its own That is, a child can redefine a method that it inherits from its parent The new method must have the same signature as the parent's method, but can have different code in the body The type of the object executing the method determines which version of the method is invoked

9 Method Overriding class A { // Base class int method1() { …}
float method2(int x) { …} } class B extends A { // subclass float method2(int z) { …} void method3() { …}

10 A method overriding error
class A { // Base class int method1() { …} float method2(int x) { …} } class B extends A { // subclass char method2(int z) { …} void method3() { …} If the argument types match, the return type must match also

11 Overloading vs. Overriding
Overloading deals with multiple methods in the same class with the same name but different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different data Overriding lets you define a similar operation in different ways for different object types

12 The super and this references
super: reserved word used in a derived class to refer to its parent class allows us to access those members of the parent class that are not inherited example: invoking the parent’s constructor class A { // Base class f(int x) { …} } class B extends A { // subclass f(int z) { …} super.f(); this.f(); // same as f();

13 Polymorphism Literally polymorphism is: multiple forms.
In programming, polymorphism means: one interface, multiple implementations of a method (overriding). Which implementation of the method actual called depends on the type of the object passed to the method. Advantage: Can write generic code that can be applied to different kinds of objects. In Java, polymorphism is implemented using a technique called dynamic (or late) method binding: which exact method to call is determined at run time.

14 Basis of Polymorphism Inheritance Method overriding
Polymorphic assignment Polymorphic methods In Java, all methods are polymorphic. That is, choice of method depends on the object.

15 Polymorphic assignment
SuperClassVariable = SubclassObject;

16 Example: Polymorphism
Furniture void prnt(); abstract class Furniture { public int numlegs; } abstract class Chair extends Furniture { public String fabric; abstract void prnt(); class Recliner extends Chair { void prnt() { System.out.println(“I’m a recliner”); class LaZBoy extends Recliner { System.out.println(“I’m a lazboy”); Chair void prnt(); Recliner void prnt(); LaZBoy void prnt(); What is the output? Chair cha; cha = new LaZBoy(); cha.prnt(); Furniture furn; furn = new Recliner(); furn.prnt(); Furniture furn; furn = new LaZBoy(); furn.prnt();

17 Example: Polymorphism
Furniture void prnt(); void display(Furniture furn) { furn.prnt(); } Chair void prnt(); What is the output? display(new LaZBoy()); Recliner void prnt(); display(new Recliner()); LaZBoy void prnt();

18 Example: Polymorphism
class Queue { Object e; public String toString() { String Str = “”; for every element e in the queue Str += e.toString() + “ “; return Str; } Object String toString(); Token Operator String toString(); Operand String toString();

19 Members use compile-time binding
class Base{ int X=99; public void prnt(){ System.out.println("Base"); } class Rtype extends Base{ int X=-1; System.out.println("Rtype"); What is the output? Base b=new Rtype(); System.out.println(b.X); b.prnt();

20 Homework 1 Show the output of the following program.
abstract class Furniture { abstract void prnt();} class Recliner extends Furniture { void prnt() { System.out.println("I'm a recliner");} } class LaZBoy extends Recliner { void prnt() { System.out.println("I'm a lazboy");} public class furnitureTest2 { public static void main(String[] args) { Furniture [] A = { new Recliner(), new Recliner(), new LaZBoy()}; for (int i=0; i<3; ++i) A[i].prnt(); Homework 1

21 Homework 2 Show the output of the following program.
class A { int x = 1; } class B extends A { } class C extends B { int x = 2;} public class classTest { public static void main(String[] args) { A w = new A(); System.out.println(w.x); B u = new B(); System.out.println(u.x); C v = new C(); System.out.println(v.x); A [] a = { new A(), new B(), new C()}; for (int i=0; i<3; ++i) System.out.println(a[i].x); } Homework 2


Download ppt "Inheritance and Polymorphism"

Similar presentations


Ads by Google