Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

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.

Object class in Java

•In java, “Object” is a class which is available in java.lang package.
•In java, “Object” is the implicit superclass of every other class including the user defined classes.
•So, object of every class will be able to use the methods in the class “Object”.

Methods in Object class:

•clone() – Used to create an exact clone of the existing object.
Syntax:
Object clone()

•equals() – Used to check whether two object references are equal or not.
Syntax:
boolean equals(Object object)



•finalize() – Invoked when an unused object is being garbage collected from memory.
Syntax:
protected void finalize()

•getClass() – Used to get the class of the object at runtime.
Syntax:
Class getClass()


•hashCode() – Returns the hash code associated with the object.
Syntax:
int hashCode()

•toString() – Returns the string representation of an object.
Syntax:
String toString()



•wait() – Waits on another thread of execution.
Syntax:
void wait()

•notify() – Used to resume the execution of a thread waiting on the invoking object.
Syntax:
void notify()

•notifyAll() – Used to resume the execution of all threads waiting on the invoking object.
Syntax:
void notifyAll()


•The following methods in the Object class are declared as “final”. Means these methods cannot be overriden.
1) getClass()
2) wait()
3) notify()
4) notifyAll()

Summary of methods in Object: