INHERITANCE
–As the name inheritance suggests an object is able to inherit characteristics from another object. In more concrete terms, an object is able to pass on its state and behaviors to its children. For inheritance to work the objects need to have characteristics in common with each other.
In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass.
● Superclass
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
● 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.
●Final Classes
– Classes that cannot be extended
– To declare final classes,
we write,
public final ClassName{
. . .
}
● 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.
●Final Classes
– Classes that cannot be extended
– To declare final classes,
we write,
public final ClassName{
. . .
}
● 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 subclass it belongs to.
● 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.
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:
Post a Comment