Chapter-4-Inheritance and Polymorphism
Chapter-4-Inheritance and Polymorphism
(OOP)
2019
Inheritance
– What is Inheritance
– Advantage of inheritance
– Types of Inheritance
Aggregation
Supper and Final Keyword
Polymorphism
Overloading and Overriding Methods
Abstract Class
2
What is Inheritance? ASTU
3
Example of Inheritance ASTU
4
Contd. ASTU
5
Contd. ASTU
7
Contd. ASTU
10
Problem: Code Duplication ASTU
11
Solution: Inheritance ASTU
12
weeklyEmp PartTimeEMP
String name String name ASTU
int id,nwk; int id,wkhr;
folat weeklyrate; float sal,Rate;
Float salary; String getName()
String getName() int getid()
int getid() using
inheritance
Employee
String name
superclass Int id;
Float salary;
String getName() subclass
int getid()
subclass
weeklyEmp partTimeEmp
float weeklyrate; int wkhr;
int nwk; float rate;
13
ASTU
Employee Superclass
}
14
ASTU
partimeEmp Subclass
}
15
ASTU
WeeklyEmp Subclass
} 16
ASTU
Inheritance Quiz-1
Class test{
Public static void main(String args[])
{
weeklyEmp WEmp1=new weeklyemp(“abebe”,12,120,5);
wEmp1.setSal();
partimeEmp PEmp= new partimeEmp(“Bekele”,13,30,120);
PEmp.setsal();
}
}
17
ASTU
Inheritance Rules
18
Subclass Constructor ASTU
19
Inheritance Types ASTU
20
Inheritance Types ASTU
21
ASTU
Single Inheritance Example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Out put:
barking…
eating...
22
ASTU
Multilevel Inheritance Example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Out put:
weeping…
barking…
eating…
23
ASTU
Hierarchical Inheritance Example
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Out put:
meowing…
eating…
24
ASTU
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple
inheritance is not supported in java.
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
25
Inheritance Quiz-2 ASTU
public class A {
public A() { System.out.println("I'm A"); }
}
27
Example of polymorphism ASTU
Student
Person
Employee
Rectangle
Shape Triangle
Circle
28
Runtime Polymorphism ASTU
29
Up casting ASTU
class A{}
class B extends A{}
A a=new B();//upcasting
30
Example of Runtime Polymorphism ASTU
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){System.out.println("running safely with 60km");
}
public static void main(String args[]){
Bike b = new Splender();//upcasting
b.run();
}
}
Output: running safely with 60km
31
Runtime Polymorphism with data member ASTU
class Animal{
void eat(){System.out.println("eating");}
}
class Dog extends Animal{
void eat(){System.out.println("eating meat");}
}
class BabyDog extends Dog{
void eat(){System.out.println("drinking milk");}
public static void main(String args[]){
Animal a1,a2,a3;
a1=new Animal();
a2=new Dog();
a3=new BabyDog();
a1.eat();
a2.eat();
a3.eat();
} }
Output: eating
eating meat
drinking milk
33
An example that illustrates dynamic method dispatch ASTU
34
An example that illustrates dynamic method dispatch: ASTU
Discussion:
• Also, a reference of type A, called r, is declared. The program then
assigns a reference to each type of object to r and uses that reference to
invoke callme().
• The version of callme() executed is determined by the type of object
being referred to at the time of the call. Had it been determined by the
type of the reference variable, r, you would see three calls to A’s
callme() method.
35
Quiz ASTU
36
Method overloading ASTU
37
Different ways to overload the method ASTU
38
By changing number of argument ASTU
class Addition
{
void sum(int a, int b)
{
System.out.println(a+b);
}
void sum(int a, int b, int c)
{
System.out.println(a+b+c);
}
public static void main(String args[])
{
Addition obj=new Addition();
obj.sum(10, 20);
obj.sum(10, 20, 30);
}
}
39
By changing data type ASTU
class Addition
{
void sum(int a, int b)
{
System.out.println(a+b);
}
void sum(float a, float b)
{
System.out.println(a+b);
}
public static void main(String args[])
{
Addition obj=new Addition();
obj.sum(10, 20);
obj.sum(10.05, 15.20);
}
}
40
Method Overriding ASTU
42
Example ASTU
43
without method overriding ASTU
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike extends Vehicle{
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
}
Output: Vehicle is running
44
Example ASTU
class Vehicle{
void run(){
System.out.println("Vehicle is running");
}
}
class Bike2 extends Vehicle{
void run(){
System.out.println("Bike is running safely");
}
public static void main(String args[]){
Bike2 obj = new Bike2();
obj.run();
}
Output: Bike is running safely
45
method overriding ASTU
46
Real example of Java Method Overriding ASTU
47
Real example of Java Method Overriding ASTU
class Bank{
int getRateOfInterest(){return 0;}
}
class Nibe extends Bank{
int getRateOfInterest(){return 8;}
}
class Oromia extends Bank{
int getRateOfInterest(){return 7;}
}
class Dashen extends Bank{
int getRateOfInterest(){return 9;}
}
class Test2{
public static void main(String args[]){
Nibe s=new Nibe();
Oromia i=new Oromia();
Dashen a=new Dashen();
System.out.println(" Nibe Rate of Interest: "+s.getRateOfInterest());
System.out.println(" Oromia Rate of Interest: "+i.getRateOfInterest());
System.out.println(" Dashen Rate of Interest: "+a.getRateOfInterest());
}
48
ASTU
Java useful keywords
Super keyword
Final keyword
Static keyword
This keyword
49
ASTU
Super key word
• The super keyword in java is a reference
variable that is used to refer immediate
parent class object.
50
ASTU
use of Super key word
51
used to refer immediate parent class instance variable. ASTU
class Vehicle{
int speed=50;
}
class Bike3 extends Vehicle{
int speed=100;
void display(){
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[]){
Bike3 b=new Bike3();
b.display();
}
}
Output: 100
52
used to refer immediate parent class instance variable. ASTU
class Vehicle{
int speed=50;
}
class Bike3 extends Vehicle{
int speed=100;
void display(){
System.out.println(Super.speed);//will print speed of Vehicle
}
public static void main(String args[]){
Bike3 b=new Bike3();
b.display();
}
}
Output: 50
53
ASTU
used to invoke parent class constructor
class Vehicle{
Vehicle(){System.out.println("Vehicle is created");}
}
class Bike5 extends Vehicle{
Bike5(){
super();//will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike5 b=new Bike5();
}
}
Output:
Vehicle is created
Bike is created
54
ASTU
used to invoke parent class method
class Person{
void message(){System.out.println("welcome");}
}
class Student16 extends Person{
void message(){System.out.println("welcome to java");}
void display(){
message();//will invoke current class message() method
super.message();//will invoke parent class message() method
}
public static void main(String args[]){
Student16 s=new Student16();
s.display();
}
}
Output:
welcome to java
welcome 55
Final keyword ASTU
56
Final at variable level ASTU
class Bike{
final void run(){System.out.println("running");}
}
60
Example without using this keyword ASTU
class Employee
{ int id;
String name;
Employee(int id,String name)
{
id = id; name = name;
}
void show()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Employee e1 = new Employee(111,"Harry");
Employee e2 = new Employee(112,"Jacy");
e1.show();
e2.show();
}
}
Output:
0
0
null
null
61
Example with this keyword ASTU
class Employee
{ int id;
String name;
Employee(int id,String name)
{
this. id = id; this.name = name;
}
void show()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Employee e1 = new Employee(111,“Abel");
Employee e2 = new Employee(112,“Surafel ");
e1.show();
e2.show();
}
}
Output:
62
111 Abel
112 Surafel
Aggregation in Java- HAS-A relationship. ASTU
class Employee{
int id;
String name;
Address address;//Address is a class
...
}
63
Example of aggregation ASTU
String city,state,country;
int id;
String name;
Address address;
e.display();
} e2.display(); } }
64
Abstract class in java ASTU
class Helloworld
{
void display()
{
System.out.println("Good Morning........");
}
}
Create an object
Helloworld obj=new Helloworld();
obj.display();
66
Abstract class ASTU
67
Contd. ASTU
68
Contd. ASTU
69
Contd. ASTU
70
Example ASTU
Thank You
?
72