Inheritance

Object-oriented Programming Features

  • Focus: Inheritance

Inheritance

  • Allows child classes to inherit attributes and operations from parent classes.

    • Attributes: Fields and properties inherited from the parent class.

    • Operations: Methods inherited and/or redefined from the parent class.

  • Aims for code reusability, enabling common properties/functions to be extended rather than rewritten.

Types of Inheritance

  • Terminology:

    • Base class / Derived class: Parent-child relationship in class structure.

    • Interfaces: Derived interfaces implement base interfaces.

Benefits of Inheritance

  • Extensibility and reusability of code.

  • Provides abstraction.

  • Eliminates redundant code.

  • Promotes "is-a" relationships:

    • Example: Dog is-an Animal.

  • Avoids "has-a" relationships:

    • Example: Dog has-a Name (not a true inheritance relationship).

Inheritance Examples

  • Class Structure:

    • Employee class with fields: Name, Address, Company.

    • Student class with fields: Name, Address, School, Salary.

    • Derived classes share common properties and extend functionality.

Types of Inheritance in Java

  • Single Inheritance: One parent, one child.

  • Multiple Inheritance: Not supported directly in Java but can be achieved through interfaces.

  • Multilevel Inheritance: Child classes derive from other child classes.

  • Hierarchical Inheritance: Multiple subclasses from the same parent.

Class Hierarchies

  • General characteristics defined at higher levels, specific characteristics at lower levels.

Inheritance in Java

  • A class can inherit from one base class while implementing multiple interfaces:

    • Syntax: public class B extends A implements IX, IY, IZ.

Defining Inheritance Syntax

  • Use super keyword in derived class constructors to call base class constructors.

  • Example syntax: public class Circle extends Shape {...}

Simple Inheritance Example

  • Mammal Class:

    • Contains age attribute, Sleep method.

  • Dog Class:

    • Inherits Mammal, adds breed attribute, WagTail method.

Adding Functionality in Subclasses

  • Subclasses inherit data members and methods of the superclass.

  • Can add additional members or methods to enhance functionality.

UML Diagrams Examples

  • Rectangle Class - demonstrates attributes and methods in UML format.

  • Box Class - inherits from Rectangle, adds 3D dimensional functionalities.

Important Aspects of Inheritance

  • Java does not support multiple inheritance; constructors are not inherited.

  • Inheritance is a transitive relation.

Important Features of Inheritance

  • Derived classes extend base classes and add new members but cannot remove inherited ones.

Inheritance and Accessibility

  • Access levels define what can be accessed in subclasses:

    • Protected and public members can be accessed, private members cannot.

Object Class in Java

  • All classes inherit from Object class implicitly.

    • It contains fundamental methods including toString().

Understanding Polymorphism

  • Polymorphism allows objects to take on multiple forms.

    • Example: Using a subclass as its parent class type.

    • Late binding is used to determine method calls at runtime.

Polymorphism Implementation

  • Example demonstrates how methods are called based on object type at runtime.