Recent Posts

Sunday, 10 July 2016

Interfaces Tutorial Part 2



Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface.
E.g
Serializable, Cloneable,  Remote interface

* These interfaces are marked from some ability.
E.g
1. By implementing serializable interface we can send object across the network and we can save state of object to a file. This extra ability is provided through Serializable interface.

2. By implementing cloneable interface our object will be in a position to provide exactly duplicate object.

Q. Marker interface won't contain any method then how the objects will get that special ability?
Ans. JVM is responsible to provide required ability in marker interfaces

Q. Is it possible to create our own marker interface?
Ans. Yes. But customization of JVM is required.

Adapter class
* Adapter class is a simple java class that implements an interface only with emplty implementation.
E.g
interface X {
   m1();
   m2();
   m3();
}

abstract class Adapter Y implements X {
   m1() {}
   m2() {}
   m3() {}
}
     If we implement an interface directly compulsory we should provide implementation for every method of that interface whether we are interested or not and whether it is required or not. It increases length of the code. So that readability will be reduced.
class Test implements X {
   m1() { }
   m2() { }
   m3() { 
   ----
   ----
   }
}

class Test extends Adapter X {
   m3() {
   -----
   -----
   }
}
     If we extends adapter class instead of implementation interface directly then we have to provide implementation of only for required method but not all this approach reduce length of the code and improves readability.

Concrete class Vs abstract class Vs interface
     We don't know anything about implementation just we have requirement specification. Then we should go for interface.
     We are talking about implementation but not completely (Just partially implementation) then we should go for abstract class.
     We are talking about implementation completely and ready to provide service, then we should go for concrete class.

Interface Vs Abstract Class


Interface
Abstract Class
We don't know anything about implementation just we have requirement specification. Then we should go for interface.
We are talking about implementation but not completely (Just partially implementation) then we should go for abstract class.
Every method present inside interface is by default public and abstract.
Every method present inside abstract class need not be public and abstract. We can take concrete methods also.
The following modifiers are not allowed for interface methods
strictfp, protected, static, native, private, final, synchronized
There are no restrictions for abstract class method modifier i.e., we can use any modifier
Every variable present inside interface is public, static, final by default whether we are declare or not
Abstract class variables need not be public, final, static
For the interface variables we cannot declare the following modifiers private, protected, transient, volatile
There are no restrictions for abstract class variable modifier i.e., we can use any modifier
For the interface variables compulsory we should perform initialization at the time of declaration only.
For the abstract class variables there is no restriction like performing initialization at the time of declaration
Inside interface we can't take instance and static blocks
Inside abstract class we can take instance and static blocks
Inside interface we can’t take constructor
Inside abstract class we can take constructor

Q. Inside abstract class we can take constructor but we can't create an object of abstract class, what is the need?
     Abstract class constructor will be executed whenever we are create child class object to perform initialization of parent class instance variable at parent level only and this constructor meant for child object creation only.

Q. Inside interface every method should be abstract where as in abstract class also we can take only abstract methods then what is the need of interface?
     Interface purpose we can replace abstract class but it is not a good programming practice we are miss using the role of abstract class.
     We should bring abstract class into the picture whenever we are talking about implementation.

Next Tutorial  Exception Handling Tutorial

Previous Tutorial  Interfaces Tutorial Part 1

No comments:

Post a Comment