ሐምሌ, 2013 ዓ.
Object Oriented Programming Chapter Four
Wollo University, Kombolcha Institute of Technology.
College of Informatics.
Department of Information System.
By Daniel G.
ሐምሌ,2013 ዓ.ም
Concept of Polymorphism
01
Contents Intro
Type of polymorphism
Object Oriented Programming
chapter 4 will include this topics. 02 Relationships among
objects in an inheritance
Multiple inheritance 03 Assigning reference of
scope
and interfaces subclass to superclass-type
06 variable
Assigning a superclass reference
Subclass method calls via 04
superclass-type variable 05 to subclass-type variable
7/20/2021 2
Basics questions In this chapter ሐምሌ,2013 ዓ.ም
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 ?
Polymorphism is a feature of object-oriented programming languages that
allows a specific routine to use variables of different types at different times
Polymorphism is the ability of a programming language to present the
same interface for several different underlying data types
Polymorphism is the ability of different objects to respond in a unique way
to the same message
7/20/2021
Conti… ሐምሌ,2013 ዓ.ም
Polymorphism is one of the OOPs feature that allows us to perform a single
action in different ways.
For example, lets say we have a class Animal that has a method sound().
Since this is a generic class so we can’t give it a implementation like: Roar,
Meow, Oink etc. We had to give a generic message.
7/20/2021 By Daniel G. 5
ሐምሌ,2013 ዓ.ም
Conti…
public class Animal{
public void sound(){
[Link]("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(){
[Link]("Neigh");
}
public static void main(String args[]){
Animal obj = new Horse();
[Link]();
}
}
7/20/2021
ሐምሌ,2013 ዓ.ም
Conti…
class Cat extends Animal{
@Override
public void sound(){
[Link]("Meow");
}
public static void main(String args[]){
Animal obj = new Cat();
[Link]();
}
}
7/20/2021
Types of Polymorphism ሐምሌ,2013 ዓ.ም
There are two basic concepts of Polymorphism
1. Static Polymorphism
2. Dynamic Polymorphism
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 ዓ.ም
public int add(double x, int y){
//method 3
return (int)x+y;
}
public int add(int x, double y){
//method 4
return x+(int)y;
}
7/20/2021 By Daniel G. 12
Conti… ሐምሌ,2013 ዓ.ም
class Test{
public static void main(String[] args){
DemoOverload demo=new DemoOverload();
[Link]([Link](2,3));//method 1 called
[Link]([Link](2,3,4));//method 2 called
[Link]([Link](2,3.4))//method 4 called
[Link]([Link](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(){
[Link](“Vehicles can move!!”);
}
}
class MotorBike extends Vehicle{
public void move(){
[Link](“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();
[Link]();// prints MotorBike can move and accelerate too!!
vh=new Vehicle();
[Link](); // 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 ዓ.ም
public class test{
person p = new person ();
person wu = new Wucommunity ();
person s = new Student ();
person ks = new KiotStudent ();
Student ks1 = new KiotStudent ();
}
7/20/2021 By Daniel G. 19
Assigning reference of subclass to superclass-type variable ሐምሌ,2013 ዓ.ም
Student S = new Student ();
KiotStudent k = new KiotStudent ();
K=(Student) S
We can access a super class methods via ‘k’
7/20/2021 By Daniel G. 20
Assigning a super class reference to subclass-type variable ሐምሌ,2013 ዓ.ም
Student S = new Student ();
Student k = new KiotStudent ();
S=(KiotStudent ) k;
We can access a seb class methods via ‘S’
7/20/2021 By Daniel G. 21
Subclass method calls via superclass-type variable ሐምሌ,2013 ዓ.ም
Student k = new KiotStudent ();
We can access a sub class methods via ‘k’
7/20/2021 By Daniel G. 22
Abstract class and methods ሐምሌ,2013 ዓ.ም
Data abstraction is the process of hiding certain details and showing only
essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces
Abstract class: is a restricted class that cannot be used to create objects (to
access it, it must be inherited from another class).
It represents any entity in the real world which is generic in nature
Example : Vehicle, fruit, shapes
7/20/2021 By Daniel G. 23
Conti….. ሐምሌ,2013 ዓ.ም
Syntax < abstract class <class name > >
Abstract class contains abstract methods
Abstract class can not be instantiated and can’t create an object
If a class contain an abstract method then the class must be defined as
abstract
Abstract class may or may not contain abstract method
7/20/2021 By Daniel G. 24
Conti….. ሐምሌ,2013 ዓ.ም
Abstract class can be sub classed by other concrete class to be implemented
A concrete class which implements an interface and abstract class must
implement all abstract methods of the interface and abstract class
A concrete class is a class can’t be defined as abstract and it is important for
implementing interface and abstract class.
7/20/2021 By Daniel G. 25
Conti….. ሐምሌ,2013 ዓ.ም
Abstract method: can only be used in an abstract class, and it does not
have a body or implementation. The body is provided by the subclass
(inherited from).
It ends with semicolon (;)
Syntax
abstract <data type> method name(parameter)
7/20/2021 By Daniel G. 26
Conti… ሐምሌ,2013 ዓ.ም
Example 1:
abstract class Animal {
public abstract void animalSound();
public void sleep() {
[Link]("Zzz");
}
Animal myObj = new Animal(); // will generate an error
7/20/2021 By Daniel G. 27
Conti… ሐምሌ,2013 ዓ.ም
Example 2: abstract class Animal {
public abstract void animalSound();
public void sleep() {
[Link]("Zzz");
}
}
class Pig extends Animal {
public void animalSound() {
[Link]("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
[Link]();
[Link]();
}
}
7/20/2021 By Daniel G. 29
Multiple inheritance and interfaces ሐምሌ,2013 ዓ.ም
An interface contains variables and methods like a class but the methods
in an interface are abstract by default unlike a class.
Multiple inheritance by interface occurs if a class implements multiple
interfaces or also if an interface itself extends multiple interfaces.
A program that demonstrates multiple inheritance by interface in Java is
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() {
[Link]("Animal is eating");
}
public void travel() {
[Link]("Animal is travelling");
}
}
7/20/2021 By Daniel G. 31
Conti… ሐምሌ,2013 ዓ.ም
public class Demo {
public static void main(String args[]) {
Animal a = new Animal();
[Link]();
[Link]();
}
}
We can’t create an object from an interface
7/20/2021 By Daniel G. 32
Demonstration about concrete ,abstract and interface ሐምሌ,2013 ዓ.ም
Concrete class Abstract Class Interface
Contains Contains Contains
Fields Fields Constant Fields
Constructors Un implemented methods Only abstract methods
Implemented methods Constructor Not Instantiable
Instantiable Implemented methods Can’t represent real world
Real world entities Not Instantiable entities
Represents generic entities
7/20/2021 By Daniel G. 33
ሐምሌ, 2013 ዓ.ም
THANK YOU
Any questions?
Feel free !