0% found this document useful (0 votes)
4 views11 pages

L18 New Polymorphsim - Part1

This document covers the concept of inheritance in object-oriented programming, focusing on polymorphism, early binding, and late binding. It explains how polymorphism allows objects to take on multiple forms and details the differences between compile-time (early binding) and run-time (late binding) polymorphism. Additionally, it provides examples of polymorphic behavior using superclass and subclass references in Java.

Uploaded by

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

L18 New Polymorphsim - Part1

This document covers the concept of inheritance in object-oriented programming, focusing on polymorphism, early binding, and late binding. It explains how polymorphism allows objects to take on multiple forms and details the differences between compile-time (early binding) and run-time (late binding) polymorphism. Additionally, it provides examples of polymorphic behavior using superclass and subclass references in Java.

Uploaded by

9v7jv42s2k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Inheritance

CS102 – Introduction to Computer


Science
Topics
 Learning Outcomes
 Polymorphism
 Early Binding
 Late Binding
 Object Reference Polymorphic Behavior.
Learning Outcomes

 After completion of this chapter, the student should be able to:


 Explain the concept of polymorphism
 Early Binding and late binding
 Polymorphic behavior of objects
 Use of keyword final
 Write abstract Classes
 Write abstract Methods
 Use Interfaces
Polymorphism

 Polymorphism means the ability to take on many forms, The term is applied both to objects
and to operations.
 Polymorphism is tightly coupled to inheritance and is one of the most powerful advantages to
object-oriented technologies.
 Polymorphism is a Greek word that means Many Shapes.
 It has four classes with one method print.
Which print about Shape, Circle,
Rectangle and Star
 Earlier we studies two types of
polymorphism. They are:
 Overloading
 Overriding
 Different forms of Polymorphism
 Early binding
 Late binding
Early Binding

 Early binding is called as compile time binding or static polymorphism.


 Static polymorphism means that the information required to call a method is available at
compile time itself.
 Hence method calls can be resolved at compile time. The exact method to call is determined
by the difference in either the number or type of method parameters.
 Static polymorphism is achieved through method overloading in Java.
 It is always faster and efficient than dynamic polymorphism because the run time cost of
function call resolution is avoided.
Example :
 An employee object may have two print() methods one taking no arguments and one taking
arguments.
 Given these two methods, when the print() method is called without any arguments, the
compiler, looking at the method arguments knows which method is meant to be called and it
generates the object code accordingly.
Early Binding
 In the example below polymorphism is demonstrated by the use of multiple add methods.
The computer differentiates among them by the method signatures (the list of parameters:
their number, their types, and the order of the types.)

 This form of polymorphism is called early-binding (or compile-time) polymorphism. That is,
after the compile process when the code is now in byte-code form, the computer will “know”
which of the add methods it will execute.
Late Binding

 There is another form of polymorphism called late-binding (or run-time) polymorphism


because the computer does not know at compile time which of the methods are to be
executed. It will not know that until “run time.” Run-time polymorphism is achieved through
what are called overridden methods (while compile-time polymorphism is achieved with
overloaded methods). Run-time polymorphism comes in two different forms: run-time
polymorphism with abstract base classes and run-time polymorphism with interfaces.
Sometimes run-time polymorphism is referred to as dynamic binding.
public class PersonOverriding {
public void display(){ display() is called overridden
System.out.println("I m in person class"); method which present in super
} class
}

public class StudentOverriding extends PersonOverriding {


public void display(){
System.out.println("I m in student class");
display() is called overriding
method which present in sub
}
class.
}

Example of Early Binding


Object Reference (Polymorphic Behavior)
 Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a super class reference is used to refer to a sub class
object.
 A reference variable of a superclass type can point to an object of its subclass
 An example of polymorphism is referring the instance of subclass, with reference variable of
super class.

Example :

Object o = new Object();


o = new String();
o = new Integer(0);

 String is subclass of Object class.


 Integer is subclass of Object class.
Object Reference (Polymorphic Behavior)
 A program can create an array of superclass variables that refer to objects of many subclass
types.
 Allowed because each subclass object is an object of its superclass.

Program - 1 : Demonstration of Polymorphic behavior

public class Game {


public String type(){
return "Indoor & Outdoor";
}
}

public class FootBall extends Game {


public String type(){
return "Outdoor";
}
}

public class Badminton extends Game {


public String type(){
return "Indoor";
}
}
Object Reference (Polymorphic Behavior)
public class GameTest {
public static void main(String[] args) {
Game gm = new Game();
FootBall fb = new FootBall();
Badminton ba = new Badminton();
System.out.println("Call Games's type with superclass reference "
+ " to superclass object " + gm.type()) ;
System.out.println("Call FootBall's type with subclass reference "
+ " to subclass object " + fb.type()) ;
System.out.println("Call Badminton's type with subclass reference "
+ " to subclass object " + ba.type()) ;
gm = fb;
System.out.println("Call FootBall's type with superclass "
+ "reference to subclass object " + gm.type());
gm = ba;
System.out.println("Call Badminton's type with superclass "
+ "reference to subclass object " + gm.type());
}
}

Output :
?

You might also like