Interface and Abstraction in Java
JDK 1.7 Features
- Interface is a keyword that is used to acheive multiple inheritence.
- Interface is just like a class that is used to hold constant variable and fully abstract method.
- A constant variable that can never change and syntax is like:
public static final int i; - fully abstract method means a method which does not have body like:
public abstract void eat(); - You can never create instance of an interface because interface can not have any constructor.
- Interface always having public property.
- Interface can used by inheritence only.
- A class can implements one or more than one interface seperated by , .
class A implements B,C{}
where B and C is an Interface. - An Interface can extends one or more than one interface seperated by , .
interface A extends B,C{}
where B and C is an interface. - A class can extends another class an also implements one or more than one interface.
class A extends B implements C,D{}
where B is a class and C,D is an interface. - Interface can never make final.
- Interface can be abstract.
- We can acheive loose coupling using interface.
- Interface can never extends or implements a class.
- A marker interface is an interface that has nothing like Cloneable, Serializable etc.
- We can never use super keyword in case of interface.
- All interface methods body provided by its sub class using overriding.
JDK 1.8 Features
- In an interface you can define a non static method using default keyword.
- We can also define static method inside an interface.
- We can use super keyword in case of interface using interface name.
What is Functional Interface ?
- An interface which have single abstract method only.
- An interface can have defined method also.
What is Lambda Expression ?
- Lambda Expression is an expression to optimize your code.
- Lambda Expression apply only with functional interface.
- Lambda Expression can be no parameterized or parameterized.
- Syntax is like :
()-> System.out.println("No Parameterized"); - Syntax is like :
(x)-> System.out.println("Parameterized");
JDK 1.9 Features
In an interface you can have private methods.
Abstraction in Java
Hiding the complexity and showing the functionality to the user that is called Abstraction.
Using abstract class you can acheive Abstraction.
Lets see an Example:
interface Vehicle { void gear(); void headLight(); void backLight(); void wheel(); void seat(); } class Bmw implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Scoda implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Audi implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Duster implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Yamaha implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Tvs implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Suzuki implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Scooty implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } public class Test { public static void main(String[] args) { } }
The problem with above code is complexity means each an every Vehicle need to provide implementations of all methods of Vehicle interface separately.
Lets see this above code using abstraction way:
interface Vehicle { void gear(); void headLight(); void backLight(); void wheel(); void seat(); } abstract class Car implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Bmw extends Car { } class Scoda extends Car { } class Audi extends Car { } class Duster extends Car { } abstract class Bike implements Vehicle { @Override public void gear() { // TODO Auto-generated method stub } @Override public void headLight() { // TODO Auto-generated method stub } @Override public void backLight() { // TODO Auto-generated method stub } @Override public void wheel() { // TODO Auto-generated method stub } @Override public void seat() { // TODO Auto-generated method stub } } class Yamaha extends Bike { } class Tvs extends Bike { } class Suzuki extends Bike { } class Scooty extends Bike { } public class Test { public static void main(String[] args) { } }
Using above example abstract class exactly acheive Abstraction.
No comments:
Post a Comment