0% found this document useful (0 votes)
2 views14 pages

Java Class Design: Inheritance & Polymorphism

Uploaded by

suranaishah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Java Class Design: Inheritance & Polymorphism

Uploaded by

suranaishah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Module 2

Class Design

Suzana/Java II-Chapt2
Objectives

• Describe encapsulation, polymorphism, and


inheritance
• Develop a subclass from a superclass through
inheritance
• Override method in the subclass

Suzana/Java II-Chapt2
Inheritance
Building

# size : String superclass


# price :double

+ Building (size: String, price:double)

House is a Building.. extends


(“is–a”)

House
subclass
- houseNo : int

+ House (houseNo:int)
+ display( )

Inheritance hierarchy
Suzana/Java II-Chapt2
‘super’ keyword

• ‘super’ keyword in Java has 2 uses :

(i) to call/invoke superclass constructor


(ii) to call/invoke superclass method

• The next slide demostrate the 1st use of super


keyword..

Suzana/Java II-Chapt2
‘super’ keyword
Building
public class Building
# size:String { protected String size;
protected double price;
# price:double
public Building(String size,
+ Building( size:String, double price)
price:double ) { [Link] = size;
[Link] = price;
}
// super is used to call superclass constructor

extends
(“is–a”)
public class House extends Building
{ private int houseNo;
House
public House(int houseNo)
- houseNo:int { super(“100 X 90”, 100000);
[Link] = houseNo;
+ House (no:int) }
+ display( )
Suzana/Java II-Chapt2
Inheritance
public class House extends Building
public class Building { private int houseNo;
{ protected String size;
protected double price;
(2) public House(int houseNo)
{ super(“100 X 90”, 100000);
public Building(String size, [Link] = houseNo;
double price) }
{ [Link] = size;
[Link] = price; public void display()
} (3)
{ [Link](
}
“House no:”+houseNo);
[Link](
“Size:”+size); (4)
[Link](
“Price:”+price); }
public class Test }
{ public static void main(String[] args)
{ (1)
House house = new House(8);
[Link]();
} (1) - (4) : Some execution flows between
} classes
Suzana/Java II-Chapt2
Questions

1) Based on the previous slide, can you guess the


next execution flow (5) and (6) before the
program exit ?

2) Try to modify Building class by adding a method


named display(). What are the next changes
required?
(Hint: use the super keyword to call the
superclass method)

Suzana/Java II-Chapt2
Method overriding
Circle
- radius : double

+ Circle()
superclass
+ Cirle(radius:double)
+ getRadius() : double
+ setRadius(radius:double)
+ findArea():double

Cylinder

- length : double

+ Cylinder()
+ Cylinder (radius:double, length:double)
subclass
+ getLength() :double
Overriding findArea() in + setLength(length:double):void
superclass
+ findArea():double
Suzana/Java II-Chapt2
Why overriding the method ??

• When a baseclass inherits a superclass,


sometimes it is necessary for the baseclass to
modify the implementation of a method defined in
the superclass.

Suzana/Java II-Chapt2
Method overriding
[Link]

public class Circle


{ protected double radius;

public Circle() Overloaded constructors


{ radius = 1.0;
}
public Circle(double radius)
{ [Link] = radius;
}
public double getRadius()
{ return radius;
}
public double setRadius(double radius)
{ [Link] = radius;
}
public double findArea()
// This method will be overrided in
{ return radius * radius * 3.14159; Cylinder class
}
}

Suzana/Java II-Chapt2
Method overriding
[Link]

public class Cylinder extends Circle Note :


{ private double length; Overriden method must
have the same signature and

public Cylinder() return type as its superclass


{ super();
length = 1.0;
}
public Cylinder(double radius,double length)
{ super(radius);
[Link] = length;
}
public double getLength()
{ return length;
}
public double setLength(double length)
{ [Link] = length;
// Modified findArea()
}
public double findArea()
{ return 2 * [Link]()+ 2 * getRadius() * [Link] * length;
}
}
Suzana/Java II-Chapt2
Formular  2 * circle area + cylinder body area
Method overriding

Eg :[Link]

public class Test


{ public static void main(String[] args)
{ Circle circle = new Circle();
[Link](); // calls findArea() in Circle

Cylinder cylinder = new Cylinder(3.0,5.0);


[Link](); // calls findArea() in Cylinder
:
:
}
}

Suzana/Java II-Chapt2
Polymorphism
• Allows a single variable to refer to objects from
different subclasses in the same inheritance
hierarchy
• For example, if Cat and Dog are subclasses of
Pet, then the following statements are valid:

Pet myPet;

myPet = new Dog(); // Dog is a pet


. . .
myPet = new Cat(); // Cat is a pet

Note : Further issues are dynamic binding, generic programming and casting objects.

Suzana/Java II-Chapt2
Exercise

From previous exercise ..

1) Modify the test class so it uses polymorphism


2) Create one generic method to display Computer
and WashingMachine objects using toString()
method
3) Cast object using instanceof operator

Suzana/Java II-Chapt2

You might also like