Tuesday 25 April 2017

OOPS Concept

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, the user will have the information on what the object does instead of how it does it.

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, i.e., 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 an abstract.
  • abstract keyword is used to declare the method as abstract.
  • You have to place the abstract keyword before the method name in the method declaration.
  • An abstract method contains a method signature, but no method body.
  • Instead of curly braces, an abstract method will have a semoi colon (;) at the end.
                                           
                                                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.

What is multiple inheritance and does java support?
Ans) If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes. The problem with with multiple inheritance is that if multiple parent classes have a same method name, then at runtime it becomes diffcult for the compiler to decide which method to execute from the child class. To overcome this problem it allows to implement multiple Interfaces. The problem is commonly referred as What is Diamond Problem.

                                              Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
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.

The encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how thinigs work and just exposing the requests a user can do.Encapsulation is used for access restriction to a class members and methods.
Access modifier keywords are used for encapsulation in object oriented programming. For example, encapsulation in java is achieved using privateprotected and public keywords.
                                        

                           Polymorphism

When one task is performed by different ways i.e. known as polymorphism. For example: to convince the customer differently, to draw something e.g. shape or rectangle etc.
In java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

Mehtod overriding: Overriding occurs when a class method has the same name and signature as a method in parent class. When you override methods, JVM determines the proper method to call at the program’s run time, not at the compile time.
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 of Overloading
int add(int a,int b)
 float add(float a,int b)
 float add(int a ,float b)
 void add(float a)
 int add(int a)
 void add(int a) //error conflict with the  method int add(int a)
class BookDetails {
  String title;
  setBook(String title){}
}
class ScienceBook extends BookDetails {
  setBook(String title){} //overriding
  setBook(String title, String publisher,float price){} //overloading
}