Unit - 2
Unit - 2
Inheritance
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 parent class, and you can add
new methods and fields also.
Inheritance represents the IS-A relationship, also
known as parent-child relationship.
Why use inheritance in java
For Method Overriding (so runtime polymorphism can
be achieved).
For Code Reusability.
Syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are
making a new class that derives from an existing
class. The meaning of "extends" is to increase the
functionality.
In the terminology of Java, a class which is
inherited is called parent or super class and the
new class is called child or subclass.
Types of inheritance in java
class kk
{
public static void main(String a[])
{
b b1 = new b();
b1.show2();
}
}
How the compiler
takes the interface
The java compiler adds public and abstract
keywords before the interface method.
More, it adds public, static and final
keywords before data members (Very
important) (Example program interface1.java)