Chapter 3 Inheritance
Chapter 3 Inheritance
Reusing members
Introduction
• In OOP, inheritance is a way to reuse code of existing objects, or
to establish a subtype from an existing object.
• Using inheritance classes can be derived from other classes,
thereby inheriting fields and methods from those classes.
• Definitions:
A class that is derived from another class is called a subclass (also a
derived class, extended class, or child class). The class from which
the subclass is derived is called a superclass (also a base class or a
parent class).
Excepting Object, which has no superclass, every class has one and
only one direct superclass (single inheritance). In the absence of any
other explicit superclass, every class is implicitly a subclass of Object.
Classes can be derived from classes that are derived from classes
that are derived from classes, and so on, and ultimately derived from
the topmost class, Object. Such a class is said to be descended from
all the classes in the inheritance chain stretching back to Object.
Inheritance
• The idea of inheritance is simple but powerful:
When you want to create a new class and there is
already a class that includes some of the code
that you want, you can derive your new class from
the existing class.
In doing this, you can reuse the fields and
methods of the existing class without having to
write (and debug!) them yourself.
A subclass inherits all the members (fields,
methods, and nested classes) from its superclass.
Constructors are not members, so they are not
inherited by subclasses, but the constructor of the
superclass can be invoked from the subclass.
What you can do in a
•
subclass
A subclass inherits all of the public and protected members
of its parent, no matter what package the subclass is in.
• If the subclass is in the same package as its parent, it also
inherits the package-private members of the parent.
• In the subclass:
The inherited fields can be used directly, just like any other
fields.
You can declare a field in the subclass with the same name as
the one in the superclass, thus hiding it (not recommended).
You can declare new fields in the subclass that are not in the
superclass.
The inherited methods can be used directly as they are.
You can write a new instance method in the subclass that has
the same signature as the one in the superclass, thus
overriding it.
In the subclass…
You can write a new static method in the subclass
that has the same signature as the one in the
superclass, thus hiding it.
You can declare new methods in the subclass that
are not in the superclass.
You can write a subclass constructor that invokes
the constructor of the superclass, either implicitly
or by using the keyword super.
Inheritance…
• What is not possible using java class
Inheritance?
Private members of the superclass are not
inherited by the subclass and can only be
indirectly accessed.
Members that have default accessibility in the
superclass are also not inherited by subclasses in
other packages, as these members are only
accessible by their simple names in subclasses
within the same package as the superclass.
Since constructors and initializer blocks are not
members of a class, they are not inherited by a
subclass.
A subclass can extend only one superclass.
extends keyword
• In Java, we extends keyword is used to
implement inheritance.
• The general structure looks as follows:
class subclass extends superclass{
//sublcass body
}
this and super keywords
• The two keywords, this and super help you
explicitly name the field or method that you
want.
• this keyword is used as a reference to the
current object which is an instance of the current
class.
• The keyword super also references the current
object, but as an instance of the current class’s
super class.
Multiple Inheritance
• Multiple inheritance occurs if a class is a
subclass of two or more super classes.
• Java doesnot support a multiple inheritance.
• But it approximates multiple inheritance through
interfaces.
The procedure of using members of an interface is
called implementing.
A class can implement as many interfaces as
possible.
But it can extends only one superclass.
Interface
• It is a type that can be satisfied by any class
that implements the interface.
• A Java interface specifies a set of methods that
any class that implements the interface must
have.
• It specifies the headings for methods that must
be defined in any class that implements the
interface.
It contains no instance variables nor any complete
method definitions.
• To implement interface a class must do two
things:
Implementing interface
It must include the phrase
implements Interface_Name at the start of the class
definition.
To implement more than one interface, you list all
the interface names, separated by commas, as in
implements SomeInterface, AnotherInterface
The class must implement all the method
headings listed in the definitions of the interfaces.
public interface Ordered {
public boolean precedes(Object other);
public boolean follows(Object other);
}
• An interface and all of its method headings are
normally declared to be public.