[go: up one dir, main page]

0% found this document useful (0 votes)
24 views8 pages

Polymorphism Introduction

The document explains specialized methods in Java, highlighting inherited, overridden, and specialized methods, along with their advantages. It discusses key OOP concepts such as abstraction, encapsulation, inheritance, and polymorphism, emphasizing how polymorphism allows subclasses to exhibit unique behaviors while sharing functionalities with parent classes. Additionally, it addresses the limitations of using parent type references to access child class specialized methods and introduces downcasting as a solution to this issue.
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)
24 views8 pages

Polymorphism Introduction

The document explains specialized methods in Java, highlighting inherited, overridden, and specialized methods, along with their advantages. It discusses key OOP concepts such as abstraction, encapsulation, inheritance, and polymorphism, emphasizing how polymorphism allows subclasses to exhibit unique behaviors while sharing functionalities with parent classes. Additionally, it addresses the limitations of using parent type references to access child class specialized methods and introduces downcasting as a solution to this issue.
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/ 8

Specialized methods in java

Let us understand with the help of code…


Output:
What did we see in the above example??

1. Inherited methods are such methods which are inherited from the
parent class and used without any modification within the child class.
2. Overridden methods are such methods which are inherited from parent
class and are modified as per the requirement of child class and used
within the child class.
3. Specialized methods are such methods which are present only within the
child class and not within the parent class.
4. Advantage of inherited methods is code reusability.
5. Advantage of overridden method is that the child class can modify the
parent class method to satisfy its needs.
6. Advantage of specialized methods is that it increases the features of the
child class when compared to parent class.

Amongst these four pillars of OOP, we have seen Encapsulation and


Inheritance and virtual polymorphism. Now let’s see how to achieve true
polymorphism using inheritance.
1. Abstraction : Abstraction is the process of showing only
essential/necessary features of an entity/object to the outside world
and hide the other irrelevant information. (Will be discussed in detail in
further classes.)
2. Encapsulation : Encapsulation means wrapping up data and member
function (Method) together into a single unit i.e. class. Encapsulation
automatically achieve the concept of data hiding providing security to
data by making the variable as private and expose the property to
access the private data which would be public.
3. Inheritance : The ability of
creating a new class from an
existing class. Inheritance is
when an object acquires the
property of another object.
Inheritance allows a class
(subclass) to acquire the
properties and behaviour of
another class (super-class). It
helps to reuse, customize and
enhance the existing code. So it
helps to write a code accurately
and reduce the development
time.
4. Polymorphism: Polymorphism is derived from 2 Greek words: poly and
morphs. The word "poly" means many and "morphs" means forms. So
polymorphism means "many forms". A subclass can define its own
unique behaviour and still share the same functionalities or behaviour of
its parent/base class. A subclass can have their own behaviour and share
some of its behaviour from its parent class not the other way around. A
parent class cannot have the behaviour of its subclass.
In previous example if we change the main method to the following let’s see
what happens…

We have three different references exhibiting 3 different behaviours. So


now let’s modify the main() and achieve polymorphism.
Creating parent type reference to child object is called loose coupling,
through which we have achieved polymorphism. Whereas before we had
tight coupling, where only child class’s reference can access child class’s
behaviour. For example CargoPlane reference cp can access only
CargoPlane behaviours and not of FighterPlane or PassengerPlane.

There is one limitation of parent type reference to child type. That is, using
parent type reference we cannot directly access the specialized methods of
child class.

With the same example as above here we are trying to access specialized
methods through parent type reference ref. Let’s see if we get any error, if yes
then where is it pointing to…
Output

We can see that we have got compile time error wherever we tried accessing
specialized method using parent type reference.

So now let’s see how to overcome this…


Output:

We have overcome it by changing the type of ref to respective child type


reference explicitly. And this is called as Downcasting.

You might also like