Professional Documents
Culture Documents
for
all the remaining predefined or userdefined classes
clone(), eqals(),toString().......
2) Polymorphism
3) Abstraction
4) Encapsulation
The idea behind inheritance in Java is that you can create new classes that are
built upon existing classes.
When you inherit from an existing class, you can reuse methods and fields of the
parent class.
Moreover, you can add new methods and fields in your child class also.
Advantages of Inheritance:
-------------------------
1)code reusability
2)Method overriding
syntax
------
class Subclass-name extends Superclass-name
{
code
}
-->extends keyword indicates that you are making a new class that derives from an
existing class.
-------
Types of inheritance:
--------------------
1)single inheritance
2)Hierarchial inheritance
3)Multilevel inheritance
4)Hybrid inheritance
syntax
------
class A----------->parent class(A)
{
code--->properties,methods
}
Hierarchial inheritance: -----> same parent class can extend by many child classes
-----------------------
syntax
------
class A----------->parent class(A)
{
code
}
Multilevel inheritance:
----------------------
Deriving a new class from another derived class.
There must be an intermediate class which acts as both parent and child.
syntax:
------
class B extends A ----> child class(B)-----> Here class B acts as parent for
class C.
{ In the same way it acts as child
to class A .
So class B is called as
intermediate class.
syntax:
-------
Multiple Inheritance:
--------------------
Java does not supports this type of inheritance. Because ambiguity occurs.
syntax:
-------
class A
{
public void m1()
{
System.out.println("Helloworld");
}
}
class B
{