SlideShare a Scribd company logo
Effect of Reuse on
Using Inheritance
Inheritance
 Inheritance is the mechanism of deriving a new class (ca
lled derived class) from an old one (called base class).
 Inheritance is a powerful code reuse mechanism where
a derived class inherits all/some of the description of th
e base class.
 A class whose properties are being acquired by some
other class or a class that is inherited is called the super
class or base class or parent class.
 The class that acquires the properties of some other
class or the class that does the inheriting is called the
subclass or derived class or child class.
 A derived class inherits all/some of the properties from
base class.
 A derived class can also inherit properties from more
than one class / from more than one level.
Inheritance
 Reusability: Inheritance is a powerful code reuse mecha
nism where a derived class inherits all/some of the
properties of the base class. That is, one can derive a
new class (sub-class) from an existing class and add new
features to it without modifying its parent class. There
is no need to rewrite the parent class in order to inherit
it.
 Transitive nature: If class A inherits properties from
another class B, then all subclasses of A will
automatically inherit properties from B. This property is
called the transitive nature of inheritance.
 Note: A subclass defines only those features that are
unique to it.
Inheritance
 Example, the class Student inherits from the
class Person. Then although Student is a person, the
reverse is not true. A Person need not be a Student. The
class Student has properties that it does not share with
class Person.
 For instance, the Student has a marks-percentage, but
the Person does not have.
Inheritance using class hierarchies
Vehicles
Land Vehicle Water Vehicle
Base Class
Car Rickshaw
Ship
Boat
Derived Class
is a subtype of
Teacher
School
Student
Universi
ty
Student
School
Teacher
Universit
y
Teacher
Student
Inheritance using class hierarchies
Person
what are the properties that can be acquired
by a subclass?
Types of Inheritance
Types of Inheritance
Types of Inheritance
Types of Inheritance
Syntax
class superclassName
{
. . .
}
class subclassName extends superclassName
{
. . .
}
Relationship
Single Inheritance - Example
//Base class
class Person {
String name = "John";
int age =17;
String city = "Delhi";
public void show() {
System.out.println("Student inheriting properties from Person:n");
}
}
//child class
class Student extends Person {
// defining additional properties to child class
String Course = "Java";
int marks = 78;
}
Single Inheritance - Example
class Main{
public static void main(String args[]) {
Student obj = new Student();
obj.show();
System.out.println("Name of the student is: " + obj.name);
System.out.println("Age of the student is: " + obj.age);
System.out.println("Student lives in: " + obj.city);
System.out.println("Student learns from: " + obj.tutorial);
System.out.println("Marks obtained by the student is: " + obj.marks);
}
}
Multilevel Inheritance - Example
class Person {
public void show() {
System.out.println("Person
class");
}
}
class Student extends Person {
public void show1() {
System.out.println("I am a
Student inheriting the
properties of Person");
}
}
class EngineeringStudent extends Student {
public void show2(){
System.out.println("Engineering Student
inheriting properties from Student");
}
}
public class MultilevelDemo {
public static void main(String args[]) {
EngineeringStudent obj = new
EngineeringStudent();
obj.show();
obj.show1();
obj.show2();
}
}
Hierarchical Inheritance - Example
class Person
{
public void show()
{
System.out.println("I am a
Person");
}
}
//child class1
class Student extends Person
{
public void show1()
{
System.out.println("I am a
Student who is Person ");
}
}
//child class2
class Doctor extends Person{
public void show2() {
System.out.println("I am a Doctor who is a
Person");
}
}
public class HierarchicalInheritance{
public static void main(String args[]){
Student student = new Student();
Doctor doctor = new Doctor();
student.show();
student.show1();
doctor.show2();
}
}
Multiple Inheritance – Example
(Through Interface)

More Related Content

PPTX
Inheritance
PDF
Inheritance used in java
PDF
lecture 6.pdf
PDF
java inheritance that is used in oop cls
PDF
Inheritance in Java.pdf
PPTX
Inheritance,single,multiple.access rulepptx
PPTX
Inheritance in java
DOCX
oop database doc for studevsgdy fdsyn hdf
Inheritance
Inheritance used in java
lecture 6.pdf
java inheritance that is used in oop cls
Inheritance in Java.pdf
Inheritance,single,multiple.access rulepptx
Inheritance in java
oop database doc for studevsgdy fdsyn hdf

Similar to Module 4 Effect of Reuse on using Inheritance.ppt (20)

PPTX
Inheritance
PPTX
Inheritance in C++ (Programming Fundamentals)
PPT
Inheritance
PPTX
Inheritance
PPTX
Programming Lesson by Slidesgo.pptx
PDF
Chapter25 inheritance-i
PPTX
Inheritance ppt
PPTX
Inheritance
PPT
E -COMMERCE.ppt
PPTX
Inheritance
PDF
Inheritance
PPTX
Inheritance in java
PPT
Inheritance OOP Concept in C++.
PPTX
Java Inheritance | Java Course
PPTX
Inheritance in oops
PPTX
Inheritance.pptx
PPTX
Inheritance
PPTX
Inheritance in c++
PPTX
Inheritance in java
PPTX
Inheritance
Inheritance in C++ (Programming Fundamentals)
Inheritance
Inheritance
Programming Lesson by Slidesgo.pptx
Chapter25 inheritance-i
Inheritance ppt
Inheritance
E -COMMERCE.ppt
Inheritance
Inheritance
Inheritance in java
Inheritance OOP Concept in C++.
Java Inheritance | Java Course
Inheritance in oops
Inheritance.pptx
Inheritance
Inheritance in c++
Inheritance in java
Ad

Recently uploaded (20)

PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
Simulation of electric circuit laws using tinkercad.pptx
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
OOP with Java - Java Introduction (Basics)
Embodied AI: Ushering in the Next Era of Intelligent Systems
anatomy of limbus and anterior chamber .pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Lesson 3_Tessellation.pptx finite Mathematics
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
573137875-Attendance-Management-System-original
Simulation of electric circuit laws using tinkercad.pptx
Ad

Module 4 Effect of Reuse on using Inheritance.ppt

