Java Programming 2: Inheritance, Polymorphism, Interface

Inheritance, Polymorphism, Interface

Thursday, April 22, 2010

 

Inheritance
In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass.
Supper Class
It is any class above a specific class in the class hierarchy.
Subclass
Any class below a specific class in the class hierarchy.
Benefits of Inheritance
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.
Sample Code
public class Person {
:
:
public String getName(){
System.out.println("Parent: getName");
return name;
}
}
Final Methods and Classes
Final Classes–Classes that cannot be extended–To declare final classes, we write,public final ClassName{. . . }
Other examples of final classes are wrapper classes and Strings.
public final class Person { . . . }
Final Methods–Methods that cannot be overridden–
To declare final methods, we write,
public final [returnType] [methodName]([parameters]){. . .}
Static methods are automatically final.
ExampleCode:
public final String getName(){
return name;
}
Polimorphism
It is 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 subclass it belongs to.
In Java, we can create a reference that is of type superclass to an object of its subclass.
ExampleCode:
public static main( String[] args ) {
Person ref;
Student studentObject = new Student();
Employee employeeObject = new Employee();
ref = studentObject; //Person reference points to a // Student object
}
Abstrac Classes
It is 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.
Abstract Methods
Are methods in the abstract classes that do not have implementation–To create an abstract method, just write the method declaration without the body and use the abstract keyword
Interface
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.

0 comments: