You are on page 1of 21

CSEC 313: Object – Oriented Programming

Lecture – 8 & 9 :Inheritance

Uttam Kumar Dey


CSEC 313: Object – Oriented Programming

Outline
► Inheritance Basics
► Member access
► Deriving new classes from old
classes(Code reuse)
► Class hierarchies
► Protected access specifier
► Polymorphism using inheritance
► abstract class
► uses of final keyword in inheritance
CSEC-313: Object – Oriented Programming

- Inheritance is a way to create new classes from


existing classes. The existing class is called the superclass
and the new class created from it is called the subclass.
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.
Inheritance
Example:

Here,
• The student extends person
• The student is a person
• The student is a subclass of person
• The person is a superclass of student and employee

Source code: person.java, student.java, employee.java


Inheritance
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. You
can use the inherited members as is, replace them, hide
them, or supplement them with new members:

• 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.
Inheritance
• You can write a new instance method in the subclass
that has the same signature as the one in the
superclass, thus overriding it.
• 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
Private Members in a Superclass
• A subclass does not inherit the private members of
its parent class. However, if the superclass has
public or protected methods for accessing its
private fields, these can also be used by the
subclass.

• A nested class has access to all the private members


of its enclosing class—both fields and methods.
Therefore, a public or protected nested class
inherited by a subclass has indirect access to all of
the private members of the superclass.
Inheritance
Uses of Super keyword
super has two general forms. The first calls the
superclass’ constructor.
The second is used to access a member of the
superclass that has been hidden by a member of a
subclass.
• Using super to Call Superclass Constructors
A subclass can call a constructor defined by its
superclass by use of the following form of super:
super(arg-list);
Here, arg-list specifies any arguments needed by the constructor in the
superclass. super( ) must always be the first statement executed inside a
subclass’ constructor. To see how super( ) is used, consider this improved
version of the BoxWeight( ) class:
Inheritance
class Box{
double x,y,z;
Box(double a, double b, double c) {
x=a; y=b; z=c;
}

// BoxWeight now uses super to initialize its Box attributes.


class BoxWeight extends Box {
double weight; // weight of box
// initialize width, height, and depth using super()
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}}
Inheritance
• Using super to access a member of the superclass
The second form of super acts somewhat like this,
except that it always refers to the superclass of the
subclass in which it is used. This usage has the
following general form:
super.member
Here, member can be either a method or an instance
variable.
This second form of super is most applicable to
situations in which member names of a subclass hide
members by the same name in the superclass.
Inheritance
Example:
class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);subOb.show();}}
Inheritance
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 a


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.
Inheritance
Example:
// Method overriding.
class A {
int i, j;
………………….
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);}}
class B extends A {
int k;
…………………….
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B}}
Inheritance
Abstract Class
• An abstract class is a class that is declared abstract—
it may or may not include abstract methods.
• Abstract classes cannot be instantiated, but they can
be subclassed.

Abstract Method
An abstract method is a method that is declared without
an implementation (without braces, and followed by a
semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, then the class itself
must be declared abstract, as in:
Inheritance
public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
Example: abstractExample.java, abstractExample1.java
Inheritance
Using final with Inheritance
The keyword final has three uses:
• First, it can be used to create the equivalent of a
named constant.
• The other two uses of final apply to inheritance.
- to Prevent Method Overriding
- to Prevent Inheritance
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. The following
fragment illustrates final:
class A {
final void methodname() {
System.out.println("This is a final method.");
}
}
class B extends A {
void methodname() { // ERROR! Can't override.
System.out.println("Illegal!");
}}
Inheritance
Using final to prevent Inheritance
Sometimes we 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.
Here is an example of a final class:
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
Inheritance
References:
Tutorials: Interfaces and Inheritance
Chapter – 8: Herb. Schildt
Inheritance

Questions?

You might also like