Mod7 8 Oop FT
Mod7 8 Oop FT
7.0 | INHERITANCE
• Inheritance
➢ Allows one class to inherit properties and methods from another class.
➢ The class that is being inherited from is called the parent class, while the class that is inheriting the
properties and methods is called the child class.
➢ Inheritance allows a software developer to derive a new class from an existing one
➢ The existing class is called the parent class, or superclass, or base class.
➢ The derived class is called the child class or subclass.
➢ As the name implies, the child inherits characteristics of the parent
➢ That is, the child class inherits the methods and data (attributes) defined for the parent class.
1. Base Class
➢ A class that is used as a starting point for creating derived classes.
➢ It is sometimes referred to as a superclass or parent class.
➢ A base class defines a set of common properties and methods that can be inherited by its derived classes.
2. Derived Class
➢ A class that is created by inheriting properties and methods from a base class.
➢ It is sometimes referred to as a subclass or child class.
➢ A derived class can add its own unique properties and methods to the ones it inherits from its base class.
1. Code Reuse
➢ Inheritance promotes code reuse by allowing child classes to inherit properties and methods from a parent class.
➢ This can save time and effort by reducing the need to write redundant code.
2. Hierarchical Relationships
➢ Inheritance allows for the creation of hierarchical relationships between classes.
➢ This can help to organize and structure code, making it easier to understand and maintain.
3. Polymorphism
➢ Inheritance is a key feature of polymorphism in OOP Polymorphism allows objects of different classes to be
treated as if they were of the same class, which can lead to more flexible and extensible code.
4. Easy Maintenance
➢ Inheritance can make code easier to maintain and update.
➢ If a change is made to a property or method in a parent class, the change will automatically be inherited by all
child classes that use that property or method.
5. Better Design
➢ Inheritance can help to create a better design for software systems by promoting modularity and encapsulation.
➢ This can make it easier to understand and modify code, and can lead to more robust and scalable software.
1
7.2 | ACCESSIBILITY OF BASE CLASS MEMBERS
ACCESS Accessible from own Accessible from derived Accessible from objects outside
SPECIFIER class class class
PUBLIC Yes Yes Yes
PROTECTED Yes Yes No
PRIVATE Yes No No
• “extend”
➢ keyword is used to create a subclass (also known as a child class) that inherits properties and methods from a
superclass (also known as a parent class).
➢ When a class extends another class, it inherits all the non-private properties and methods of the superclass.
This means that the subclass can access and use these properties and methods as if they were defined within the
subclass itself
➢ In Java, the extends keyword is used to create a subclass that inherits from a superclass
➢ This allows the subclass to inherit all of the members (fields and methods) of the superclass, and also to add
new members or override existing ones.
• In Java, the ‘super’ keyword is a reference variable that is used to refer to the immediate parent class of a subclass.
It is often used to call a method in the superclass that has been overridden by the subclass.
Here are some ways you can use the super keyword in Java:
2
3. To refer to a variable in the parent class
➢ If you have a variable with the same name
in both the parent class and the subclass,
you can use the super keyword to refer to
the variable in the parent class explicitly.
• Method Overloading
➢ is another feature of object oriented programming where multiple methods can have the same name, but with
different parameters. The compiler determines which version of the method to use based on the number, order,
and type of arguments passed in.
3
OOP – FT | MOD8: IMPLEMENTATION OF INHERITANCE
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
---------------------------------------------------------------------------------------------------------------------------------------------------
1) Single Inheritance
➢ This is the simplest form of inheritance, where a derived class inherits properties and methods from a single
base class.
2) Multiple Inheritance
➢ In this type of inheritance, a derived class can inherit properties and methods from multiple base classes.
➢ This can lead to complex class hierarchies and potential conflicts, but it can also be very useful in certain
situations.
3) Multilevel Inheritance
➢ In this type of inheritance, a derived class inherits properties and methods from a base class, which in turn
inherits from another base class.
➢ This creates a hierarchy of classes, with each class building on the properties and methods of its parent class
4
4) Hierarchical Inheritance
➢ In this type of inheritance, multiple derived classes inherit properties and methods from a single base class.
➢ Each derived class can add its own unique properties and methods to the ones it inherits from the base class.
5) Hybrid Inheritance
➢ This is a combination of multiple inheritance and hierarchical inheritance.
➢ In this type of inheritance, a class inherits properties and methods from multiple base classes, and those base
classes themselves inherit from a single base class.
5
6