Object Oriented Programming: Chapter Four
Object Oriented Programming: Chapter Four
By Daniel G.
ሐምሌ,2013 ዓ.ም
Concept of Polymorphism
01
Contents Intro
Type of polymorphism
1. What is Polymorphism ?
2. What are the type of polymorphism?
3. What is abstract class and methods?
4. What is interface in java ?
5. Multiple inheritance in OOP with interface ?
7/20/2021
ሐምሌ,2013 ዓ.ም
What is Polymorphism ?
7/20/2021
Conti… ሐምሌ,2013 ዓ.ም
For example, lets say we have a class Animal that has a method sound().
7/20/2021 By Daniel G. 5
ሐምሌ,2013 ዓ.ም
Conti…
public class Animal{
public void sound(){
System.out.println("Animal is making a sound");
}
}
Now lets say we two subclasses of Animal class: Horse and Cat that extends
Animal class. We can provide the implementation to the same method like…..
7/20/2021
ሐምሌ,2013 ዓ.ም
Conti…
class Horse extends Animal{
@Override
public void sound(){
System.out.println("Neigh");
}
public static void main(String args[]){
Animal obj = new Horse();
obj.sound();
}
}
7/20/2021
ሐምሌ,2013 ዓ.ም
Conti…
class Cat extends Animal{
@Override
public void sound(){
System.out.println("Meow");
}
public static void main(String args[]){
Animal obj = new Cat();
obj.sound();
}
}
7/20/2021
Types of Polymorphism ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 9
Conti… ሐምሌ,2013 ዓ.ም
Static Polymorphism
In Java, static polymorphism is achieved through method overloading.
Method overloading means there are several methods present in a class
having the same name but different types/order/number of parameters.
At compile time, Java knows which method to invoke by checking the
method signatures. So, this is called compile time
polymorphism or static binding.
7/20/2021 By Daniel G. 10
Conti… ሐምሌ,2013 ዓ.ም
Example:
class MOverload{
public int add(int x, int y){
//method 1
return x+y;
}
public int add(int x, int y, int z){
//method 2
return x+y+z;
}
7/20/2021
} By Daniel G. 11
Conti… ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 12
Conti… ሐምሌ,2013 ዓ.ም
class Test{
public static void main(String[] args){
DemoOverload demo=new DemoOverload();
System.out.println(demo.add(2,3));//method 1 called
System.out.println(demo.add(2,3,4));//method 2 called
System.out.println(demo.add(2,3.4))//method 4 called
System.out.println(demo.add(2.5,3));//method 3 called }
7/20/2021 By Daniel G. 13
Conti… ሐምሌ,2013 ዓ.ም
Dynamic Polymorphism
Suppose a subclass overrides a particular method of the superclass.
Let’s say we create an object of the subclass and assign it to the
superclass reference. Now, if we call the overridden method on the
superclass reference then the subclass version of the method will be
called.
7/20/2021 By Daniel G. 14
Conti… ሐምሌ,2013 ዓ.ም
Example:
class Vehicle{
public void move(){
System.out.println(“Vehicles can move!!”);
}
}
class MotorBike extends Vehicle{
public void move(){
System.out.println(“MotorBike can move and accelerate too!!”);
}
}
7/20/2021 By Daniel G. 15
Conti… ሐምሌ,2013 ዓ.ም
class Test{
public static void main(String[] args){
Vehicle vh=new MotorBike();
vh.move();// prints MotorBike can move and accelerate too!!
vh=new Vehicle();
vh.move(); // prints Vehicles can move!!
}
}
7/20/2021 By Daniel G. 16
Relationships among objects in an inheritance ሐምሌ,2013 ዓ.ም
The relationship between objects defines how these objects will interact
or collaborate to perform an operation in an application.
Relationships among objects in an inheritance hierarchy In public
inheritance an object of a derived class can be treated as an object of its
base class. Each derived class object is an object of its base class
7/20/2021 By Daniel G. 17
Conti… ሐምሌ,2013 ዓ.ም
Example:
class Person {
}
class Wucommunity extends person{
}
clas Student extends Wucommunity{
}
class KiotStudent extends Student{
}
7/20/2021 By Daniel G. 18
Example ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 19
Assigning reference of subclass to superclass-type variable ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 20
Assigning a super class reference to subclass-type variable ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 21
Subclass method calls via superclass-type variable ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 22
Abstract class and methods ሐምሌ,2013 ዓ.ም
Data abstraction is the process of hiding certain details and showing only
Abstract class: is a restricted class that cannot be used to create objects (to
abstract
7/20/2021 By Daniel G. 24
Conti….. ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 25
Conti….. ሐምሌ,2013 ዓ.ም
Abstract method: can only be used in an abstract class, and it does not
(inherited from).
Syntax
7/20/2021 By Daniel G. 26
Conti… ሐምሌ,2013 ዓ.ም
Example 1:
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
7/20/2021 By Daniel G. 27
Conti… ሐምሌ,2013 ዓ.ም
Example 2: abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
7/20/2021 By Daniel G. 28
Conti… ሐምሌ,2013 ዓ.ም
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
7/20/2021 By Daniel G. 29
Multiple inheritance and interfaces ሐምሌ,2013 ዓ.ም
An interface contains variables and methods like a class but the methods
given as follows:
7/20/2021 By Daniel G. 30
Conti… ሐምሌ,2013 ዓ.ም
interface AnimalEat {
void eat();
}
interface AnimalTravel {
void travel();
}
class Animal implements AnimalEat, AnimalTravel {
public void eat() {
System.out.println("Animal is eating");
}
public void travel() {
System.out.println("Animal is travelling");
}
}
7/20/2021 By Daniel G. 31
Conti… ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 32
Demonstration about concrete ,abstract and interface ሐምሌ,2013 ዓ.ም
7/20/2021 By Daniel G. 33
ሐምሌ, 2013 ዓ.ም
THANK YOU
Any questions?
Feel free !