Anda di halaman 1dari 2

6/8/2017 Object Oriented Programming through Java: Substitutability, Subtype and Subclass

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.

http://oopjavavit.blogspot.in/2012/03/substitutability­subtype­and­subclass.html 1/2
6/8/2017 Object Oriented Programming through Java: Substitutability, Subtype and Subclass

•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.

http://oopjavavit.blogspot.in/2012/03/substitutability­subtype­and­subclass.html 2/2

Anda mungkin juga menyukai