This document discusses inheritance in object-oriented programming using Java. Inheritance allows a class to inherit properties and methods from a parent class, facilitating code reuse. It provides an example where the Person class is a parent class of Student and Professor subclasses. Both subclasses inherit attributes and methods from Person but can also have their own. The document outlines key inheritance terms and concepts in Java like extends keyword, access modifiers, and provides an example program demonstrating inheritance. Advantages include code reuse and flexibility, while disadvantages are tight coupling and performance impacts.
2. Objectives
To understand what inheritance is.
To be able to apply and use it in an already known programming
language.
3. Inheritance
Inheritance is a mechanism in which one class acquires the
property of another class.
It allows to derive a class from another class for a hierarchy of
classes that share a set of attributes and methods.
With inheritance, we can reuse the fields and methods of the
existing class.
Hence, inheritance facilitates Reusability and is an important
concept of OOPs
4. A diagram as an example
As displayed in the diagram, Person
is the superclass and Student and
Professor are the subclasses.
Both subclasses inherit the properties
and methods of the superclass
Both subclasses can also have their
own properties and methods
5. Java as a choice
Java was chosen to
demonstrate the use of
inheritance.
It remains a powerful and
very useful language
6. Terms used in Inheritance
Class: A class is a group of objects which have common properties. It is a template
or blueprint from which objects are created.
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.
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.
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.
7. Java keywords and access modifiers
extends : indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
private attributes or methods can only be accessed within the same class.
protected attributes or methods can be accessed within the same class,
by all classes within the same package, and by all subclasses.
public attributes and methods can be accessed by all classes.
8. Java example
class Person
{
String Name;
String PhoneNumber;
}
public class Professor extends Person
{
int salary=10000;
public static void main(String args[])
{
Professor p=new Professor();
p.Name = "Shara";
System.out.println("Professor salary is:"+p.salary);
System.out.println("Professor's name:"+p.Name);
}
}
9. Advantages of Inheritance
Code reusability
Avoiding code duplication
Improving code flexibility and extensibility
Provide better code structure and management
Help to achieve run the polymorphism
Avoid possible code errors
Preserves the integrity of the superclass
Data hiding
10. Disadvantages of Inheritance
Tight coupling
Slow performance of inherited methods
Extra maintenance effort on code change