0% found this document useful (0 votes)
23 views19 pages

Polymorphsm

The document discusses polymorphism in Java. Polymorphism allows objects of different subclasses to be treated as objects of a single super class, and allows the proper methods to be automatically selected based on the subclass. It provides examples of how polymorphism works, including how a parent class reference can refer to a child class object and call the child's methods, and how a method can accept parameters of a parent class and work with objects of child classes. It also discusses static and dynamic binding, explaining that dynamic binding determines which method is called at runtime based on the object's actual type.

Uploaded by

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

Polymorphsm

The document discusses polymorphism in Java. Polymorphism allows objects of different subclasses to be treated as objects of a single super class, and allows the proper methods to be automatically selected based on the subclass. It provides examples of how polymorphism works, including how a parent class reference can refer to a child class object and call the child's methods, and how a method can accept parameters of a parent class and work with objects of child classes. It also discusses static and dynamic binding, explaining that dynamic binding determines which method is called at runtime based on the object's actual type.

Uploaded by

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

Polymorphism

Agenda

• What is & Why Polymorphism?


• Polymorphism Examples.
• Dynamic binding.
What is & Why
Polymorphism?
What is Polymorphism

• Generally, polymorphism refers to the ability to appear


in many forms.
• In java
• Allows multiple objects of different subclasses to be treated
as objects of a single super class, while automatically
selecting the proper methods to apply to a particular object
based on the subclass it belongs to

3-4
Polymorphism Example

• For example, given a base class shape, polymorphism


enables the programmer to define different area
methods for any number of derived classes, such as
circles, rectangles and triangles.
• No matter what shape an object is, applying the area
method to it will return the correct results.

3-5
Polymorphism Examples
Example 1: Polymorphism

• When parent class reference is used to refer to a


child class object.
• Given the parent class Person and the child class
Student, we add another subclass of Person which is
Employee.
• Below is the class hierarchy

3-7
Example 1: Polymorphism

• Now suppose we have a printName method in our


super class Person, and we override this method in
both Student and Employee subclass's.
• public class Student extends Person {
public void printName(){
System.out.println("Student Name" );
}}
• public class Employee extends Person {
public void printName(){
System.out.println("Employee Name" );
}}

3-8
Example 1: Polymorphism

• In Java, we can create a reference that is of type super


class, Person, to an object of its subclass, Student.
• public static main( String[] args ) {
Student studentObject = new Student();
Employee employeeObject = new Employee();
// Person reference point to a Student object
Person ref = studentObject;
// Calling printName() of the Student object instance
ref.printName();
}

3-9
Example 1: Polymorphism

• Going back to our main method, when we try to call


the printName method of the reference Person ref,
the printName method of the Student object will be
called.

• Now, if we assign ref to an Employee object, the


printName method of Employee will be called.

3-10
Example 2: Polymorphism

• Another example that illustrates polymorphism is


when we try to pass a reference to methods as a
parameter.
• Suppose we have a static method printlnformation
that takes in a Person reference as parameter.

public static void printInformation( Person p ){


// It will call printName() method of the
// actual object instance that is passed
p.printName();
}

3-11
Example 2: Polymorphism

• We can actually pass a reference of type Employee


and type Student to the printInformation method as
long as it is a subclass of the Person class.

public static main( String[] args ) {


Student studentObject = new Student();
Employee employeeObject = new Employee();
printInformation( studentObject );
printInformation( employeeObject ); Output:
Student Name
Employee Name

3-12
Dynamic binding
Static and dynamic binding

Static binding:
• Which Method will be invocated will be
determined at compile time.
Dynamic binding:
• Which Method will be invocated will be
determined at runtime time.

3-14
Static and dynamic binding

What happen when I make thing like


this?
Animal a=new Horse();
a.eat();//will call which one
a.buck();
3-15
Static and dynamic binding

Note :
• Method invocations allowed by the compiler are
based solely on the declared type of the
reference, regardless of the object type
• Which overridden version of the method to call (in
other words, from which class in the inheritance
tree) is decided at runtime based on object type

3-16
Static and dynamic binding

Compiler
error

3-17
Questions
Thanks

You might also like