2. Introduction
Poly – means “Many”
Morphs – “Forms”
Polymorphism: is the ability to treat on objects of any sub-class of a super-
class as if it were an object of the super class.
There fore, it enables you to write programs that process objects that shares
the same super-class (either directly or indirectly) as if they are all objects of
the super-class.
Enables you to program in general rather than program in specific.
In polymorphism objects has the ability of having many forms.
3. Example.
- A Deer is A Animal
- A Deer is A Vegetarian
- A Deer is A Deer
- The Deer class is considered to be polymorphic. Since it has multiples
inheritance.
4. ◦ Polymorphism is java has two types: these are Method overloading and Method
Overriding.
1. Method Overloading (Compile time polymorphism or static Binding):- a 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 the JVM knows which method to invoke by checking the method
signatures.
5.
6. 2. Method Overriding (Run time polymorphism or Dynamic Binding):- In
inheritance, a child class provides it’s own implementations to the parents
method, there by overriding the implementation of the present.
- Two methods are said to be overridden only if they have the same signature, that
is the same name and the same parameters.
- The parent’s method is called and Overridden Method and the child’s method is
Overriding Method.
7.
8. Advantages of polymorphism
1. Method overloading allows method that perform similar or closely related functions to be
accessed through a common name.
2. Method overloading can be implemented on constructors allowing different ways to
initialize objects of a class. This enables you to define multiple constructors for handling
different types of initialization.
3. Method overriding allows a sub class to use all the general definitions that a super-class
provides and adds specialized definitions through overridden methods.
4. Method overriding works together with inheritance to enable code re-use of existing class
without the need for recompilation.
9. Abstract class
Abstraction: is a process of hiding the implementation details and showing only
functionality to the user.
There are some ways to achieve abstraction:
1. Abstract class
2. Interface
1. Abstract class: is a class in which declared with the abstract keyword
It can have abstract and non-abstract methods.
It needs to be extended and it’s method implemented.
It can’t be Instantiated.
Abstract Method: is a method which is declared as abstract and doesn’t have
implementation.
10. Rules of java abstract class
It can’t be Instantiated they can be sub-classed.
It needs to be declared with abstract keyword.
It can have final methods.
It can have constructors and static methods.
E.g abstract voii moviio(ioubli iiltaii ioubli iiltai)i
11.
12.
13. Interface
An interface describes a set of methods that can be called on object, but
doesn’t provide concrete implementations for all methods.
An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
It is a collection of abstract methods. A class can implements an interface
there by inhering the abstract methods of the interface.
Once a class implements an interface, all the objects of that class have an
Is – a relationship with the interface type, and all objects of the class are
guaranteed to provide the functionality described by the interface.
16. Interface can’t be instantiated.
Interface doesn’t contain any constructor.
All the methods of interface are abstract.
Static and final fields can only appear in interface.
An interface is not extended by a class; it is implemented by a class.
An interface is implicitly abstract.
Each method in an interface is also implicit.
Methods in an interface are implicitly public.
Difference between interface and class
Interface properties
19. Extending interfaces
An interface can extend another interface in the same way that a class can
extend another class using extend keyword, results a child interface inherits
methods of the parent interface.
A java class can only extend one parent class. However an interface can
extend more than one parent interface.
e.g. public iitirfaci iociii iitiiis sportsi iviits {
}
Extending multiple interfaces
20. Multiple inheritance in Java by interface
─ If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
21.
22.
23. Interface inheritance
─ A class implements an interface, but one interface extends another interface.