Pillars of Object Oriented Program
Chapter Three
OOP using Java Prepared byDagne w . 1
Chapter Three
Inheritance, Interface, Polymorphism
and Abstraction in Java
OOP using Java Prepared byDagne w . 2
Outline
Distinguish between Inheritance, interface and
polymorphism
Describe how to initialize instance variables (using super
key word)
Recognize, describe, and use interface ,interface and
polymorphism
Distinguish type of inheritance in java .
Recognize, describe abstract in java
OOP using Java Prepared byDagne w . 3
Concept of OOP
OOP using Java Prepared byDagne w . 4
Inheritance in Java
Inheritance in java: Inheritance is a mechanism which
a subclass inherits all the properties and behaviors of
super class. Inheritance represents the IS-A
relationship, also known as parent-child relationship
OOP using Java Prepared byDagne w . 5
Inheritance in Java
Inheritance is a mechanism which a subclass inherits all
the properties and behaviors of super class
OOP using Java Prepared byDagne w . 6
Inheritance in Java
Inheritance represents the IS-A relationship
OOP using Java Prepared byDagne w . 7
Why we use inheritance
For Method Overriding (so runtime polymorphism can be achieved).
For Code reusability
The extends keyword indicates that you are making a new class
that derives from an existing class. The meaning of "extends" is to increase
the functionality.
class Mobile{ ..... //methods and fields
Syntax of Inheritance:
} class Subclass-
class Laptop extends Mobile { … //methods and fields name extends Super
} class-name
class Tablet extends Mobile { ..... {
//methods and field
//methods and fields
s
}
class Smartphone extends Mobile{ ....
}
//methods and fields .
}
OOP using Java Prepared byDagne w . 8
Java Inheritance Program
class Employee{
float salary=5000f;
}
class Teacher extends Employee{
int bonus=2000;
public static void main(String args[]){
Teacher t=new Teacher();
System.out.println(" Teacher salary is:"+t.salary);
System.out.println("Bonus of Teacher is:"+t.bonus);
}
Output
}
Teacher salary is:5000.0
Bonus of Teacher is:2000
OOP using Java Prepared byDagne w . 9
Inheritance in Java
Types of inheritance in java
OOP using Java Prepared byDagne w . 10
Type of Inheritance
Single inheritance is a mechanism where one
subclass inherit all the methods and attributes of one
super class
Multilevel inheritance is a mechanism where one
subclass inherit all the methods and attributes of
more than one super classes at multiple level
Hierarchical inheritance is a mechanism where
more than one subclasses inherit all the methods and
attributes of one supper class
Hybrid inheritance is a combination of one or more
inheritances.
OOP using Java Prepared byDagne w . 11
Inheritance in Java
Diamond problem Hybrid
Java not
support
multiple
inheritance!!
OOP using Java Prepared byDagne w . 12
Single inheritance Program
One direct super class
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");
}
public static void main(String args[]){
Dog d=new Dog();
d.bark(); Output
d.eat(); eating…
barking…
}
}
OOP using Java Prepared byDagne w . 13
Multilevel Inheritance Program
One direct super class and one or more indirect supper class
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...");}
public static void main(String args[]){
BabyDog d=new BabyDog();
Output
d.weep();
weeping…
d.bark();
barking…
d.eat();
eating…
}}
OOP using Java Prepared byDagne w . 14
Hierarchical Inheritance Program
One direct super class and multiple subclasses
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(); Output
c.meow(); meowing...
c.eat(); eating…
//c.bark();//C.T.Error}}
OOP using Java Prepared byDagne w . 15
Abstract class in Java
A class that is declared with abstract keyword, is known as
abstract class in java. It can have abstract and non-abstract
methods (method with body).
Abstraction is a process of hiding the implementation
details and showing only functionality to the user.
for example sending SMS, you just type the text and send the
message. You don't know the internal processing about the
message delivery.
Ways to achieve Abstraction
Abstract class (0 to 100%)
Interface (100%)
OOP using Java Prepared byDagne w . 16
Java Abstract Program
OOP using Java Prepared byDagne w . 17
Rule for Abstract class in Java
OOP using Java Prepared byDagne w . 18
Java Abstract Program
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle extends Shape{
void draw(){System.out.println("drawing circle");}
}
public static void main(String args[]){
Shape s=new Circle();
s.draw(); Output
} draw circle…
}
OOP using Java Prepared byDagne w . 19
Interface in Java
It has static constants and abstract methods.
Does not have access modifiers
The interface in java is a mechanism to achieve
abstraction. There can be only abstract methods in the java
interface not method body.
Java Interface also represents IS-A relationship.
It is used to achieve abstraction.
By interface, we can support the functionality of multiple
inheritance.
Interface is Highly abstract class.
Interface does not have data members.
Interface includes incomplete
Signature members
OOP using Java Prepared byDagne w . 20
Java Interface Program
interface Printable{
void print();
}
interface Showable{
void print();
}
class Interface implements Printable, Showable{
public void print(){System.out.println("Hello");}
Output
public static void main(String args[]){
Hello…
Interface obj = new Interface();
obj.print();
}
}
OOP using Java Prepared byDagne w . 21
Classes and Interfaces
OOP using Java Prepared byDagne w . 22
Classes vs. Interfaces
Abstract class Interface
1) Abstract class can have abstract and Interface can have only
non-abstractmethods. abstract methods. Since Java 8, it can
have default and static methods also.
2) Abstract class doesn't support Interface supports multiple
multiple inheritance. inheritance.
3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables. variables.
4) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
5) The abstract keyword is used to The interface keyword is used to
declare abstract class. declare interface.
6) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
OOP using Java Prepared byDagne w . 23
Polymorphism in java
Polymorphism
Poly Morphism
- many - forms
OOP using Java Prepared byDagne w . 24
Polymorphism in java
Can be achieved through: :
Method Overloading
Method Overriding
Example :
Example :
Compile time polymorphism
Runtime Polymorphism
(static polymorphism)
(Dynamic polymorphism)
OOP using Java Prepared byDagne w . 25
Polymorphism in java
OOP using Java Prepared byDagne w . 26
Polymorphism in java
public class MultiplyNumbers
{ public int Multiply(int a, int b) { return a * b;
} public int Multiply(int a, int b, int c)
{ return a*b*c; } }
OOP using Java Prepared byDagne w . 27
Method Overloading in Java
If a class has multiple methods having same name but
different in parameters, it is known as Method Overloading
Advantage of method overloading
Method overloading increases the readability of the program.
Different ways to overload the method
By changing number of arguments
By changing the data type.
OOP using Java Prepared byDagne w . 28
Method overloading program
By changing no. of arguments
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
public static void main(String[] args){
System.out.println(Adder.add(121,121));
System.out.println(Adder.add(121,121,121));
}
}
OOP using Java Prepared byDagne w . 29
Method overloading program
By changing data type of arguments
class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
public static void main(String[] args){ Output
System.out.println(Adder.add(11,11)); 22
24.9
System.out.println(Adder.add(12.3,12.6));
}}
Method Overloading is not possible by changing the
return type of method only
OOP using Java Prepared byDagne w . 30
Method Overriding in Java
If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding in java.
Rules for Java Method Overriding
method must have same name as in the parent class
method must have same parameter as in the parent class.
must be IS-A relationship (inheritance).
OOP using Java Prepared byDagne w . 31
Method Overriding in Java
OOP using Java Prepared byDagne w . 32
Method Overriding in Java
OOP using Java Prepared byDagne w . 33
Method Overriding in Java
OOP using Java Prepared byDagne w . 34
Method Overriding in Java
OOP using Java Prepared byDagne w . 35
Method Overriding program in Java
class Car{
void run(){System.out.println(“Car is running");}
}
class Bicycle extends Car{
void run(){System.out.println("Bicycle is running safely"
);
public static void main(String args[]){
Bicycle obj = new Bicycle();
obj.run(); Output
} Bicycle is running safely …
OOP using Java Prepared byDagne w . 36
Method Overriding program in Java
OOP using Java Prepared byDagne w . 37
Super key word in Java
The super keyword in java is a reference variable which
is used to refer immediate parent class object.
Usage of java super Keyword
super can be used to refer immediate parent class
instance variable.
super can be used to invoke immediate parent class
method.
super() can be used to invoke immediate parent class
constructor.
OOP using Java Prepared byDagne w . 38
Super key word program in Java
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
public static void main(String args[]){
Dog d=new Dog();
Output
d.printColor();
}}
black…
white..
OOP using Java Prepared byDagne w . 39
Encapsulation
OOP using Java Prepared byDagne w . 40
Encapsulation
OOP using Java Prepared byDagne w . 41
Encapsulation
The practice of combining data and operations into
a single entity is defined as encapsulation. In
Java, encapsulation can be illustrated by making
the data member of class private and declare
getter and setter methods to make it accessible.
Encapsulation example:
OOP using Java Prepared byDagne w . 42
Encapsulation
Example of Encapsulation in Java
How to implement encapsulation in java:
1) Make the instance variables private so that they
cannot be accessed directly from outside the class.
You can only set and get values of these variables
through the methods of the class.
2) Have getter and setter methods in the class to set
and get the values of the fields.
OOP using Java Prepared byDagne w . 43
Encapsulation
Example of Encapsulation in Java
package encapsulation;
public class Student{
private String fname;
public String getName(){
return name; }
public void setName(String name){
this.name=fname } }
public static void main(String[] args){
Student s=new Student(); s.setName(“Belay Zeleke");
System.out.println(s.getName());
}
Output
}
Belay Zeleke
OOP using Java Prepared byDagne w . 44
Think a Scenario
1) Suppose if you are in class room that time you behave like a student, when you
are in market at that time you behave like a customer, when you at your home
at that time you behave like a son or daughter, Here one person present in
different-different behaviors and what you understand from this scenario?
Explain in detail
2) Suppose Man and Woman. They acquired the attributes and functions of class
human. However he added some attributes and functions specific to these
classes. For the man and the woman class, he added attributes specific to their
anatomy. Like a special Adam's apple for the man, the breasts for the woman
and a few other specific ones. He added functions specific to these classes, for
instance he gave the function "GiveBirthToNewHuman" to the woman class
and what you understand from this scenario? Explain in detail
OOP using Java Prepared byDagne w . 45
Think a Scenario
1) God first created a class called Human. Added some attributes and functions.
Attributes such as eyes, ears, brain, legs. Functions such as walk, talk, think,
sleep etc. and what you understand from this scenario? Explain in detail
2) Finally, it was time to have some fun, and create some instances of the classes
made. And viola, God created Adam, Eve, Chunnu, Munnu, Raveena, Sunny,
Mari, Salman, John, Samuel, Anoushka, Rama and many many more. and
what you understand from this scenario? Explain in detail
3) a car in it is a well-defined object, which is composed of several other smaller
objects like a gearing system, steering mechanism, engine, which are again
have their own subsystems. But for humans car is a one single object, which
can be managed by the help of its subsystems, even if their inner details are
unknown and what you understand from this scenario ? Explain in detail
OOP using Java Prepared byDagne w . 46
Think a Scenario
1) Suppose we are asked to develop communication software such as an FTP (File
Transfer Protocol) or Telnet program that uses a modem. Our program must
work with a variety of different modems. Although all the modems provide the
same functionality, their implementations are quite different and what you
understand from this scenario? Explain in detail
2) Suppose your mobile phone, one name but many forms you used as phone, as
camera as mp3 player and as radio, so what you understand from this
scenario??
OOP using Java Prepared byDagne w . 47
Summery
Inheritance
Interface
Abstract
Polymorphism
Super key word
OOP using Java Prepared byDagne w . 48
THANK YOU !!!
OOP using Java Prepared byDagne w . 49