Sunday 14 December 2014

The Difference's Between Interface and Abstract Class JNTU JAVA Unit II

  1. Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
  2. Variables declared in a Java interface is by default final. An  abstract class may contain non-final variables.
  3. Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
  4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
  5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
  6. A Java class can implement multiple interfaces but it can extend only one abstract class.
  7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
  8. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

Substitutability, Subtype and Subclass

Substitutability:
•Substitutability means, the type of the variable does not have to match with the type of the value assigned to that variable.
•Substitutability cannot be achieved in conventional languages in C, but can be achieved in Object Oriented languages like Java.
•We have already seen the concept of “Assigning a subclass object to superclass variable or reference”. This is called substitutability. Here I am substituting the superclass object with the object of subclass.

•When new subclasses are constructed by extending the superclass, we can say that substitutability is satisfied under the following conditions:
1) Instances of the subclass must contain all the fields(instance variables) of the super class.
2) Instances of the subclass must implement, through inheritance all functionality defined for the super class.
3) Thus, the instance of a subclass will contain all the characteristics and behavior of the super class. So, the object of the subclass can be substituted in the place of a superclass object.

Subtype:

The term “subtype” is used to describe the relationship between different types that follow the principle of “substitution”.
•If we consider two types(classes or interfaces) A and B, type B is called as a subtype of A, if the following two conditions are satisfied:
1) The instance(object) of type B can be legally assigned to the variable of type A.
2) The instance(object) of type B can be used by the variable of type A without any observable change in its behavior.

Subclass:

•A class which inherits the characteristics and behavior of another class is called a subclass.
•A subclass can be easily identified in the programs, by looking at the “extends” keyword.


 

 


In the above example, Shape is the superclass and the “subclasses” are Triangle and Rectangle. 
•We are substituting the objects of Triangle and Rectangle since they satisfy the principles of “substitutability”. 
•Since Triangle and Rectangle are satisfying the principles of “substitutability” and also the conditions for “subtype”, they can be called as “subtypes” of the type Shape.

No comments:

Post a Comment