Topic 3 Part 3.1
Topic 3 Part 3.1
Presentation 1
Revision
1. Define Function.
2. Write the syntax and example for defining a function.
Objectives
At the end of this presentation, you will be able to : Define Inheritance. List the advantages of Inheritance. Discuss the types of Inheritance. Create Subclasses.
Advantages of Inheritance
The advantages of Inheritance are:
1. The variables and methods that are common to two or more classes can be defined in one class and used in other classes. This is called as code reusability. 2. When there is any modification in the common information, it will apply to all the inherited classes. 3. Inheritance avoids code redundancy.
Types of Inheritance
There are 3 types of Inheritance. They are:
1. Single Inheritance 2. Multiple Inheritance and 3. Multi-Level Inheritance
Single Inheritance
When a class is inherited from only one existing class, then it is Single inheritance.
The inherited class is a sub class and the existing class from which a sub class is created is the super class. In single inheritance a super class can have any number of sub classes but a sub class can have only one super class.
Single Inheritance
Class A
Super class
Sub class
Class B
class B extends A
{ ------------}
-------------
Single Inheritance
Class A
Class B
Class C
Class D
Multiple Inheritance
When a class is inherited from more than one existing class, then it is Multiple inheritance.
The inherited class is a sub class and all the existing classes from which the sub class is created are super classes. Java supports Multiple Inheritance through a concept called Interface.
Multiple Inheritance
Class A Class B
Class C
Multi-Level Inheritance
Multi-Level Inheritance is the extension of Single Inheritance.
When a class is inherited from another sub class, then it is Multi-Level Inheritance. The sub class at the lowest level can access the member variables and methods of all the super classes at the higher level.
Multi-Level Inheritance
Class A
Super class
Class B
Sub class for Class A
Class C
Sub class
class B extends A
{ ------------}
class C extends B
{ ------------}
-------------
Super Class
A new class can be inherited from an existing class.
The existing class from which a new class is created is known as the Super class. It is also called as Base class.
SubClass
The new class that is inherited from an existing class is known as Subclass.
This class is also called as Derived class. The subclass inherits all the accessible methods and member variables of the super class.
Creating SubClass
Syntax
Example
class Cylinder extends Sphere
{
}
An object of the sub class can access the variables and methods of the super class.
Object.variable Object.method()
Hands-On!
Program Shape.java illustrates how an object of a subclass can access the variables and methods of a super class.
super.variable_name super.method_name
Hands-On!
Program Sup_vol.java illustrates how a subclass can access a super class variables and methods.
Activity 6.8.1
Detect the errors in the following program:
// Program to display student details using inheritance class Person { String name; int age; }
Lab Exercise
1. Write a program to define a class Marks with data members Mark1, Mark2 and Mark3. Create a sub class Total of Marks class that has a method to find the total of the 3 marks
2. Write a program to define a class Box with data members width, depth and height. Create a sub class Volume of Box class that has a method to find the volume of the box.
Summary
In this presentation, you learnt the following Inheritance avoids code redundancy because the extended classes need not have the code that is shared with other classes. There are three types of Inheritance are Single Inheritance, Multiple Inheritance and Multi-Level Inheritance. When a class is inherited from only one existing class, then it is single inheritance.
Summary
In this presentation, you learnt the following When a class is inherited from more than one existing class, then it is Multiple inheritance. When a class is inherited from another sub class, then it is Multi-Level Inheritance.
Whe existing class from which a new class is created is known as the Super class.
Summary
In this presentation, you learnt the following The new class that is inherited from an existing class is known as Sub class.
Assignment
1. Define Inheritance.
2. List the advantages of Inheritance. 3. Write the syntax to create a subclass. 4. Write the syntax for an object of a subclass to access the variables, methods of a super class.