Skip to content
Home » Java Programing Language » Abstraction In Java

Abstraction In Java

Abstraction

  1. Abstract is a hiding of complex implementation and showing only the functionality.
  2. Abstraction can be achieved by 2 ways.
    1. By Using abstract classes.
      1. Can achieve 0 – 100% abstraction.
    2. By Using Interfaces.
      1. Can achieve 100% abstraction.

 

Abstract Class

  1. Abstract classes are use to achieve 0-100% abstraction.
  2. Abstract class can be created by using abstract keyword.
  3. Abstract keyword can be use only to a class or a method.
  4. You cannot create object of abstract class, but can be use as a reference.
  5. Abstract class can have abstract methods and non-abstract(concrete) methods.
  6. Abstract methods are the methods without implementation and just created by declaring it.
  7. If any class has a abstract method then that class must be mark as abstract.
  8. Mostly abstract classes are use as a top level in a hierarchy.
  9. Abstract class used as a super class, if any abstract class extends by concrete class, then child class has to provide implementation (Override) for all the abstract methods from abstract class.
  10. You can create constructors inside Abstract class, it gets invoked at the time of Sub class object creation.
  11. Can create static/final variables.
  12. Can create static/final method but that method must be non-abstract.
  13. If sub-class of abstract class cannot provide the implementation for abstract methods them mark sub class as an abstract class.
  14. Abstract class can extends another abstract class or can also extends concrete class.

Interface

  1. Interface is use to achieve 100% abstraction.
  2. In Interface can create only abstract methods (till jdk 1.7).
  3. Interface are not a class and can be create by using interface keyword.
  4. Each interface gets a .class file after compilation even if it is not a class.
  5. By default every method of interface class is public abstract.
  6. By default every variable of interface is public static final.
  7. Can use only public access modifier inside interface.
  8. Cannot create object of interface but can be use as a reference variable.
  9. Interface are use as a top level in a hierarchy.
  10. Interfaces implements by concrete class or abstract class.
  11. If any interface implemented by concrete class, then that class has to provide implementation for all the abstract methods.
  12. If any interface implements by abstract class, then that class is not necessary to provide implementations for all abstract methods.
  13. Cannot create constructor inside interface.
  14. Once interface can extends another interface.
  15. One concrete class/abstract class can implements more than one interface.
  16. One interface can extends more than one interface.
  17. One concrete class/abstract class can extends another concrete class/abstract class and implements more than one interface

Multiple Inheritance By Interface

 

Type of Interface

  1. Marker Interface

    1. Is an interface which do not have any member (No Variables and no Methods).
    2. These interfaces can be use to provide special feature at the time of class execution.
    3. Serializable interface is a build-in marker interface.
  2. Functional interface

    1. It is an interface which has only one abstract method.
    2. These interfaces are used to achieve function programming (Lambda Expression) in java 8.
    3. These interfaces are introduced in java from JDK-1.8 and above.
    4. @FunctionalInterface annotation is also introduce to make sure that created interface is functional interface.
    5. Runnable, Predicate, Cloneable interfaces are some examples of build-in functional interface.

 

Interfaces changes in JDK 1.8 and above

  1. Functional interface concept is introduced in Java
  2. From Java 1.8 and above versions can create implemented methods (non-abstract) methods inside interface. These methods are called as default method.
  3. Can create static method inside interface and also can create main method inside interface.

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !