Object oriented concepts – Java

Q: What are basic OOPS Concepts?

A: Inheritance : When a child class acquires all the methods and properties of parent class it is called inheritance. Java supports inheritance either using classes or interfaces.

Encapsulation: In above class, we have declared id as private and defined two methods that will act as mutator and accessors for this id propert. Thus we are encapsulateing objects property and  methods with in an object, keeping it safe from outside world.

Polymorphism: In below scenario, we have added 2 declaration of a function add() to child class, one is for adding  integers and another is to add float values. This kind of behavior when same name is used to perform different tasks as per the parameters passed to it is called polymorphism.

In java polymorphism is achieved by function overloading. It is also called Compile time Polymorphism. Compile decides at compile time that which version of function is called depending up on the parameters passed.

function overriding is also type of polymorphism, some times it is called Run time polymorphism, because the methods version is decided depending on the reference type of object at run time.

 

Important Notes:

 

  • return type is not part of method signature, so mere change of return types will not qualify a method to be polymorphic.
  • if only parameter names are changed then that is not polymorphism.

 

For above methods to be polymorphic you need to use them as –

Add a Comment

Your email address will not be published. Required fields are marked *