Abstraction
In Java we have interfaces and abstract classes where we can provide abstract methods(methods without definition) and ask
the implementation classes to provide the definition.
Also while sharing the APIs, we can just provide the Interface/Contract imstead of giving all the details.
That way we hide the implementation details that are not required to call the APIs and keep the things abstract.
So in Java, we achieve abstraction though abstract classes or interfaces.
Interfaces : Prior to Java 8
- Interfaces had only public, static and final fields
- Interfaces had only public and abstract methods
All methods in an interface are public by default, so need to mark the methods as public.
- Static methods : can define static util methods as part of the interfaces if required.
- Default methods : We can also add some default method that can utilized by the interfaces/implementing classes. Prior to Java 8, if you wanted to add a new method to the interface, how would you do it ?
- Functional interfaces : The interfaces that contains only one abstract methods. Functional interfaces provide the capability to use lambda expressions. To know more can go through LinkedHashMap
- Abstract class can define abstract methods that are to be implemented by the inherited class
- Abstract class can have abstract methods as well as concrete methods
- Abstract class can have methods with different access specifiers - public, private, protected and default
- Abstract class can have static or non static methods
- Abstract class can have a constructor
First approach :
Add a new abstract method to the interface – what would it do ? All the implementing classes will have to define that method too, all the implementing classes must be updated or code will not compile anymore that would be disastrous as programmers may not be willing to change the existing code.
Second Approach(A better approach): Create a new interface implementing the existing interface and use new interface wherever that is required. And we will not need to change any other classes. But still we will end up creating a new interface.
Thrid Approach (Using default methods): New method can be added to the interface as a default method.
Classes that implement interfaces modified with new default or static methods do not have to modify or recompile them to accommodate the additional methods.
Post Java 9
As part of Java 9 private methods can also be added to the interfaces
Abstract class
How to declare an interface :
InterfaceInterface example: Vehicle interface and the implementing class Car.{ //fields – public,static and final //abstract/public method(s) //default method(s) //static method(s) //private method(s) }
package oops.abstraction; public interface Vehicle { void start(); void accelerate(int speedUpAmount); void applyBreak(int speedDownAmount); }
package oops.abstraction; public class Car implements Vehicle { private boolean isStarted = false; private int speed = 0; @Override public void start() { isStarted = true; } @Override public void accelerate(int speedUpAmount) { this.speed = this.speed + speedUpAmount; } @Override public void applyBreak(int speedDownAmount) { this.speed = this.speed - speedDownAmount; } public boolean isStarted() { return isStarted; } public int getSpeed() { return speed; } }
Abstract class example : //overloading example
public abstract class Shape { String name; public abstract double getArea(int n); public String getName() { return name; } public void setName(String name) { this.name = name; } }
Implementing class:
package oops.polymorphism; public class Square extends Shape{ @Override public double getArea(int n) { // TODO Auto-generated method stub return n*n; } }