Jumat, 07 Februari 2014

What is Abstraction in Java? Abstract Class or Interface




What is abstraction?


Abstraction in Java or Object oriented programming is a way to segregate implementation from interface and one of the five fundamentals along with Encapsulation, Inheritance, Polymorphism, 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 abstract method with concrete behavior. One example of Abstraction is creating interface to denote common behavior without specifying any details about how that behavior works e.g. You create an interface called Server which has start() and stop() method. This is called abstraction of Server because every server should have way to start and stop and details may differ. As I said earlier Abstraction in Java is implemented using abstract class and interface as discussed in next section. In fact what is abstraction in Java, difference between Abstraction and Encapsulation  is also a very popular core Java interview because strong OOPS skill is one of the primary requirement for Java developers.








What is abstract class in Java


An abstract class is something which is incomplete and you can not create instance of abstract class. If you want to use it you need to make it complete or concrete by extending it. A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended. By the way Java has concept of abstract classes, abstract method but a variable can not be abstract in Java. Popular example of abstract class in Java is ActionListener which has abstract method called actionPerformed(ActionEvent ae). This method is called when an ActionEvent is fired like when you click on JButton. Its 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 body , its just a declaration. In order to use abstract method you need to override that method in sub class.





so when do you use abstraction ? ( most important in my view )


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 . Same concept apply to interface in Java as well, which we will discuss in some other post.





So implementation of those start() and stop() methods should be left to there concrete implementation e.g. Scooter , MotorBike , Car etc.








Abstraction Using Interface in Java




What is Abstraction in Java with ExampleIn Java Interface is an another way of providing abstraction, Interfaces are by default abstract and only contains publicstatic, final constant or abstract methods. Its very common interview question is that where should we use abstract class and where should we use Java Interfaces in my view this is important to understand to design better Java application, you can go for java interface if you only know the name of methods your class should have e.g. for Server it should have start() and stop() method but we don't know how exactly these start and stop method will work. if you know some of the behavior while designing class and that would remain common across all sub classes add that into abstract class. Interface like Runnable interface is good example of abstraction in Java which is used to abstract task executed by multiple thread. Callable is another good abstract of a task which can return value. 








Abstraction : Things to Remember




1) Use abstraction if you know something needs to be in class but implementation of that varies. Abstraction is actually result of thought process and it really need good experience of both domain and Object oriented analysis and design to come up with good abstraction for your project.




2) In Java you can not create instance of abstract class using new operator, its compiler error. Though abstract class can have 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. Abstract method doesn't have body, just declaration.




4) A class automatically becomes abstract class when any of its method declared as abstract.




5) abstract method doesn't have method body.




6) In Java, 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.








Other Object oriented concept tutorials from Javarevisited blog






























Source:http://javarevisited.blogspot.com/2010/10/abstraction-in-java.html

Tidak ada komentar:

Posting Komentar