0% found this document useful (0 votes)
31 views

Polymorphism

Polymorphism refers to an object showing different behaviors at different stages of its lifecycle, which in Java is achieved through methods exhibiting different implementations depending on when they are called. The document discusses two types of polymorphism in Java - compile-time polymorphism where binding occurs during compilation based on arguments, and runtime polymorphism where binding occurs during execution based on object creation. It also covers method overriding and typecasting as ways to implement polymorphism.

Uploaded by

chidambaram
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Polymorphism

Polymorphism refers to an object showing different behaviors at different stages of its lifecycle, which in Java is achieved through methods exhibiting different implementations depending on when they are called. The document discusses two types of polymorphism in Java - compile-time polymorphism where binding occurs during compilation based on arguments, and runtime polymorphism where binding occurs during execution based on object creation. It also covers method overriding and typecasting as ways to implement polymorphism.

Uploaded by

chidambaram
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Polymorphism

It is one of the OOP’s principle.

An object showing different behavior at different


stages of it life cycle is consider as polymorphism.

In java polymorphism is performed by methods


that means method showing different behavior at
different stages.
Polymorphism
Types of Polymorphism
1. Compiletime Polymorphism
2.Runtime Polymorphism
Polymorphism
Compile-Time Polymorphism

Method declaration is going to get binded with its definition


by compiler during compilation based on arguments its
know as compile time polymorphism.

Here binding take place before execution only because of


this it is also considers as early binding.

Here binding takes place only once in its life time because of
this re-binding is not possible, so it also consider as static
binding.
Polymorphism
Run-Time Polymorphism

Method declaration is going to get binded with its


definition by JVM during execution based on object
creation is know as Run time polymorphism.

Here binding takes place during execution by JVM hence


it is known as late binding.

Here binding take place based on object creation hence


re-binding is possible so it consider as Dynamic binding.
Polymorphism

It can be achievable by –
• Inheritance
• Method Overriding
• Up-Casting
Polymorphism
Method Overriding
Subclass method maintaining the same method signature as that of
super class with different implementation is know as Method Over
Riding.
Or
Acquiring the property from super class and change its implementation
according to sub-class is consider as Method Over Riding.
Rules for Method Overriding
1. Inheritance
2. Method Signature should be same
3. Access Specifiers visibility can be increased / retained but cannot
decrease the visibility.
4. Final method cannot be over-ridded.
Non-Primitive Casting
Non-Primitive Casting
Converting one non-primitive information to
another non-primitive is known as Non-Primitive
casting.
Classified in to two types :
1. Up-Casting
2. Down-Casting
Non-Primitive Casting
Up-Casting
Converting sub-class type object to super class type
is known as Up-Casting.
Or
Create a sub-class object by providing super class
reference is called as Up-Casting.
Up-Casting is implicitly done by Compiler.
It is also know as Auto-Casting.
Non-Primitive Casting
WHY DO WE NEED UPCASTING ?

It is used to achieve generalization.


It helps to create a generalized container so that the
reference of any type of child object can be stored.
Non-Primitive Casting
Down-Casting
Converting super class type object to sub-class type
is known as Down casting.
To perform down casting up casting is mandatory.
Or
Provide a sub-class reference for up casted object
is called as Down Casting.
To perform down casting extra piece of code is
should be provided by the programmer , hence
it is also consider as Force Casting.
Non-Primitive Casting
WHY DO WE NEED A DOWNCASTING ?

If the reference is up-casted, we can’t use the members


of a subclass.

To use the members of a subclass we need to downcast


the reference to a subclass.
ClassCastException :
It is a RuntimeException.
It is a problem that occurs during runtime while
downcasting.
When and why do we get a ClassCastException?
When we try to convert a reference to a
specific class type and the object does’t have an
instance of that type then we get
ClassCastException.
instanceof Operator
Instance of operator is used for Comparison Operator.
It will check whether the object is a specified class
type or not.
If it is specified class type it returns true else return
false.
It is also known as type comparison operator because
it compares the instance with type.
There must be IS-A-Relationship to perform
instanceOf Operator.
VARIABLE SHADOWING

