0% found this document useful (0 votes)
7 views24 pages

CS4001NI WK09 T Inheritance and Its Types 82790 170967

This document covers the concept of inheritance in Java, explaining its definition, types, and the use of the super keyword. It details how inheritance allows for code reuse and defines key terminologies such as superclass and subclass. Additionally, it provides examples of single, multilevel, and hierarchical inheritance, as well as method overriding.

Uploaded by

irshadrayn2414
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)
7 views24 pages

CS4001NI WK09 T Inheritance and Its Types 82790 170967

This document covers the concept of inheritance in Java, explaining its definition, types, and the use of the super keyword. It details how inheritance allows for code reuse and defines key terminologies such as superclass and subclass. Additionally, it provides examples of single, multilevel, and hierarchical inheritance, as well as method overriding.

Uploaded by

irshadrayn2414
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
You are on page 1/ 24

Week – 9

Inheritance in java
CONCEPTS COVERED THIS WEEK

• Inheritance
• Types of Inheritance
• Super keyword
• Method Overriding
What is inheritance?
• A mechanism in which one class inherits the properties of another class

• Create a new class by extending the functionality of existing classes

• Inheritance defines an is-a relationship between the classes. For eg: square is-a

shape, employee is-a person

• Java allows us to define what can and cannot be inherited


Super class / Parent Class Sub class / Child Class

Methods and properties of A* Methods and properties of A


Derived from

Methods and properties of B

Class A Class B

* conditions apply
Inherited properties
Super class Sub class
String name;
String name; int length, breadth;

void displayName(); void displayName();


void area(); void area();
void perimeter();

Inherited methods
Class Shape Class Rectangle
Inheritance for code reuse
• Inheritance can be used to reuse code and avoid redundancy.
we can use the same fields and methods already
defined in the parent class
Rectangle Square Circle

String name String name String name

int area() int area() int area()


void displayName() void displayName() void displayName()

Duplicate Code
Shape
String name

int area()
void displayName()

Code put in parent class to avoid


redundancy

Rectangle Square Circle


Inheritance Terminologies
Terms Definition

Subclass [Child Class / Derived class] Class that inherits properties and methods

Superclass [Parent Class / Base class] Class whose properties and methods are inherited

extends Java specific keywords for inheritance

public, private, protected Access modifiers


Types of Inheritance in Java
Single Inheritance Example:
class Bird extends Aves
{
public void eat()
class Aves {
{ System.out.println("Eats to live");
public void nature() }
{ public static void main(String args[])
System.out.println("Generally, Aves fly"); {
} Bird b1 = new Bird ();
} b1.eat(); // calling its own method
b1.nature(); // calling super class method

}
}
Multilevel Inheritance Example:

class Aves class Bird extends Aves


{ {
public void nature() public void eat()
{
{
System.out.println("Generally, Aves fly");
} System.out.println("Eats to live");
} }
}
public class Parrot extends Bird
{
public void food()
{
System.out.println("Parrot eats seeds and fruits");
}
public static void main(String args[])
{
Parrot p1 = new Parrot();
p1.food(); // calling its own
p1.eat(); // calling super class Bird method
p1.nature(); // calling super class Aves method
}
}
Hierachical Inheritance Example:
public class ClassC extends ClassA
{
public class ClassA public void dispC()
{ {
public void dispA() System.out.println("disp() method of ClassC");
{ }
System.out.println("disp() method of ClassA"); }
}
}
public class ClassB extends ClassA public class ClassD extends ClassA
{ {
public void dispB() public void dispD()
{ {
System.out.println("disp() method of ClassB"); System.out.println("disp() method of ClassD");
} }
} }
public class HierarchicalInheritanceTest
{ //Assigning ClassD object to ClassD reference
public static void main(String args[]) ClassD d = new ClassD();
{ //call dispD() method of ClassD
//Assigning ClassB object to ClassB reference d.dispD();
ClassB b = new ClassB(); //call dispA() method of ClassA
//call dispB() method of ClassB d.dispA();
b.dispB(); }
//call dispA() method of ClassA }
b.dispA();

//Assigning ClassC object to ClassC reference Output:


ClassC c = new ClassC(); disp() method of ClassB
//call dispC() method of ClassC
disp() method of ClassA
c.dispC();
//call dispA() method of ClassA disp() method of ClassC
c.dispA(); disp() method of ClassA
disp() method of ClassD
disp() method of ClassA
Example:

class Calculation {
int z;

public void addition(int x, int y) {


z = x + y;
System.out.println("The sum of the given numbers:"+z);
}

public void Subtraction(int x, int y) {


z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}

public static void main(String args[]) {


int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
What is “super” keyword in java?
super keyword in java:
-It is a reference variable which is used to refer immediate parent class
object.
-Whenever you create the instance of subclass, an instance of parent class is
created implicitly which is referred by super reference variable.
Usage of java super Keyword
-super can be used to refer immediate parent class instance variable.
-super can be used to invoke immediate parent class method.
-super() can be used to invoke immediate parent class constructor.
Example of Super keyword:
class Dog extends Animal {
class Animal {
public void move() {
public void move() {
super.move(); // invokes the super class method
System.out.println("Animals can move");
System.out.println("Dogs can walk and run");
}
}
}
}

public class TestDog {

public static void main(String args[]) {


Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
} Output:
} Animals can move
Dogs can walk and run
Method Overriding
• If subclass (child class) has the same method as declared in the
parent class, it is known as method overriding in Java.

• In other words, If a subclass provides the specific


implementation of the method that has been declared by one of
its parent class, it is known as method overriding
Example of Method Overriding:
class Square extends Shape {
class Shape {
@Override
public void dispaly() {
public void display() {
System.out.println(“Display Shape");
System.out.println(“Display Square");
}
}
}
}

public class MainClass {

public static void main(String args[]) {


Square mySquare = new Square();
mySquare.display(); // runs the method in Square class
} Output:
} Display square
Thank You!

You might also like