Thursday, 8 March 2012

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.

4 comments:

  1. It is useful article for beginners.thank you
    web programming tutorial
    welookups

    ReplyDelete
  2. Thanks man..! Our while institution is using your material!

    ReplyDelete
  3. Nice blog, Java is an object oriented programming languages. Learn java course from Uncodemy. Uncodemy offers best java course in Kanpur.

    ReplyDelete
  4. Thank you for sharing! I always appreciate engaging with excellent content like this. The ideas presented are not only valuable but also creatively presented, making the post thoroughly enjoyable. I look forward to more insightful reads from you.
    visit: Natural Language Processing in Data Science: Text Analysis

    ReplyDelete