0% found this document useful (0 votes)
2 views13 pages

Section3 Inheritance

Inheritance in Java allows one class to acquire properties and behaviors from another, promoting code reusability and method overriding. It establishes an IS-A relationship between classes, with subclasses inheriting from superclasses, and supports various types of inheritance such as single, multilevel, and hierarchical. Additionally, Java uses interfaces to achieve multiple inheritance, allowing classes to implement methods without sharing code.

Uploaded by

m80433899
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Section3 Inheritance

Inheritance in Java allows one class to acquire properties and behaviors from another, promoting code reusability and method overriding. It establishes an IS-A relationship between classes, with subclasses inheriting from superclasses, and supports various types of inheritance such as single, multilevel, and hierarchical. Additionally, Java uses interfaces to achieve multiple inheritance, allowing classes to implement methods without sharing code.

Uploaded by

m80433899
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Section 3

Inheritance in Java
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.

Inheritance represents the IS-A relationship which is also known as a parent-


child relationship.

Why use inheritance in java

o For Method Overriding (so runtime polymorphism can be achieved).


o For Code Reusability.

Terms used in Inheritance

o Class: A class is a group of objects which have common properties. It is a template


or blueprint from which objects are created.
o 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.
o 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.
o 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 syntax of Java Inheritance

1. class Subclass-name extends Superclass-name


2. {
3. //methods and fields
4. }

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 a parent or superclass, and
the new class is called child or subclass.

Java Inheritance Example

As displayed in the above figure, Programmer is the subclass and Employee is the
superclass. The relationship between the two classes is Programmer IS-A Employee. It
means that Programmer is a type of Employee.
Ex_1:

1. class Employee{
2. float salary=40000;
3. }
4. class Programmer extends Employee{
5. int bonus=10000;
6. public static void main(String args[]){
7. Programmer p=new Programmer();
8. [Link]("Programmer salary is:"+[Link]);
9. [Link]("Bonus of Programmer is:"+[Link]);
10. }
11. }
Test it Now
Programmer salary is:40000.0
Bonus of programmer is:10000

Types of inheritance in java


On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only.
We will learn about interfaces later.
Note: Multiple inheritance is not supported in Java through class.

When one class inherits multiple classes, it is known as multiple inheritance. For Example:
Ex2:Single Inheritance Example
When a class inherits another class, it is known as a single inheritance. In the example
given below, Dog class inherits the Animal class, so there is the single inheritance.

File: [Link]

1. class Animal{
2. void eat(){[Link]("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){[Link]("barking...");}
6. }
7. class TestInheritance{
8. public static void main(String args[]){
9. Dog d=new Dog();
10. [Link]();
11. [Link]();
12. }}

Output:

barking...
eating...
EX3:Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel inheritance. As you can see
in the example given below, BabyDog class inherits the Dog class which again inherits the
Animal class, so there is a multilevel inheritance.

File: [Link]

1. class Animal{
2. void eat(){[Link]("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){[Link]("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){[Link]("weeping...");}
9. }
10. class TestInheritance2{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. [Link]();
14. [Link]();
15. [Link]();
16. }}

Output:

weeping...
barking...
eating...
Method Overriding in Java Inheritance
we see the object of the subclass can access the method of the superclass.
However, if the same method is present in both the superclass and
subclass, what will happen?
In this case, the method in the subclass overrides the method in the
superclass. This concept is known as method overriding in Java.

Example 4: Method overriding in Java Inheritance


class Animal {

// method in the superclass


public void eat() {
[Link]("I can eat");
}
}

// Dog inherits Animal


class Dog extends Animal {

// overriding the eat() method


@Override
public void eat() {
[Link]("I eat dog food");
}

// new method in subclass


public void bark() {
[Link]("I can bark");
}
}

class Main {
public static void main(String[] args) {

// create an object of the subclass


Dog labrador = new Dog();

// call the eat() method


[Link]();
[Link]();
}
}
Run Code

Output

I eat dog food


I can bark

In the above example, the eat() method is present in both the


superclass Animal and the subclass Dog .

Here, we have created an object labrador of Dog .

Now when we call eat() using the object labrador , the method inside Dog is
called. This is because the method inside the derived class overrides the
method inside the base class.
This is called method overriding. To learn more, visit Java Method Overriding.

super Keyword in Java Inheritance


Previously we saw that the same method in the subclass overrides the
method in superclass.

In such a situation, the super keyword is used to call the method of the parent
class from the method of the child class.
Example 5: super Keyword in Inheritance
class Animal {

// method in the superclass


public void eat() {
[Link]("I can eat");
}
}

// Dog inherits Animal


class Dog extends Animal {

// overriding the eat() method


@Override
public void eat() {

// call method of superclass


[Link]();
[Link]("I eat dog food");
}

// new method in subclass


public void bark() {
[Link]("I can bark");
}
}

class Main {
public static void main(String[] args) {

// create an object of the subclass


Dog labrador = new Dog();

// call the eat() method


[Link]();
[Link]();
}
}
Run Code

Output

I can eat
I eat dog food
I can bark

In the above example, the eat() method is present in both the base
class Animal and the derived class Dog . Notice the statement,

[Link]();
Here, the super keyword is used to call the eat() method present in the
superclass.
We can also use the super keyword to call the constructor of the superclass
from the constructor of the subclass. To learn more, visit Java super keyword.

////////////////////

EX6:child class can also replace ("override") behavior from parent.

// [Link]
// Parent class Shape
public class Shape {
public double getArea() {
return 0.0;}}
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
[Link] = length;
[Link] = width;}
@Override
public double getArea() {
return length * width; }}

public class Main {


public static void main(String[] args) {
Rectangle rectangle = new Rectangle(3.0, 10.0);
double area = [Link]();
[Link]("The area of the rectangle is: " + area) }}
out put:
30.0
Interfaces

• interface: A list of methods that a class can promise to implement.


▪ Inheritance gives you an is-a relationship and code sharing.
• A Lawyer can be treated as an Employee and inherits its code.
( as before - use extends)
▪ Interfaces give you an is-a relationship without code sharing.
• A Rectangle object can be treated as a Shape but inherits no code.
(as next) - use implements)

Interface syntax
public interface name {
public type name(type name, ..., type name);
public type name(type name, ..., type name);
...
public type name(type name, ..., type name);
}

Example:
public interface Shape {
public double area();
public double perimeter();
}
Implementing an interface
public class name implements interface {
...
}
• A class can declare that it "implements" an interface.
▪ The class promises to contain each method in that interface.
(Otherwise it will fail to compile.)
▪ Example:
public class Rectangle implements Shape {
...
public double area() { ... }
public double perimeter() { ... }
}
Example 6:
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){[Link]("drawing
rectangle");}
}
class Circle implements Drawable{
public void draw(){[Link]("drawing
circle");}
}
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();
//In real scenario, object is provided by method [Link]()
[Link]();
}}
Out put :

drawing circle

You might also like