  • 1. Effect of Reuse on Using Inheritance
  • 2. Inheritance  Inheritance is the mechanism of deriving a new class (ca lled derived class) from an old one (called base class).  Inheritance is a powerful code reuse mechanism where a derived class inherits all/some of the description of th e base class.  A class whose properties are being acquired by some other class or a class that is inherited is called the super class or base class or parent class.  The class that acquires the properties of some other class or the class that does the inheriting is called the subclass or derived class or child class.  A derived class inherits all/some of the properties from base class.  A derived class can also inherit properties from more than one class / from more than one level.
  • 3. Inheritance  Reusability: Inheritance is a powerful code reuse mecha nism where a derived class inherits all/some of the properties of the base class. That is, one can derive a new class (sub-class) from an existing class and add new features to it without modifying its parent class. There is no need to rewrite the parent class in order to inherit it.  Transitive nature: If class A inherits properties from another class B, then all subclasses of A will automatically inherit properties from B. This property is called the transitive nature of inheritance.  Note: A subclass defines only those features that are unique to it.
  • 4. Inheritance  Example, the class Student inherits from the class Person. Then although Student is a person, the reverse is not true. A Person need not be a Student. The class Student has properties that it does not share with class Person.  For instance, the Student has a marks-percentage, but the Person does not have.
  • 5. Inheritance using class hierarchies Vehicles Land Vehicle Water Vehicle Base Class Car Rickshaw Ship Boat Derived Class is a subtype of
  • 7. what are the properties that can be acquired by a subclass?
  • 12. Syntax class superclassName { . . . } class subclassName extends superclassName { . . . }
  • 14. Single Inheritance - Example //Base class class Person { String name = "John"; int age =17; String city = "Delhi"; public void show() { System.out.println("Student inheriting properties from Person:n"); } } //child class class Student extends Person { // defining additional properties to child class String Course = "Java"; int marks = 78; }
  • 15. Single Inheritance - Example class Main{ public static void main(String args[]) { Student obj = new Student(); obj.show(); System.out.println("Name of the student is: " + obj.name); System.out.println("Age of the student is: " + obj.age); System.out.println("Student lives in: " + obj.city); System.out.println("Student learns from: " + obj.tutorial); System.out.println("Marks obtained by the student is: " + obj.marks); } }
  • 16. Multilevel Inheritance - Example class Person { public void show() { System.out.println("Person class"); } } class Student extends Person { public void show1() { System.out.println("I am a Student inheriting the properties of Person"); } } class EngineeringStudent extends Student { public void show2(){ System.out.println("Engineering Student inheriting properties from Student"); } } public class MultilevelDemo { public static void main(String args[]) { EngineeringStudent obj = new EngineeringStudent(); obj.show(); obj.show1(); obj.show2(); } }
  • 17. Hierarchical Inheritance - Example class Person { public void show() { System.out.println("I am a Person"); } } //child class1 class Student extends Person { public void show1() { System.out.println("I am a Student who is Person "); } } //child class2 class Doctor extends Person{ public void show2() { System.out.println("I am a Doctor who is a Person"); } } public class HierarchicalInheritance{ public static void main(String args[]){ Student student = new Student(); Doctor doctor = new Doctor(); student.show(); student.show1(); doctor.show2(); } }
  • 18. Multiple Inheritance – Example (Through Interface)