2. Inheritance is a mechanism in which one class
acquires the property of another class. For
example, a child inherits the traits of his/her
parents. With inheritance, we can reuse the
fields and methods of the existing class.
Hence, inheritance facilitates Reusability and
is an important concept of OOPs.
4. In Single Inheritance one
class extends another
class (one class only).
In Multiple Inheritance, one
class extending more than
one class. Java does not
support multiple inheritance.
5. In Multilevel Inheritance, one class
can inherit from a derived class.
Hence, the derived class becomes
the base class for the new class.
In Hierarchical Inheritance, one
class is inherited by many sub
classes. example, Class B, C,
and D inherit the same class A.
6. Hybrid inheritance is a combination of Single and Multiple
inheritance. All the public and protected members of Class A are
inherited into Class D, first via Class B and secondly via Class C.
7. JAVA INHERITANCE is a mechanism in which one
class acquires the property of another class. In
Java, when an "Is-A" relationship exists between
two classes, we use Inheritance. The parent class
is called a super class and the inherited class is
called a subclass. The keyword extends is used
by the sub class to inherit the features of super
class.
Java doesn't support hybrid/Multiple inheritance.
Inheritance is important since it leads to the
reusability of code.
8. class Doctor
{
void Doctor_Details()
{
System.out.println("Doctor Details...");
}
}
class Surgeon extends Doctor // class Surgeon is a child class of class Doctor
{
void Surgeon_Details()
{
System.out.println("Surgen Detail...");
}
}
public class Hospital
{
Public static void main(String args[])
{
Surgeon s = new Surgeon();
s.Doctor_Details();
s.Surgeon_Details();
}
}
As we know, Surgeon IS A Doctor
9. The super keyword is similar to "this"
keyword. (Keyword THIS is a reference
variable in Java that refers to the current
object.)
The keyword super can be used to access any
data member or methods of the parent class.
Super keyword can be used at variable,
method and constructor level.
Syntax:
super.<method-name>();
10. Reusability - facility to use public methods of
base class without rewriting the same.
Extensibility - extending the base class logic as
per business logic of the derived class.
Data hiding - base class can decide to keep some
data private so that it cannot be altered by the
derived class
Overriding -With inheritance, we will be able to
override the methods of the base class so that
meaningful implementation of the base class
method can be designed in the derived class.
11. If a method is deleted in the "base class" or
aggregate, then we will have to re-factor in case
of using that method.Here things can get a bit
complicated in case of inheritance because our
programs will still compile, but the methods of
the subclass will no longer be overriding base
class methods. These methods will become
independent methods in their own right.
Main disadvantage of using inheritance is that
the two classes (base and inherited class) get
tightly coupled. This means one cannot be used
independent of each other.