0% found this document useful (0 votes)
35 views8 pages

Oops

Uploaded by

Anisha Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views8 pages

Oops

Uploaded by

Anisha Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

RCC Institute of Information Technology Department

of Computer Science & Engineering Academic Session


2023-2024

NAME : ANNESA SUTAR


UNIVERSITY ROLL NUMBER : 11700121096
CLASS ROLL NUMBER : CSE2021102
SEMESTER : 5th SEM(3rd YEAR)
PAPER NAME : Object Oriented Programming
PAPER CODE : PCC-CS503

TYPES OF INHERITANCE &


DIFFERENCES BETWEEN ABSTRACT
CLASS AND INTERFACE

Related COs 2

Full marks

Obtained marks
Total marks obtained
Signature of the faculty
ABSTRACT
In object-oriented programming, inheritance is a fundamental concept that allows for the creation of new
classes based on existing ones. It enables the reuse of code and promotes a hierarchical relationship
between classes. This article will provide an in-depth exploration of the various types of inheritance, along
with examples, to illustrate their usage and benefits.

On the other hands, two important concepts of object-oriented programming that often come up are abstract
classes and interfaces. Both abstract classes and interfaces provide a way to define common behavior and
create a contract for classes to follow. However, they have distinct differences and use cases. In this article,
we will explore the differences between abstract classes and interfaces in both Java and C#. We will
discuss their definitions, syntax, usage, and when to use each of them.
Types of Inheritance in Object-Oriented Programming

1. Single Inheritance
Single inheritance is a type of inheritance where a class extends only one other class, also known as a
superclass. It establishes a parent-child relationship between the classes, allowing the child class to inherit
the attributes and methods of the parent class. This type of inheritance promotes code reuse and simplifies
the class hierarchy.

In this example, the Child class inherits from the Parent class, acquiring its display() method. The Child class
also adds its own unique method, show().

2. Multiple Inheritance
Multiple inheritance is a type of inheritance where a class can inherit from multiple classes, known as
superclasses. This allows the child class to acquire properties and behaviors from multiple parent classes.
However, multiple inheritance is not supported in all programming languages, including Java. It can lead to
complexity and potential conflicts in method names and attribute definitions.

In this example we have two interfaces, ‘Swimmer’ and ‘Flyer’, each defining a specific behavior. The Bird
class implements both ‘Swimmer’ and ‘Flyer’ interfaces, which means it inherits both swimming and flying
behaviors. In the main method, we create an instance of the ‘Bird’ class and demonstrate that it can both
swim and fly.
This demonstrates how Java achieves multiple inheritance through interfaces, allowing classes to inherit
behaviors from multiple sources while avoiding the complexities and ambiguities associated with traditional
multiple inheritance from multiple classes.
3. Multilevel Inheritance
Multilevel inheritance is a type of inheritance where a class is derived from another derived class. This creates
a hierarchical chain of inheritance, with each class inheriting properties and behaviors from its parent class.
It allows for the creation of specialized classes that inherit characteristics from multiple levels up the
inheritance hierarchy

In this example ‘Vehicle’ is the base class with a method start. ‘Car’ is an intermediate class that inherits
from Vehicle and adds a method drive. ‘SportsCar’ is the derived class that inherits from ‘Car’ and adds a
method accelerate().
When we create an instance of ‘SportsCar’ and call its methods, we can see that it can access methods from
all three levels of the inheritance hierarchy. This is the essence of multilevel inheritance, where each derived
class inherits not only from its immediate parent but also from all the ancestor classes in the chain.

4. Hierarchical Inheritance
Hierarchical inheritance is a type of inheritance where multiple classes inherit from a single superclass. This
results in a tree-like structure, with the superclass at the top and multiple subclasses branching out from it.
Each subclass inherits the properties and behaviors of the superclass while adding its own unique features.
In this example ‘Animal’ is the base class with an eat method. ‘Dog’ and ‘Cat’ are two derived classes that
inherit from the ‘Animal’ base class. Both ‘Dog’ and ‘Cat’ have their own unique methods (bark and
meow) in addition to the eat method inherited from the base class.
Hierarchical inheritance allows multiple classes to share common behavior and attributes from a single
parent class while still having the flexibility to define their own specific behavior. It promotes code reuse
and a structured approach to designing class hierarchies in Java.

5. Hybrid Inheritance
Hybrid inheritance is a combination of single and multiple inheritance. It allows for the creation of classes
that inherit from both a single superclass and multiple superclasses. This type of inheritance provides
flexibility in designing class hierarchies and enables code reuse from multiple sources.

