Abstraction
- Abstract is a hiding of complex implementation and showing only the functionality.
- Abstraction can be achieved by 2 ways.
- By Using abstract classes.
- Can achieve 0 – 100% abstraction.
- By Using Interfaces.
- Can achieve 100% abstraction.
- By Using abstract classes.
Abstract Class
- Abstract classes are use to achieve 0-100% abstraction.
- Abstract class can be created by using abstract keyword.
- Abstract keyword can be use only to a class or a method.
- You cannot create object of abstract class, but can be use as a reference.
- Abstract class can have abstract methods and non-abstract(concrete) methods.
- Abstract methods are the methods without implementation and just created by declaring it.
- If any class has a abstract method then that class must be mark as abstract.
- Mostly abstract classes are use as a top level in a hierarchy.
- 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.
- You can create constructors inside Abstract class, it gets invoked at the time of Sub class object creation.
- Can create static/final variables.
- Can create static/final method but that method must be non-abstract.
- If sub-class of abstract class cannot provide the implementation for abstract methods them mark sub class as an abstract class.
- Abstract class can extends another abstract class or can also extends concrete class.
Interface
- Interface is use to achieve 100% abstraction.
- In Interface can create only abstract methods (till jdk 1.7).
- Interface are not a class and can be create by using interface keyword.
- Each interface gets a .class file after compilation even if it is not a class.
- By default every method of interface class is public abstract.
- By default every variable of interface is public static final.
- Can use only public access modifier inside interface.
- Cannot create object of interface but can be use as a reference variable.
- Interface are use as a top level in a hierarchy.
- Interfaces implements by concrete class or abstract class.
- If any interface implemented by concrete class, then that class has to provide implementation for all the abstract methods.
- If any interface implements by abstract class, then that class is not necessary to provide implementations for all abstract methods.
- Cannot create constructor inside interface.
- Once interface can extends another interface.
- One concrete class/abstract class can implements more than one interface.
- One interface can extends more than one interface.
- One concrete class/abstract class can extends another concrete class/abstract class and implements more than one interface
Type of Interface
Marker Interface
- Is an interface which do not have any member (No Variables and no Methods).
- These interfaces can be use to provide special feature at the time of class execution.
- Serializable interface is a build-in marker interface.
Functional interface
- It is an interface which has only one abstract method.
- These interfaces are used to achieve function programming (Lambda Expression) in java 8.
- These interfaces are introduced in java from JDK-1.8 and above.
- @FunctionalInterface annotation is also introduce to make sure that created interface is functional interface.
- Runnable, Predicate, Cloneable interfaces are some examples of build-in functional interface.
Interfaces changes in JDK 1.8 and above
- Functional interface concept is introduced in Java
- From Java 1.8 and above versions can create implemented methods (non-abstract) methods inside interface. These methods are called as default method.
- Can create static method inside interface and also can create main method inside interface.
Join Telegram : Click Here
All Full Stack Java Study Material
Job’s For Fresher