VARIABLE SHADOWING :
If the superclass and subclass have variables with same name
then it is known as variable shadowing.
Which variable is used, depend on what ?
In variable shadowing binding is done at compile time , hence it
is a compile time polymorphism. Variable used depends on the
reference type and does not depend on the type of object created.
NOTE :
● It is applicable for both static and non static variable.
● It is a compile time polymorphism.
● Variable usage depends on type of reference and does not
depend on type of object created.
METHOD SHADOWING
METHOD SHADOWING :
If a subclass and superclass have the static method with same
signature , it is known as method shadowing.
Which method implementation gets execute, depend on what ?
In method shadowing binding is done at compile time , hence it is
compile time polymorphism. The execution of the method depends on
the reference type and does not depend on the type of object created.
NOTE :
● Return type should be same
● Access modifier should be same or higher visibility than super
class method.
● Method shadowing is applicable only for the static method.
● It is compile time polymorphism
● Execution of implemented method depends on the reference
type of an object.
Packages
A package in java is used to group a related classes ,
interfaces and subclasses. In a simple word it is a folder
which consist of several classes and interfaces.
Note:
Packages contains only class files.
WHY PACKAGE ?
Packages are used to avoid name conflict.
It increases maintainability.
It is used to categorize classes and interfaces.
It increases the access protection.
It is used to achieve code reusability.
Import
Import statement is used to import the
classes or interfaces present in packages.
Import statement should be used before
declaring a class.
import statement should be end with ( ; ).
We can use multiple import statement in a
same program.
Packages
Package should be the first statement in a java
program.
A java source file should contain only one package
Statement.
A package can contain multiple classes/Interfaces but
one class/interface should be public.
If a package contains public class/interface then it is
mandatory to use public class/interface name as a
source file name. Otherwise, we will get Compile
Time Error.
Access Modifiers
Access Modifier are used to control the visibility
level of java components.
All access Modifier are the keywords in java.
Access Modifier are of 4 types:-
1. private
2. default
3. protected
4. public
Access Modifiers
Access Modifiers
private : If we declare class member as private then that member can be
accessible only within that particular class.
This members are not accessible outside the class body or in other class of
same package.
default: If we declare class member as default then that member can be
accessible in entire package, i.e., it can be used in different class of same
package also.
We don’t use any keyword for default access specifiers.
protected: If we declare class member as protected then that member can be
accessible in entire package and also in different package based on the
relationship.
public: if we declare class member as public then that member is accessible in
entire project.
Access Modifiers
Rule 1 :

In java class inside is class is allowed.


Note:
a.method inside an another method------(not possible)
b.Constructor inside an another constructor----(not possible)
c.Constructor inside a method------(not possible)
d.Class inside a class-----(possible)

Rule 2 :
Outer class can have either public/default access only, but inner class can have any access.

Rule 3 :
A java file can have multiple classes, but only one class should have public access.
The class which as public access must be the file name.
Access Modifiers
Rule 4:
Every class in java will have a constructor., if
programmer is not defining the constructor,
compiler will add the default constructor.
If it is default constructor it will have access level
as per that class.
If it is programmer defined constructor we can
given any access level.
ENCAPSULATION
The process of binding the data member of the class within the
class body into a single unit is called as Encapsulation.

A class is an encapsulation of data member and methods.


A package is an encapsulation of class and interface

Encapsulation can be achieved by


--- By private member
----- getter and setter

Advantage of Encapsulation

To achieve data hiding


DATA HIDING

It is a process of restricting the direct access of


data members and provides indirect secured
access of data members through method is
known as data hiding.
ENCAPSULATION
Getters and Setters :
Getter :
Getters are used to access the private members in
another class.
Getters are read only access, it is used to fetch the
information.
Getters follow the return type as per the data type.
ENCAPSULATION
Setters:
Setters are used to provide the value for the
private member.
Setter have write only access.
Setter follow the return type as void.
JAVA BEAN

The class which as private data member should


be initialized through getter and setter is a
Java Bean Class.
Java Bean class should have only non-arg
constructor
Should be Serializables type.
POJO (Plain Old Java Object)
If any class having an arrangement like private
data member initialize through constructor,
getter and setter then it is consider as POJO
POJO can have arg constructor or non-arg
constructor.
Single Ton Class
The class which allows only one object creation that
class is consider as single ton class.
Single Ton class is a java class design pattern to
allow only one object creation.
Programmer can create Single ton class by using
private constructor and control statement.
This concept is mostly suitable when programmer
wants to restrict multiple object creation with a
same content.
Private Constructor
When we declare a constructor with the access
modifier as Private then we cannot access such
constructor outside the class.
The object creation of private constructor is
restricted only inside that particular class.
To access such class object outside the class we use
helper method as public and static.
This helper method are designed to get the object
of such private constructor class.
Interface
Interface is a definition block, which is similar to class
(can define methods and data member).
Data member are by default static and final.
Methods are by default public and abstract.
Interface cannot be Instantiated.
Constructor are not allowed inside an interface.
Multiple inheritance is achievable in interface.
----If interface is inheriting another interface
should use extends keyword
-----If class is inheriting an interface we should use
implements keyword
Interface
Note : Interface object cannot be created but
reference can be created.
An interface can extends another interface but not
class
Class an extends only one class but class can
implements multiple interfaces.
Interface

Factory Class
It is an intermediate class between the
implementation class and the main method.
Factory Method
It is related to object creation we can create an
object without exposing the logic to user and it
use the common interface to create a new
Object.
Interface
Marker Interface

An interface which doesn't have any fields/


An empty interface is called as Marker interface

It is used by JVM during runtime to validate the object


and used to provide some special behavior to the class.

example :Cloneable, Serializable


Interface
Functional Interface

An interface which contains single abstract method but it can


hold multiple default method and static method is called
as Functional Interface.
@FunctionalInterface

why do we go for functional interface ?

To make the code less complex and easier we go for Functional


interface.
Abstraction
It is a process of hiding the implementation but
showing the functionality of a method is known as
Abstraction.
Abstraction is mainly done for 2 reasons –
 Security
 Simplicity
Abstraction can be achieved by
 abstract class
 interface
Abstraction is achieved by loose coupling
Tight Coupling
Whenever one application is fully dependent on
another application for their functionality then it is
called as Tight Coupling.

Loose Coupling
When ever one application is not fully dependent
another application for their functionality then it is
consider as loose coupling.

You might also like