M.Rajshree
II-MSC(IT)
Nadar saraswathi college of arts&science
Polymorphism is the ability of a message to
be displayed in more than one form
 The most common use of polymorphism in
OOP occurs when a parent class reference is
used to refer to a child class object.
Any Java object that can pass more than one a
test is considered to be polymorphic
Create a completely separate class specific to
each type of object
These object classes would share a lot of
members in common, but they would be
completely separate from each other
for example, that had the generic object
members such as the speed property and the
travel method
There is another object oriented
Programming concept that is closely related
to abstract classes called interface
Interfaces allow us to define polymorphism
in a declarative way unrelated to
implementation
In interfaces, you can only define (not
implement) methods inside the parent class
Use the concept of an Interface class that will
define (but will not implement) common
properties and methods for the more specific
classes
Create separate classes then implement each
specific type of object that you will need
And that use the interface class to specify
common functionality
 The classes that implement the Interface must
implement all members of the Interface
 An Interface is a class that has only a method
body but no implementation
Example:
public interface shape
{
void draw();
}
Shape is an interface with only one method,
i.e. draw(). Basically, it is a contract that when
you are implementing that interface, you have
to implement the draw functionality, or else
you are not a complete class
 So for any class like Triangle or Rectangle,
when we implement that Interface, they must
implement the draw() method
 We sometimes use an arraylist, we are not as
concerned about how the feature set by the list
interface have been implemented by arraylist
Example :
List l = New ArrayList();
l.add()
 we needed to use an LinkedList instead of
an ArrayList, we could easily do that by changing:
List l = new LinkedList();
 ArrayList used in our program because we used
them through the contract published by the Interface
 The methods cannot be implemented inside the
interface
 Variables (properties) cannot be defined inside the
interface
 All the methods defined inside the interface need to
be implemented in the child class
 All the necessary variables need to be defined
inside the child class
 Man interface enforces its implementing classes to
implement all the methods in the interface
 Implementation interfaces, there are several
rules a class can implement more than
one interface at a time
A class can extend only one class but it
implement many interfaces
An interface can extend another interface in
a similar way as a class can extend another
class
Thank you

Implementing polymorphism

  • 1.
  • 2.
    Polymorphism is theability of a message to be displayed in more than one form  The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one a test is considered to be polymorphic
  • 3.
    Create a completelyseparate class specific to each type of object These object classes would share a lot of members in common, but they would be completely separate from each other for example, that had the generic object members such as the speed property and the travel method
  • 4.
    There is anotherobject oriented Programming concept that is closely related to abstract classes called interface Interfaces allow us to define polymorphism in a declarative way unrelated to implementation In interfaces, you can only define (not implement) methods inside the parent class
  • 5.
    Use the conceptof an Interface class that will define (but will not implement) common properties and methods for the more specific classes Create separate classes then implement each specific type of object that you will need And that use the interface class to specify common functionality
  • 6.
     The classesthat implement the Interface must implement all members of the Interface  An Interface is a class that has only a method body but no implementation Example: public interface shape { void draw(); }
  • 7.
    Shape is aninterface with only one method, i.e. draw(). Basically, it is a contract that when you are implementing that interface, you have to implement the draw functionality, or else you are not a complete class  So for any class like Triangle or Rectangle, when we implement that Interface, they must implement the draw() method
  • 8.
     We sometimesuse an arraylist, we are not as concerned about how the feature set by the list interface have been implemented by arraylist Example : List l = New ArrayList(); l.add()  we needed to use an LinkedList instead of an ArrayList, we could easily do that by changing: List l = new LinkedList();  ArrayList used in our program because we used them through the contract published by the Interface
  • 9.
     The methodscannot be implemented inside the interface  Variables (properties) cannot be defined inside the interface  All the methods defined inside the interface need to be implemented in the child class  All the necessary variables need to be defined inside the child class  Man interface enforces its implementing classes to implement all the methods in the interface
  • 10.
     Implementation interfaces,there are several rules a class can implement more than one interface at a time A class can extend only one class but it implement many interfaces An interface can extend another interface in a similar way as a class can extend another class
  • 11.