OOP Concepts

1. What is polymorphism?
The ability to define a function in multiple forms is called Polymorphism. In java, c++ there are two types of polymorphism : compile time polymorphism (overloading) and runtime polymorphism (overriding).

Mehtod overriding: Overriding occurs when a class method has the same name and signature as a method in parent class.
Mehtod overloading: Overloading is determined at the compile time. It occurs when several methods have same names with:
  • Different method signature and different number or type of parameters.
  • Same method signature but different number of parameters.
  • Same method signature and same number of parameters but of different type
  Example :-

class BookDetails {
             String title;
             setBook(String title){}
class ScienceBook extends BookDetails{
             setBook(String title){} //overriding
             setBook(String title, String publisher,float price){} //overloading

2. What is inheritance?
Inheritance allows a Child class to inherit properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.

3. What is an abstraction ?
Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words user will have the information on what the object does instead of how it does it.

In Java, Abstraction is achieved using Abstract classes, and Interfaces.

Abstract Class

A class which contains the abstract keyword in its declaration is known as abstract class.
·         Abstract classes may or may not contain abstract methods ie., methods without body ( public void get(); )
·         But, if a class has at least one abstract method, then the class must be declared abstract.
·         If a class is declared abstract it cannot be instantiated.
·         To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it.
·         If you inherit an abstract class you have to provide implementations to all the abstract methods in it.


Abstract Methods:

If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.
·         An abstract method contains a method signature, but no method body.

4. What is encapsulation?
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as single unit. In encapsulation the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefore it is also known as data hiding.
Which enforce protecting variables, functions from outside of class, in order to better manage that piece of code and having least impact or no impact on other parts of program due to change in protected code.
You can completely encapsulate a member be it a variable or method in Java by using private keyword and you can even achieve a lesser degree of encapsulation in Java by using other access modifier like protected or public.

No comments:

Post a Comment