Ads Sponsored By Google :)

Saturday, 19 January 2013

Define|Definition of|for data|abstraction vs encapsulation in java with example code|detail|meaning

abstraction in java data abstraction in java abstraction in java with example abstraction example in java
encapsulation vs abstraction in java define abstraction in java example of abstraction in java example for abstraction in java abstraction definition in java definition of abstraction in java abstraction vs encapsulation in java data abstraction in java with example abstraction in java definition abstraction in java with example code abstraction in java in deatil abstraction in java meaning explain abstraction in java

   Abstraction :

          Abstraction is one of the distinct feature in Java or Object oriented programming 
    is a way to segregate or isolate the implementation from interface. Abstraction is one 
   of the fundamental principles along with Encapsulation, Inheritance, Polymorphism,
   Class and Object.
        Abstraction in Java can be achieved by using interface and abstract class in Java. 
  An interface or abstract class is something which is not implemented or concrete, 
 something which is incomplete. In order to use interface or abstract class we need to 
 implement abstract method into a class by extend and implement abstract class, interface 
 with or implement or concrete behavior.

       For example regarding Abstraction is creating interface to denote common features or 
     behavior without specifying any details about how that behavior works e.g. You create an 
     interface called Servlet which has start() , service() and stop() methods. Service() method
     is used for business logic it is implemented in user defined class. This is called abstraction 
     of Servlet because every servlet.

    Definition of Abstraction:

         Abstraction is only providing essential data with respect to any background information. 
     This is achieved in java by 

      1) Abstract Class and Interface: We are not know about the function implementation of 
         code until unless at run time.
        we have the information about the concrete class which implements Interface or Extends 
        Abstract Class If we go for a particular method it directly take us to Interface method not to 
        Class which actually  implementing this method. 
     2)  Hiding data by access modifier only gives access that are required.
     3)  Object - Nothing can be access by Object ref. 

    What is abstract class in Java

             An abstract class is something which is incomplete and you cannot 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 cannot be abstract in Java.
          In General abstract class is a combination of abstract methods and concrete methods. 
     But we can create instance for abstract class.

          In real time in JDK we are applying abstraction in most of the cases. For example for 
    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 :

     The Following Code will clarifies how abstract class and abstract methods are in 
           JButton  ok = new JButton("OK");
           ok.addActionListener(new ActionListener(){
           public void  actionPerformed(ActionEvent ae){
               //code to handle event

    What is Interface in java ?
           In Java Interface is an another way of providing abstraction, Interfaces are by default 
     abstract and only contains public static, final constant or abstract methods.

    Point To Remember Regarding Abstraction In Interview View:

               About abstraction in two ways we can provide abstraction one is abstract class and  
          another way is through Interface. But It’s very common interview question is that where 
          should we use abstract class and where should we use Java Interfaces ?

      In my view it is very important to understand to design better Java application for java.

  1)        If 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.
  2)     if you know some of the behavior while designing class and that would remain common across all subclasses add that into abstract class. Interface like Runnable are good example of abstraction in Java which is used to abstract task executed by multiple thread.

   1)   Use abstraction if you know something needs to be in class but implementation of that 
   2)   In Java you cannot create instance of abstract class, its compiler error.
   3)   Abstract is a keyword in java.
   4)   A class automatically becomes abstract class when any of its method declared as 
   5)  abstract method doesn't have method body.
   6)  variable can not be made abstract , its only behavior or methods which would be 
   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.

         Object Oriented Programming Principles Are :

   1)      Class
   2)      Object
   3)      Data Abstraction
   4)      Data Encapsulation
   5)      Inheritance
   6)      Polymorphism
   7)      Dynamic Binding

No comments:

Post a Comment