Module 4:
Inheritance, Interfaces & Packages
Course Code & Name : SEIT2210 - Object Oriented
Programming with Java
Outline
• This module focuses on:
• Inheritance
• Types of Inheritance
• Using super creating multilevel Hierarchy
• Method overriding
• Dynamic method dispatch
• Abstract classes
• Using final with Inheritance
• Using Package: Defining package, finding package and CLASSPATH
• Access protection
• Importing package
• Interface: Defining Interface, Implementing Interface, Variables in Interface.
Inheritance
• Inheritance is a fundamental object-oriented technique used to
organize and create reusable classes.
• Inheritance is the process by which one object acquires the
properties of another object.
• Inheritance means creating new classes based on existing ones. A
class that inherits from another class can reuse the methods and
fields of that class. In addition, you can add new fields and methods
to your current class as well.
Inheritance
Terminologies
• Class: Class is a set of objects that shares common characteristics/ behavior and common
properties/ attributes.
• Super Class/Parent Class: The class whose features are inherited is known as a superclass(or a
base class or a parent class).
• Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields and methods in
addition to the superclass fields and methods.
• Reusability: when we want to create a new class and there is already a class that includes some
of the code that we want, we can derive our new class from the existing class. By doing this, we
are reusing the fields and methods of the existing class.
Extend Keyword
• The extends keyword is used for inheritance in Java. Using the
extends keyword indicates you are derived from an existing class. In
other words, “extends” refers to increased functionality.
Example
Member Access and Inheritance
• Although a subclass includes
all of the members of its
superclass, it cannot access
those members of the
superclass that have been
declared as private.
• For example, consider the
following simple class
hierarchy:
Types of Inheritance in Java
• Java supports the following four types of inheritance:
– Single Inheritance
– Multi-level Inheritance
– Hierarchical Inheritance
– Hybrid Inheritance
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.
Example
Multi-level Inheritance
• In multi-level inheritance, a
class is derived from a class
which is also derived from
another class is called multi-
level inheritance.
• Note that the classes must be at
different levels. Hence, there
exists a single base class and
single derived class but
multiple intermediate base
classes.
Example
Hierarchical Inheritance
• If a number of classes
are derived from a
single base class, it is
called hierarchical
inheritance.
Hybrid Inheritance
• Hybrid inheritance is the
combination of two or more
types of inheritance.
Super Keyword
• The super keyword in Java is a reference variable which is used
to refer immediate parent class object.
• Whenever you create the instance of subclass, an instance of
parent class is created implicitly which is referred by super
reference variable.
• We can use super keyword to access the data member or field of
parent class. It is used if parent class and child class have same
fields.
• It's especially useful in multilevel inheritance, where a class can
inherit from another class, which in turn inherits from another
class, creating a hierarchy.
Example
Super Keyword
• Super is used to
invoke parent class
constructor.
Method Overriding
• In a class hierarchy, when a method in a subclass has the same name
and type signature as a method in its superclass, then the method in
the subclass is said to override the method in the superclass.
• When an overridden method is called from within its subclass, it
will always refer to the version of that method defined by the
subclass. The version of the method defined by the superclass will
be hidden.
Example
• Output :
k: 3
Method Overriding
• When show() is invoked on an object of type B, the version of show()
defined within B is used.
• That is, the version of show() inside B overrides the version declared
in A.
• If you wish to access the superclass version of an overridden
method, you can do so by using super.
• For example, in this version of B, the superclass version of show() is
invoked within the subclass’ version. This allows all instance
variables to be displayed
Cont..
• Output
i and j: 1 2
k: 3
Dynamic Method Dispatch
• Dynamic method dispatch is the mechanism by which a call to an
overridden method is resolved at run time, rather than compile time.
• Dynamic method dispatch is important because this is how Java
implements run-time polymorphism.
Example
Cont..
• Inside the main( ) method, objects of type A, B, and C are declared.
Also, a reference of type A, called r, is declared.
• The program then in turn assigns a reference to each type of object
to r and uses that reference to invoke callme( ).
• As the output shows, the version of callme( ) executed is determined
by the type of object being referred to at the time of the call
Abstract Classes
• Abstract classes in Java are designed to be extended by other classes,
and they may contain abstract methods that must be implemented
by subclasses.
• They allow you to define common behavior and enforce certain rules
across different subclasses.
• Key Characteristics of Abstract Classes:
– An abstract class must be declared with an abstract keyword.
– It can have abstract and non-abstract methods.
– It cannot be instantiated.
– It can have constructors and static methods also.
– It can have final methods which will force the subclass not to change the
body of the method.
Example
Using final with Inheritance
• The keyword final has three uses:
1. It can be used to create the equivalent of a named constant.
2. To prevent overriding
3. To prevent Inheritance
Using final to Prevent Overriding
• While method overriding is one of Java’s most powerful features,
there will be times when you will want to prevent it from occurring.
• To disallow a method from being overridden, specify final as a
modifier at the start of its declaration. Methods declared as final
cannot be overridden.
Example
Using final to Prevent Inheritance
• Sometimes you will want to
prevent a class from being
inherited. To do this, precede
the class declaration with final.
Declaring a class as final
implicitly declares all of its
methods as final, too.
Package
• In Java, a package is a namespace that organizes a set of related
classes and interfaces.
• Packages are used to avoid name conflicts and to control access to
the classes, interfaces, and other resources.
• Think of a package like a folder in a file directory. It groups together
similar classes.
• Package in java can be categorized in two form, built-in package
and user-defined package.
• There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc.
Why Use Packages?
• Organization: Packages help in organizing classes into namespaces,
making large software projects easier to manage.
• Avoid Name Conflicts: Classes with the same name can exist in
different packages without causing naming conflicts.
• Access Control: Packages provide a way to restrict access to classes,
interfaces, methods, and fields.
Java Package
Defining a Package
• To create a package simply include a package command as the first
statement in a Java source file. Any classes declared within that file
will belong to the specified package.
• The package statement defines a name space in which classes are
stored. If you omit the package statement, the class names are put
into the default package, which has no name.
• This is the general form of the package statement:
package pkg;
Cont…
• You can create a hierarchy of packages. To do so, simply separate
each package name from the one above it by use of a period. The
general form of a multileveled package statement is shown here:
package pkg1[.pkg2[.pkg3]];
• A package hierarchy must be reflected in the file system of your Java
development system.
Example
Steps for the execution
• Call this file [Link] and put it in a directory called MyPack.
• Next, compile the file using following command:
javac –d . [Link]
• The -d is a switch that tells the compiler where to put the class file i.e ‘.’ it
represents destination. The ‘.’ represents the current folder.
• Make sure that the resulting .class file is also in the MyPack directory.
• Then, try executing the AccountBalance class, using the following command
line:
java [Link]
Finding Packages and CLASSPATH
• First, by default, the Java run-time
system uses the current working
directory as its starting point. Thus,
How does the Java run-time if your package is in a subdirectory of
system know where to look for the current directory, it will be found.
packages that you create?
• Second, you can specify a directory
The answer has three parts. path or paths by setting the
CLASSPATH environmental variable.
• Third, you can use the -classpath
option with java and javac to specify
the path to your classes.
Access Protection
• Java provides many levels of protection to allow fine-grained control over the
visibility of variables and methods within classes, subclasses, and packages.
• Packages act as containers for classes and other subordinate packages. Classes
act as containers for data and code.
• The class is Java’s smallest unit of abstraction. Because of the interplay
between classes and packages, Java addresses four categories of visibility for
class members:
– Subclasses in the same package
– Non-subclasses in the same package
– Subclasses in different packages
– Classes that are neither in the same package nor subclasses
Class Member Access
Importing Packages
• If you use package.* then all the classes and interfaces of this package will be
accessible but not subpackages.
• The import keyword is used to make the classes and interface of another package
accessible to the current package.
• This is the general form of the import statement:
import pkg1 [.pkg2].(classname | *);
• Here, pkg1 is the name of a top-level package, and pkg2 is the name of a
subordinate package inside the outer package separated by a dot (.).
• Finally, you specify either an explicit classname or a star (*), which indicates that
the Java compiler should import the entire package.
Example
Interface
• An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
• The interface in Java is a mechanism to achieve abstraction. There can
be only abstract methods in the Java interface, not method body.
• It is used to achieve abstraction and multiple inheritance in Java.
• It cannot be instantiated just like the abstract class.
Why use Java interface?
• There are mainly three reasons to use interface:
– It is used to achieve abstraction.
– By interface, we can support the functionality of multiple inheritance.
– It can be used to achieve loose coupling.
Defining Interface
• An interface is declared by using
the interface keyword.
• It provides total abstraction;
means all the methods in an
interface are declared with the
empty body, and all the fields are
public, static and final by default.
• A class that implements an
interface must implement all the
methods declared in the interface.
Implementing Interfaces
• Once an interface has been
defined, one or more classes can
implement that interface.
• To implement an interface,
include the implements clause
in a class definition, and then
create the methods required by
the interface.
• The general form of a class that
includes the implements clause
looks like this:
Accessing Implementations Through Interface
References
Variables in Interfaces
• The interfaces also
use to import shared
constants into
multiple classes by
simply declaring an
interface that
contains variables
that are initialized to
the desired values.