Inheritance,Polymorphism,Interface

*Inheritance

-When a Student object is instantiated, the default
constructor of its superclass is invoked implicitly to do the
necessary initializations.
- After that, the statements inside the subclass's constructor
are executed.

*Superclass

-Any class above a specific class in the class hierarchy.
-A super constructor call in the constructor of a subclass will
result in the execution of relevant constructor from the
superclass, based on the arguments passed.

*Subclass

-Any class below a specific class in the class hierarchy.
A subclass can also explicitly call a constructor of its
immediate superclass.

●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

●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 Methods and classes

– 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.
-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.
- When a class extends the LivingThing abstract class, it is
required to override the abstract method walk(), or else, that
subclass will also become an abstract class, and therefore
cannot be instantiated.
- abstract methods
– 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

– 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.
- To reveal an object's programming interface without
revealing its class
- To model multiple inheritance which allows a class to have
more than one superclass
0 Responses