Wednesday, March 8, 2017

OOPS Concepts


What is Abstraction in Java? Abstract Class or Interface


Abstraction in Java or OOP is a way to segregate implementation from interface and one of the five fundamentals along with Encapsulation, Inheritance, polymorphic, class and object. Abstraction in Java is achieved by  using interface and abstract class in Java. 

An interface or abstract class is something which is not concrete , something which is incomplete. In order to use interface or abstract class, we need to extend and implement an abstract method with concrete behavior.
You create an interface called server which has the start() and stop() method. This is called abstraction of Server because every server should have a way to start and stop and details may differ. 

Abstract Classes In Java:


Example:
A popular example of abstract class in Java is ActionListener which has an abstract method called actionPerformed(ActionEvent ae). This method is called when an ActionEvent is fired like when you click on JButton. It's common in java to attach ActionListener with JButton by implementing abstract method actionPerformed(ActionEvent ae) using Anonymous class, as shown in below Example :

JButton  ok = new JButton("OK");
ok.addActionListener(new ActionListener(){
           public void  actionPerformed(ActionEvent ae){
               //code to handle event
           }
});

An abstract method in Java doesn't have the body , it's just a declaration


when do you use abstraction ?

when you know something needs to be there but not sure how exactly it should look like. e.g. when I am creating a class called Vehicle, I know there should be methods like start() and stop() but don't know how that start and stop method should work, because every vehicle can have different start and stop mechanism e..g some can be started by kicking or some can be by pressing buttons


Abstraction Using Interface

Interfaces are by default abstract and only contains public, static, final, constant or abstract methods.

1. All methods declared inside Java Interfaces are implicitly public and abstract, 

2. All variables declared inside interface is implicitly public final variable or constants.

3. In Java its legal for an interface to extend multiple interface.

many design patterns like decorator patternFactory method pattern  or Observer design pattern  makes very good use of Java interfaces.


If you know some of the behavior while designing class and that would remain common across all subclasses add that into an abstract class. An interface like Runnable interface is a good example of abstraction in Java which is used to abstract task executed by multiple threads.

Abstraction: things to remember:

1) Use abstraction if you know something needs to be in class but the implementation of that varies.
2) In Java, you can not create an instance of the abstract class using the new operator, its compiler error. Though abstract class can have a constructor.
3) abstract is a keyword in Java, which can be used with both class and method.  Abstract class can contain both abstract and concrete method. An abstract method doesn't have the body, just declaration.
4) A class automatically becomes abstract class when any of its methods declared as abstract.
5) abstract method doesn't have method body.
6) In Java, a variable can not be made abstract , its only class or methods which would be abstract.

7) If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. alternatively, this class can also be abstract.

Abstraction and Encapsulation:

Abstraction means that you've just named the behaviors and outline of your class, and implementations are there to be written somewhere else in detail (not mattering using which way/strategy)

Encapsulation means that you implement your whole class (not mattering whether you've made an abstract before or not) and you put some restrictions in this very class so that no one can access it or change it directly.
Well Abstraction is the concept and encapsulation is mechanism to fulfill that concept..

Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifier.
encapsulation is also called data hiding.

you can only change or access this variable directly inside this class- getter() and setter() methods.

Benefits:


1. Encapsulated Code is more flexible and easy to change with new requirements.
2. Encapsulation in Java makes unit testing easy.
3. Encapsulation in Java allows you to control who can access what.
4. Encapsulation also helps to write immutable class in Java which is a good choice in multi-threading environment.
- mutable objects - low Performance and tracking changes hard. 
5. Encapsulation reduces coupling of modules and increases cohesion inside a module because all piece of one thing is encapsulated in one place.
coupling: how one class in independent from other class. how code change in some part will effects other modules of program. code should be loose coupled. 
cohesion: Refers to degree to which the elements fo a module belong together. code should be highly cohesion. 
6. Encapsulation allows you to change one part of code without affecting other parts of code.

Note :
tracking changes using immutable objects.
example :  we can change the values of other parameters based on one input change, which is not possible in mutable objects. 
if(code.inputchange){
this.otherparameters.value = this.otherparameter.value(inputchange;
}


Design Pattern based on Encapsulation in Java

Many design pattern in Java uses encapsulation concept, one of them is Factory pattern which is used to create objects. Factory pattern is a better choice than new operator for creating an object of those classes whose creation logic can vary and also for creating different implementation of the same interface. BorderFactory class of JDK is a good example of encapsulation in Java which creates different types of Border and encapsulate creation logic of Border. Singleton pattern in Java also encapsulate how you create an instance by providing getInstance() method. since an object is created inside one class and not from any other place in code you can easily change how you create an object without affect another part of code.

Polymorphism:

Polymorphism is an Oops concept which advice use of common interface instead of concrete implementation while writing code.
Java has excellent support of polymorphism in terms of Inheritance, method overloading and method overriding. Method overriding allows Java to invoke method based on a particular object at run-time instead of declared type while coding. 


Where to use polymorphism in code:

1. Method argument:
Always use super type in method argument that will give you leverage to pass any implementation while invoking a method.

2. Variable names:
Always use Super type while you are storing reference returned from any Factory method in Java, This gives you the flexibility to accommodate any new implementation from Factory

3. The return type of any method is another place where you should be using interface to take advantage of Polymorphism in Java. In fact, this is a requirement of Factory design pattern in Java to use interface as a return type for factory method.


==============


Method overloading and method overriding uses concept of Polymorphism in Java where method name remains same in two classes but actual method called by JVM depends upon object at run time and done by dynamic binding in Java

















No comments:

Post a Comment

Ericsson Interview

Round 1 : mostly Java 1. SingleTon - In multiple servers in a culstered envirnment. 2. HashMap put method internally, how that works 3....