In this example, we've demonstrated hybrid inheritance by combining single inheritance, multiple
inheritance (through interfaces), hierarchical inheritance, and multilevel inheritance within a single
program. This showcases Java's ability to handle various inheritance scenarios, providing flexibility and
power in creating complex class hierarchies.
The differences between Abstract class and Interface

❖ Abstract Class

An abstract class in Java is a class that is declared with the abstract keyword. It serves as a blueprint for
other classes and cannot be instantiated. Abstract classes can contain both abstract and non-abstract
methods. Abstract methods are declared without implementation and must be overridden by the concrete
subclasses. Non-abstract methods can have an implementation and are used to provide default behavior for
the subclasses.

Declared by using “abstract” keyword in its class definition. And also have fields and constructors.

For example :

❖ Interface:

An interface in Java is a collection of abstract methods. It defines a contract that a class must adhere to by
implementing all the methods declared in the interface. Unlike abstract classes, interfaces cannot contain
any method implementations. All methods in an interface are implicitly public and abstract.

Declared by using “interface” keyword. Since java doesn’t support multiple inheritances in the case of
class, by this interface it can achieve multiple inheritances.

For example :

An interface is about capabilities like a Player may be an interface & any class implementing Player must
be able to move().Therefore, it specifies set of methods that the class has to implement. Java library named
Comparator Interface, this interface is used to sort a collection.

Any class can extend only 1 class at a time but can any class implement infinite number of interfaces.
❖ Differences Between Abstract Class and Interface in Java
Now that we have a basic understanding of abstract classes and interfaces in Java, let's explore the
differences between them:

1. Syntax and Usage


• Abstract Class: An abstract class is declared using the abstract keyword followed by the class
keyword. It can have both abstract and non-abstract methods. Abstract classes are used when we
want to provide a common base implementation and define a hierarchy of related classes.
• Interface: An interface is declared using the interface keyword. It can only contain abstract methods.
Interfaces are used when we want to define a contract that classes must adhere to. They allow
multiple inheritance of behavior as a class can implement multiple interfaces.

2. Instantiation
• Abstract Class: Abstract classes cannot be instantiated directly. They are meant to be extended by
subclasses, which provide concrete implementations for the abstract methods.
• Interface: Interfaces cannot be instantiated either. They are implemented by classes, which provide
implementations for all the methods declared in the interface.

3. Inheritance
• Abstract Class: A class can only extend a single abstract class. This means that the subclass inherits
both the abstract and non-abstract methods of the abstract class. Java does not support multiple
inheritance of classes.
• Interface: A class can implement multiple interfaces. This allows the class to inherit the abstract
methods from multiple interfaces and provide implementations for all of them. Java supports multiple
inheritance of interfaces.

4. Default Behavior
• Abstract Class: Abstract classes can provide default behavior by implementing non-abstract
methods. Subclasses can choose to override these methods or use the default implementation.
• Interface: Interfaces cannot provide any default behavior. All methods declared in an interface must
be implemented by the classes that implement the interface.

5. Access Modifiers
• Abstract Class: Abstract class members can have different access modifiers, such as public, private,
protected, or no modifier at all. This allows for finer-grained control over the visibility of the
members.
• Interface: All methods in an interface are implicitly public. They cannot have any other access
modifiers.
CONCLUSION
Inheritance is a powerful mechanism in object-oriented programming that allows for code reuse,
modularity, and polymorphism. Understanding the different types of inheritance, such as single, multiple,
multilevel, hierarchical, and hybrid, enables developers to design class hierarchies that capture the essence
of their domain and promote efficient and maintainable code.

In this article, we explored the advantages and disadvantages of inheritance, along with examples that
illustrate its usage. By leveraging inheritance effectively, developers can build robust and flexible software
systems that promote code reusability and extensibility.

On the other hand, abstract classes and interfaces are essential tools in object-oriented programming for
defining class structures and ensuring code contract adherence. They have distinct characteristics and use
cases, with abstract classes providing more flexibility in terms of implementation and interfaces enabling
multiple inheritance and strict contract adherence. The choice between them depends on the specific
requirements of your software design and the relationships between your classes. Careful consideration of
these differences is crucial for designing robust and maintainable object-oriented systems.

REFERENCES

1. https://medium.com/@ubale.vikas9/interface-in-oops-6eae3731c242
2. https://users.cs.utah.edu/~germain/PPS/Topics/interfaces.html
3. https://www.geeksforgeeks.org/interfaces-in-java/
4. https://cs-fundamentals.com/tech-interview/java/difference-between-abstract-class-and-interface
5. https://www.edureka.co/blog/difference-between-abstract-class-and-interface

You might also like