Inheritance

The process by which new classes are generated from existing classes by maintaining some or all of its properties is called inheritance. The original classes through which other classes can be generated are classed parent class or superclass or base class whereas the generated classes are known as derived classes or subclasses.

Example:

For a class Vehicle, derived classes can be a Car, Bike, Bus, etc. In this example, these derived classes are passed down the properties of their parent class Vehicle along with their own properties like the number of wheels, seats, etc. The above example is demonstrated in the following code –

Java




class Vehicle {
    String belongsTo = "automobiles";
}
public class Car extends Vehicle {
    int wheels = 4;
    public static void main(String args[])
    {
        Car c = new Car();
        System.out.println("Car belongs to- "
                           + c.belongsTo);
        System.out.println("Car has " + c.wheels
                           + " wheels.");
    }
}


Output:

Car belongs to- automobiles
Car has 4 wheels.

Types of inheritance 

  1. Single inheritance – One derived class generated out of a single base class.
  2. Multiple inheritance – One derived class generated out of two or more base classes.
  3. Multilevel inheritance – One derived class is generated out of a base class which is also generated out of another base class.
  4. Hierarchical inheritance – A group of derived classes generated out of a base class which in turn might have derived classes of their own.
  5. Hybrid inheritance – A lattice structure out of a combination of multilevel and multiple inheritances.

Polymorphism

The ability in which objects can have a common external interface with different internal structures. While inheritance is implemented, Polymorphism is particularly effective. In polymorphism, functions can have the same names but different parameter lists. 

Example: A Car and a Bus class both can have wheels() method but the number of wheels for both is different so since the internal implementation is different, no conflict arises.

Generalization and Specialization

The representation of the hierarchy of different classes where derived classes are generated out of base classes –

Generalization

The combination of common characteristics from derived classes in order to form a generalized base class. Example – “A cow is a land animal”.

Specialization

The distinction of objects from existing classes into specialized groups is specialization. This is almost like a reverse process of generalization. 

The following diagram demonstrates generalization vs specialization –

Links and Association

Link: The representation of a connection in which an object collaborates with other objects i.e the relationship between objects is called a link.

Association: A set of links that identify and demonstrated the behavior between objects is called association. The instances of associations are called links.

Degree of Association

There are three types of Association:

  • Unary relationship: Objects of the same class get connected.
  • Binary relationship: Objects of two classes are connected.
  • Ternary relationship: Objects of three or more classes are connected.

Cardinality of Binary Association

  • One-to-One: One object of class A associated with an object of B.
  • One-to-Many: One object of class A associated with more than one object of class B.
  • Many-to-Many: More than one object of class A associated with more than one object from class B.

Composition or Aggregation

A class can generally be made using a combination of other classes and objects. This is class composition or aggregation.  The “has-a” or “part-of” of a relationship is generally the aggregate. If an object consists of other objects then this object is called an aggregate object.

Example:

In a student-books relationship, the student “has-a” book and the book is a “part-of” the student’s curriculum. Here the student is the complete object. An aggregate includes –

  • Physical containment – For example, a bag consists of zips and a bottle holder.
  • Conceptual containment – For example, a student has marks.

Advantages of the Object Model

  • Enable DRY (Don’t repeat yourself) way of writing code.
  • While integrating complex systems, reduces development risks.
  • Enables quick software development.
  • Enables upgrades quite easily.
  • Makes software easier to maintain.


Object Model in Java

The object model is a system or interface which is basically used to visualize elements in terms of objects in a software application. It is modeled using object-oriented techniques and before any programming or development is done, the object model is used to create a system model or an architecture. It defines object-oriented features of a system like inheritance, encapsulation, and many other object-oriented interfaces. Let us learn some of those object-oriented terminologies in-depth:

Similar Reads

Objects and Classes

These form the basis of the Object-Oriented Paradigm in Java or any other Object-Oriented Programming Languages. They are explained in detail here:...

Encapsulation and Data Hiding

...

Inheritance

To protect our data from being accessed and exploited by outside usage, we need to perform encapsulation. This is explained in detail below –...

Contact Us