Object-Oriented Programming (CS F213) : BITS Pilani

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Object-Oriented Programming (CS F213)

Module III: Inheritance and Polymorphism in Java


CS F213 RL 9.1: Inheritance Basics-I

BITS Pilani Dr. Pankaj Vyas


Department of Computer Science, BITS-Pilani, Pilani Campus
CS F213 RL 9.1 : Topics

Inheritance in Java

2 Object-Oriented Programming (CS F213)


Inheritance Basics

Reusability is achieved by Inheritance


Java classes can be Reused by Extending a class. Extending an
existing class is nothing but reusing properties of the existing
classes.
The class whose properties are extended is known as super or base
or parent class.
The class which extends the properties of super class is known as
sub or derived or child class
A class can either extends another class or can implement an
interface

3 Object-Oriented Programming (CS F213)


Javas Support For Inheritance
By Extending a Class
Syntax
class class-name extends super-class-name
{
..
}
Example
class A
{

}// End of class A A <<super-class>>
class B extends A
{
..
}// End of class B
B <<sub-class>>

4 Object-Oriented Programming (CS F213)


Javas Support For Inheritance
By Implementing an Interface
Syntax
class class-name implements interface-name
{
..
}
Example
interface A
{

}// End of interface A A <<interface>>
class B implements A
{
..
}// End of class B
B <<sub-class>>

5 Object-Oriented Programming (CS F213)


Forms of Inheritance
Single Inheritance Hierarchical Inheritance

A A X X

B B A B C A B C
Multiple Class Inheritance Multiple Interface Implementation
NOT SUPPORTED BY JAVA SUPPORTED BY JAVA
Multi-Level Inheritance Multiple Inheritance

A A
A B A B

B B

C C C C

6 Object-Oriented Programming (CS F213)


Multiple Inheritance in Java

Java Does Not Support Multiple Inheritance by Extending


Multiple Classes

The Following Code is Wrong

class A { } // End of class A


class B { } // End of class B
class C extends A, B
Wrong Java Does not
{ Allow Multiple Class
}// End of class C Extensions

7 Object-Oriented Programming (CS F213)


Multiple Inheritance in Java .

Java Supports Multiple Inheritance by Implementing Multiple


Interfaces

The Following Code is Correct

interface A { } // End of interface A


interface B { } // End of interface B

class C implements A, B Java Allows Multiple


{ Interface
}// End of class C Implementations

8 Object-Oriented Programming (CS F213)


Thank You

9 Object-Oriented Programming (CS F213)

You might also like