Thursday, April 22, 2010

Inheritance, Polymorphism, Interface

Inheritance

☺In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass.
  • Superclass– Any class above a specific class in the class hierarchy.

  • Subclass– Any class below a specific class in the class hierarchy.

Benefits of Inheritance in OOP : Reusability - Once a behavior (method) is defined in a superclass, that behavior is automatically inherited by all subclasses. – Thus, you can encode a method only once and they can be used by all subclasses. – A subclass only needs to implement the differences between itself and the parent.

Overriding Methods - If for some reason a derived class needs to have a different implementation of a certain method from that of the superclass, overriding methods could prove to be very useful. A subclass can override a method defined in its superclass by providing a new implementation for that method.

To override, the getName method of the superclass Person, we write in the subclass Student,

public class Student extends Person{
:
:
public String getName(){
System.out.println("Student: getName");
return name;
}
:
}

Now, when we invoke the getName method of an object of the subclass Student, the overridden getName method would be called, and the output would be,

Student: getName

Final Classes
– Classes that cannot be extended

– To declare final classes, we write,

public final ClassName{
. . .
}

Example:
public final class Person {
. . .
}

Other examples of final classes are your wrapper classes and Strings.

Final Methods
– Methods that cannot be overridden
– To declare final methods, we write,

public final [returnType] [methodName]([parameters]){
. . .
}

Static methods are automatically final.

Polymorphism
– The ability of a reference variable to change behavior according to what object it is holding.
– This allows multiple objects of different subclasses to be treated as objects of a single superclass, while automatically selecting the proper methods to apply to a particular object based on the subclassit belongs to. To illustrate polymorphism, let us discuss an example.

☺Given the parent class Person and the subclass Student of the previous examples, we add another subclass of Person which is Employee.

Abstract class
– a class that cannot be instantiated.
– often appears at the top of an object-oriented programming class hierarchy, defining the broad types of actions possible with objects of all subclasses of the class.

An interface
– is a special kind of block containing method signatures (and possibly constants) only.
– defines the signatures of a set of methods, without the body.
– defines a standard and public way of specifying the behavior of classes.
– allows classes, regardless of their locations in the class hierarchy, to implement common behaviors.
– NOTE: interfaces exhibit polymorphism as well, since program may call an interface method, and the proper version of that method will be executed depending on the type of object passed to the interface method call.

No comments:

Post a Comment