Inheritance
Inheritance
SCUD COMP
Inheritance in Java is a mechanism in which one object acquires all
the properties and behaviors of a parent object. It is an important
part of OOPs (Object Oriented programming system).
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
current class also
Why use inheritance in java
•For Method Overriding (so runtime
polymorphism can be achieved).
•For Code Reusability
Terms used in Inheritance
•Class: A class is a group of objects which have common properties. It is a
template or blueprint from which objects are created.
•Sub Class/Child Class: Subclass is a class which inherits the other class. It
is also called a derived class, extended class, or child class.
•Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
•Reusability: As the name specifies, reusability is a mechanism which
facilitates you to reuse the fields and methods of the existing class when
you create a new class. You can use the same fields and methods already
defined in the previous class.
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.
Types of inheritance in java
there can be three types of inheritance in java: single, multilevel and hierarchical. Multiple
inheritance is not allowed in Java. It can be got by using Interface
Hierarchial
Single
Class z
Multilevel
1.class Animal{
2.void eat(){System.out.println("eating...");}
3.}
4.class Dog extends Animal{
5.void bark(){System.out.println("barking...");}
6.}
7.class BabyDog extends Dog{
8.void weep(){System.out.println("weeping...");}
9.}
10.class TestInheritance2{
11.public static void main(String args[]){
12.BabyDog d=new BabyDog();
13.d.weep();
14.d.bark();
15.d.eat();
16.}}
1.class Animal{
2.void eat(){System.out.println("eating...");}
3.}
4.class Dog extends Animal{
5.void bark(){System.out.println("barking...");}
6.}
7.class Cat extends Animal{
8.void meow()
{System.out.println("meowing...");}
9.}
10.class TestInheritance3{
11.public static void main(String args[]){
12.Cat c=new Cat();
13.c.meow();
14.c.eat();
15.//c.bark();//C.T.Error
16.}}
Why multiple inheritance is not supported in java?
1.class A{
2.void msg(){System.out.println("Hello");}
3.}
4.class B{
5.void msg(){System.out.println("Welcome");}
6.}
7.class C extends A,B{//suppose if it were
8.
9. public static void main(String args[]){
10. C obj=new C();
11. obj.msg();//
Now which msg() method would be invoked?
12.}
13.}
Points to remember:
Every class declared in java is direct or indirect subclass o a
class Object defined in java.lang package. That means even if
you declare a class which is not inheriting from any other ,
class, still your class has a superclass the class Object provided
by java.
// A simple interface
interface In1
{
// public, static and final
final int a = 10;
// Driver Code
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
Output:
10
class Bicycle implements Vehicle{
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// to decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed
+ " gear: " + gear);
}
}
class Bike implements Vehicle {
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// to decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed
+ " gear: " + gear);
}
}
class GFG {
public static void main (String[] args) {
// creating an inatance of Bicycle
// doing some operations
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
System.out.println("Bicycle present state :");
bicycle.printStates();
// creating instance of the bike.
Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);
System.out.println("Bike present state :");
bike.printStates();
}
}