Unit 3: INHERITANCE, INTERFACES AND PACKAGES
• Inheritance and its types, Role of Constructors in inheritance,
• Method Overriding, super keyword, abstract class and abstract method,
• final keyword, Static and dynamic binding in Java, finalize method.
• Interfaces: Implementing multiple inheritance and extending interfaces
• Packages: explore predefined packages, creating user defined packages and importing
the same
Prof. Hemant Borase
Inheritance
• Inheritance is one of the key features of OOP that allows us to create a new class from an
existing class.
• The new class that is created is known as subclass (child or derived class) and the existing
class from where the child class is derived is known as superclass (parent or base class).
• The extends keyword is used to perform inheritance in Java. For example,
Prof. Hemant Borase
• Explanation:
• Animal is the base class.
• Dog, Cat and Cow are derived classes that extend Animal class and provide specific
implementations of the sound() method.
• The Main class is the driver class that creates objects and demonstrates runtime
polymorphism using method overriding.
Prof. Hemant Borase
Prof. Hemant Borase
• is-a relationship
• In Java, inheritance is an is-a relationship. That is, we use inheritance only if there exists
an is-a relationship between two classes. For example,
• Car is a Vehicle
• Orange is a Fruit
• Surgeon is a Doctor
• Dog is an Animal
Prof. Hemant Borase
Types of inheritance
• There are five types of inheritance.
Prof. Hemant Borase
1. Single Inheritance
• In single inheritance, a sub-class is derived from only one super class. It inherits the
properties and behavior of a single-parent class. Sometimes, it is also known as simple
inheritance.
Prof. Hemant Borase
Prof. Hemant Borase
2. Multilevel Inheritance
• In multilevel inheritance, a subclass extends from a superclass and then the same
subclass acts as a superclass for another class.
Prof. Hemant Borase
Prof. Hemant Borase
3. Hierarchical Inheritance
• In hierarchical inheritance, more than one subclass is inherited from a single base class.
i.e. more than one derived class is created from a single base class. For example, cars and
buses both are vehicle
Prof. Hemant Borase
Prof. Hemant Borase
Java Interface
• An Interface in Java is an abstract type that defines a set of methods a class must
implement.
• An interface acts as a contract that specifies what a class should do, but not how it
should do it. It is used to achieve abstraction and multiple inheritance in Java. We define
interfaces for capabilities (e.g., Comparable, Serializable, Drawable).
• A class that implements an interface must implement all the methods of the interface.
Only variables are public static final by default.
• Before Java 8, interfaces could only have abstract methods (no bodies). Since Java 8, they
can also include default and static methods (with implementation) and since Java
9, private methods are allowed.
• Interface is ideal for achieving abstraction and multiple inheritance.
Prof. Hemant Borase
When to Use Class and Interface?
• Use a Class when:
• Use a class when you need to represent a real-world entity with attributes (fields) and
behaviors (methods).
• Use a class when you need to create objects that hold state and perform actions
• Classes are used for defining templates for objects with specific functionality and
properties.
• Use an Interface when:
• Use an interface when you need to define a contract for behavior that multiple classes
can implement.
• Interface is ideal for achieving abstraction and multiple inheritance.
• Implementation: To implement an interface, we use the keyword implements
Prof. Hemant Borase
How to Implement Multiple Inheritance by Using Interfaces in Java?
• Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit
properties of more than one parent class.
• The problem occurs when methods with the same signature exist in both the super
classes and subclass.
• On calling the method, the compiler cannot determine which class method to be called
and even on calling which class method gets the priority.
Prof. Hemant Borase
Achieving Multiple Inheritance with Interfaces
• Java allows a class to implement multiple interfaces, thus supporting multiple inheritance safely.
Prof. Hemant Borase
5. Hybrid Inheritance
• It is a mix of two or more of the above types of inheritance. In Java, we can achieve
hybrid inheritance only through Interfaces if we want to involve multiple inheritance to
implement Hybrid inheritance.
Prof. Hemant Borase
Prof. Hemant Borase
Extending Interfaces
• One interface can inherit another by the use of keyword extends. When a class
implements an interface that inherits another interface, it must provide an
implementation for all methods required by the interface inheritance chain.
Prof. Hemant Borase
Prof. Hemant Borase
Method Overriding in Java
• When a subclass provides a specific implementation for a method that is already defined
in its parent class, it is called method overriding. The overridden method in the subclass
must have the same name, parameters, and return type as the method in the parent
class.
• Rules for Method Overriding
• Name, parameters, and return type must match the parent method.
• Java picks which method to run at run time, based on the actual object type, not just the
reference variable type.
• Static methods cannot be overridden.
• The @Override annotation catches mistakes like typos in method names.
Prof. Hemant Borase
Prof. Hemant Borase
Constructors in Inheritance
• Inheritance and Constructors in Java control how objects are initialized in a class
hierarchy. When a child class object is created, the parent class constructor executes first
to ensure proper initialization. Constructor chaining ensures that both parent and child
class states are set correctly.
• Parent class constructor is called before the child class constructor.
• super() is used to call the parent constructor and must be the first statement.
• A parameterized parent constructor must be explicitly invoked using super(arguments).
Prof. Hemant Borase
Implicit Call to Superclass Constructor
• If a subclass constructor does not explicitly call a superclass constructor, Java
automatically inserts a call to the no-argument constructor of the superclass using
super().
Prof. Hemant Borase
Using super() to Call Parameterized Constructor
• If the superclass does not have a no-argument constructor, the subclass must explicitly
call the parameterized constructor using super().
Prof. Hemant Borase
Super Keyword in Java
• The super keyword in Java is used to refer to the immediate parent class object in an
inheritance hierarchy. It allows a subclass to explicitly access parent class members when
they are hidden or overridden. This keyword helps maintain clarity and control while
working with inheritance.
• Used to call parent class constructors using super().
• Helps access parent class methods and variables when overridden or hidden.
• Ensures proper inheritance behavior and code reusability.
Prof. Hemant Borase
Prof. Hemant Borase
Abstract Method in Java
• An abstract method is a method declared without an implementation (i.e., a body). It
only defines the method signature, and subclasses must provide the implementation.
• Abstract methods declared using the abstract keyword
• A class containing one or more abstract methods must be declared abstract.
• Does not have a body
• Can exist only inside an abstract class or interface
• Subclasses must override and implement it
• Abstract methods cannot use the following modifiers(final, static, private, synchronized).
Prof. Hemant Borase
Abstract Class in Java
• In Java, an abstract class is a class that cannot be instantiated
and is designed to be extended by other classes. It is used to
achieve partial abstraction, where some methods are
implemented while others are left for subclasses to define. An
abstract class is declared using the abstract keyword.
• It may contain:
• Abstract methods (methods without a body)
• Concrete methods (methods with implementation)
• Constructors
• Instance variables
• Static and final methods
• In Java, we can have an abstract class without any abstract
method. This allows us to create classes that cannot be
instantiated but can only be inherited. It is as shown below as
follows with help of a clean java program.
Prof. Hemant Borase
Abstract Class with Only Abstract Method.
Prof. Hemant Borase
Abstract Class with Constructor and Methods.
Prof. Hemant Borase
final Keyword in Java
• The final keyword is a non-access modifier used to restrict modification. It applies to
variables (value cannot change) methods (cannot be overridden) and classes (cannot be
extended). It helps create constants, control inheritance and enforce fixed behavior.
• The following are different contexts where the final is used:
• Variable
• Method
Prof. Hemant Borase
Characteristics of final keyword in Java
• Final variables hold a value that cannot be reassigned after initialization.
• Final methods cannot be overridden by subclasses.
• Final classes cannot be extended.
• Initialization rules require that a final variable must be assigned exactly once either at
declaration or inside constructors or initializer blocks.
• Reference final variables cannot change which object they point to though the internal
state of the object can change.
• Static final variables represent constants shared across all objects.
• Blank final variables are declared without initialization and must be assigned once before
use.
• Local final variables inside methods must be initialized within their block.
Prof. Hemant Borase
Prof. Hemant Borase
Advantages of final Keyword
• Supports immutability by preventing reassignment
• Helps compiler and JVM optimize code in some scenarios
• Makes behavior predictable since values or methods stay unchanged
• Prevents accidental or unauthorized modification of critical logic
• Preserves API contracts by avoiding unwanted overriding
Prof. Hemant Borase
Static vs Dynamic Binding in Java
• There are certain key points that are needed to be remembered before adhering forward
where we will be discussing and implementing static and dynamic bindings in Java later
concluding out the differences.
• private, final and static members (methods and variables) use static binding while for
virtual methods (In Java methods are virtual by default) binding is done during run time
based upon the run time object.
• The static binding uses Type information for binding while Dynamic binding uses Objects
to resolve to bind.
• Overloaded methods are resolved (deciding which method to be called when there are
multiple methods with the same name) using static binding while overridden methods
use dynamic binding, i.e, at run time.
Prof. Hemant Borase
Static Binding
• The binding which can be resolved at compile time by the compiler is known as static or
early binding. The binding of all the static, private, and final methods is done at compile-
time.
Prof. Hemant Borase
finalize() Method in Java
• The finalize() method in Java is called by the Garbage Collector just before an object is
destroyed. It allows the object to perform a clean-up activity. Clean-up activity means
closing the resources associated with that object, like Database Connection, Network
Connection or we can say resource de-allocation.
• It is defined in the Object class and available to all Java classes.
• It is not a reserved keyword.
• Called only once per object by the Garbage Collector.
• What is Finalization in Java?
• Just before destroying any object, the garbage collector always calls the finalize() method
to perform clean-up activities on that object. This process is known as Finalization in
Java. The Garbage collector calls the finalize() method only once on any object.
Prof. Hemant Borase
Prof. Hemant Borase
Java Packages
• A package in Java is a mechanism to group related classes, interfaces, and sub-packages
into a single unit. Packages help organize large applications, avoid naming conflicts,
provide access protection, and make code modular and maintainable.
• Avoiding name conflicts (two classes with the same name can exist in different packages)
• Providing access control using public, protected, and default access
• Reusability: packaged code can be imported and used anywhere
• Encouraging modular programming
• Types of Java Packages
Prof. Hemant Borase
Built-in Packages
• Built-in Packages comprise a large number of classes that are part of the Java API. Some
of the commonly used built-in packages are:
• [Link]: Contains language support classes(e.g, classes that define primitive data types,
math operations). This package is automatically imported.
• [Link]: Contains classes for supporting input/output operations.
• [Link]: Contains utility classes that implement data structures such as Linked Lists and
Dictionaries, as well as support for date and time operations.
• [Link]: Contains classes for creating Applets.
• [Link]: Contains classes for implementing the components for graphical user interfaces
(like buttons, menus, etc).
Prof. Hemant Borase
Prof. Hemant Borase
User-Defined Packages in Java
• User-defined packages are those packages that are designed or created by the developer
to categorize classes and packages. It can be imported into other classes and used in the
same way as we use built-in packages. But if we omit the package statement, the class
names are put into the default package, which has no name.
• To create a package, we're supposed to use the package keyword.
Prof. Hemant Borase
Steps to Create User-defined Packages
• Step 1: Creating a package in a Java class. The format is very simple and easy. Just write a
package by following its name
Step 2: Include the class in a Java package, but remember that a class only has one package
declaration.
Step 3: Now the user-defined package is successfully created, we can import it into other
packages and use it's functions.
Prof. Hemant Borase
• O/P: This is a user-defined package.
Prof. Hemant Borase