0% found this document useful (0 votes)
6 views

Inheritance

The document discusses inheritance and method overriding in object-oriented programming. Inheritance allows a subclass to inherit attributes and methods from a superclass. Method overriding occurs when a subclass defines a method with the same name and parameters as a method in the superclass, thereby overriding the superclass version of that method when called on subclass objects.

Uploaded by

mrudul mathews
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Inheritance

The document discusses inheritance and method overriding in object-oriented programming. Inheritance allows a subclass to inherit attributes and methods from a superclass. Method overriding occurs when a subclass defines a method with the same name and parameters as a method in the superclass, thereby overriding the superclass version of that method when called on subclass objects.

Uploaded by

mrudul mathews
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

M

O
D
U
L
Inheritance E

Sunu Fathima T.H, Asst.Professor, IT, SOE

1
Inheritance
Inheritance is the procedure in which one class
inherits the attributes and methods of another class.
We can create a general class that defines traits
common to a set of related items. This class can then
be inherited by other, more specific classes, each
adding those things that are unique to it.

2
Inheritance
Superclass
A class that is inherited : Parent/Base Class

Subclass
The class that does the inheriting : Child/Derived Class

A subclass is a specialized version of a superclass. It inherits all of the


instance variables and methods defined by the superclass and adds its own,
unique elements.

3
Inheritance
Basics

4
Example

5
Example

6
Example

7
Inheritance Basics
❏ The subclass B includes all of the members of its superclass, A.
❏ This is why subOb can access i and j and call showij( ).
❏ Also, inside sum( ), i and j can be referred to directly, as if they were part
of B.
❏ Even though A is a superclass for B, it is also a completely independent,
stand-alone class.
❏ Being a superclass for a subclass does not mean that the superclass
cannot be used by itself.
❏ A subclass can be a superclass for another subclass.

8
Inheritance Basics
❏ The general form of a class declaration that inherits a superclass is shown
here:

class subclass-name extends superclass-name {

// body of class

❏ Can only specify one superclass for any subclass that you create.
❏ Java does not support the inheritance of multiple superclasses into a
single subclass.
9
Member Access
and
Inheritance

10
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.
❏ In a class hierarchy, private members remain private to their class.
❏ Following program will not compile because the reference to j inside the
sum( ) method of B causes an access violation.
❏ Since j is declared as private, it is only accessible by other members
of its own class.
❏ Subclasses have no access to it.

11
Example

12
Using super

13
Using super

❏ You want to create a superclass that keeps the details of its


implementation to itself
❏ that is, that keeps its data members private
❏ No way for a subclass to directly access or initialize these variables on its
own.
❏ Java provides a solution to this problem.
❏ Whenever a subclass needs to refer to its immediate superclass, it can do
so by use of the keyword super.

14
Using super

❏ 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.

15
Using super to Call Superclass
Constructors
❏ A subclass can call a constructor method defined by its superclass by use
of the following form of super:

super(parameter-list);

❏ parameter-list specifies any parameters needed by the constructor in the


superclass.
❏ super( ) must always be the first statement executed inside a subclass’
constructor.

16
Using super to Call Superclass
Constructors

17
Using super to Call Superclass
Constructors
❏ BoxWeight( ) calls super( ) with the parameters w, h, and d.
❏ This causes the Box( ) constructor to be called, which initializes width,
height, and depth using these values.
❏ BoxWeight no longer initializes these values itself.
❏ It only needs to initialize the value unique to it: weight.
❏ This leaves Box free to make these values private if desired.
❏ Since constructors can be overloaded, super( ) can be called using any
form defined by the superclass.
❏ The constructor executed will be the one that matches the arguments.

18
Using super to Call Superclass
Constructors
❏ When a subclass calls super( ), it is calling the constructor of its
immediate superclass.
❏ Thus, super( ) always refers to the superclass immediately above the
calling class.
❏ This is true even in a multileveled hierarchy.
❏ Also, super( ) must always be the first statement executed inside a
subclass constructor.

19
A Second Use for super

❏ 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.

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.

20
Example

21
A Second Use for super

❏ This program displays the following:

i in superclass: 1

i in subclass: 2

❏ Although the instance variable i in B hides the i in A, super allows access


to the i defined in the superclass.
❏ super can also be used to call methods that are hidden by a subclass.

22
04
Creating a
Multilevel
Hierarchy

23
Creating a Multilevel
Hierarchy
❏ Can build hierarchies that contain as many layers of inheritance as we
like.
❏ It is perfectly acceptable to use a subclass as a superclass of another.
❏ Given three classes called A, B, and C.
❏ C can be a subclass of B, which is a subclass of A.
❏ Each subclass inherits all of the traits found in all of its superclasses.
❏ C inherits all aspects of B and A.
❏ subclass BoxWeight is used as a superclass to create the subclass called
Shipment.
❏ Shipment inherits all of the traits of BoxWeight and Box, and adds a field
called cost, which holds the cost of shipping such a parcel. 24
Example

25
Example

26
Example

27
Example

28
Creating a Multilevel
Hierarchy

29
Creating a Multilevel
Hierarchy
❏ Because of inheritance, Shipment can make use of the previously
defined classes of Box and BoxWeight, adding only the extra
information it needs for its own, specific application.
❏ This is part of the value of inheritance; it allows the reuse of code.
❏ super( ) always refers to the constructor in the closest superclass.
❏ The super( ) in Shipment calls the constructor in BoxWeight.
❏ The super( ) in BoxWeight calls the constructor in Box.
❏ If a superclass constructor requires parameters, then all subclasses must
pass those parameters “up the line.”

30
05
Method
Overriding

31
Method Overriding

❏ 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.

32
Example

33
Example
Output:

k: 3

34
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 function,
you can do so by using super.
❏ In this version of B, the superclass version of show( ) is invoked within
the subclass’ version.
❏ This allows all instance variables to be displayed.

35
Example
Output:

i and j: 1 2

k: 3

36
Method Overriding

❏ Here, super.show( ) calls the superclass version of show( ).


❏ Method overriding occurs only when the names and the type signatures
of the two methods are identical.
❏ If they are not, then the two methods are simply overloaded.
❏ Example: In A,

void show() {

System.out.println("i and j: " + i + " " + j);

37
Method Overriding

❏ In B,
void show(String msg) {
System.out.println(msg + k);
}
❏ Calling from Override:
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A

38
Method Overriding

❏ Output:

This is k: 3

i and j: 1 2

❏ The version of show( ) in B takes a string parameter.


❏ This makes its type signature different from the one in A, which takes no
parameters.
❏ Therefore, no overriding (or name hiding) takes place.

39
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.
❏ This is how Java implements run-time polymorphism.
❏ A superclass reference variable can refer to a subclass object.
❏ When an overridden method is called through a superclass reference,
Java determines which version of that method to execute based upon the
type of the object being referred to at the time the call occurs.
❏ Thus, this determination is made at run time.

40
Dynamic Method Dispatch

❏ It is the type of the object being referred to (not the type of the reference
variable) that determines which version of an overridden method will be
executed.
❏ If a superclass contains a method that is overridden by a subclass, then
when different types of objects are referred to through a superclass
reference variable, different versions of the method are executed.
❏ Example (refer textbook)

41
Why Overridden Methods?

❏ Allow Java to support run-time polymorphism.


❏ Allows a general class to specify methods that will be common to all
of its derivatives, while allowing subclasses to define the specific
implementation of some or all of those methods.
❏ Another way that Java implements the “one interface, multiple
methods” aspect of polymorphism.
❏ Superclasses and subclasses form a hierarchy which moves from lesser to
greater specialization.

42
Why Overridden Methods?

❏ Superclass provides all elements that a subclass can use directly.


❏ It also defines those methods that the derived class must implement on its
own.
❏ This allows the subclass the flexibility to define its own methods,
yet still enforces a consistent interface.
❏ Thus, by combining inheritance with overridden methods, a superclass
can define the general form of the methods that will be used by all of its
subclasses.

43
Example

44
Example

45
Applying Method Overriding

46
06
Using Abstract
Classes

47
Using Abstract Classes

❏ Define a superclass that declares the structure of a given abstraction


without providing a complete implementation of every method.
❏ Superclass only defines a generalized form that will be shared by all
of its subclasses, leaving it to each subclass to fill in the details.
❏ When a superclass is unable to create a meaningful implementation
for a method.
❏ Ensure that a subclass does override all necessary methods.
❏ Java’s solution to this problem is the abstract method.
❏ Certain methods be overridden by subclasses by specifying the abstract
type modifier.
48
Using Abstract Classes

❏ These methods are sometimes referred to as subclasser responsibility


because they have no implementation specified in the superclass.
❏ Thus, a subclass must override them—it cannot simply use the version
defined in the superclass.
❏ To declare an abstract method, use this general form:

abstract type name(parameter-list);

❏ No method body is present.

49
Using Abstract Classes

❏ Any class that contains one or more abstract methods must also be
declared abstract.
❏ To declare a class abstract, you simply use the abstract keyword in front
of the class keyword at the beginning of the class declaration.
❏ There can be no objects of an abstract class.
❏ Any subclass of an abstract class must either implement all of the
abstract methods in the superclass, or be itself declared abstract.

50
Example

51
07
Using final
with
Inheritance

52
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.

53
Using final to Prevent Overriding

❏ 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.

54
Example
❏ Because meth( ) is declared
as final, it cannot be
overridden in B.
❏ If you attempt to do so, a
compile-time error will
result.

55
Using final to Prevent Inheritance

❏ To prevent a class from being inherited.


❏ Precede the class declaration with final.
❏ Declaring a class as final implicitly declares all of its methods as final,
too.
❏ It is illegal to declare a class as both abstract and final since an abstract
class is incomplete by itself and relies upon its subclasses to provide
complete implementations.
❏ Following example, it is illegal for B to inherit A since A is declared as
final.

56
Using final to Prevent Inheritance

final class A {

// ...

// The following class is illegal.

class B extends A { // ERROR! Can't subclass A

// ...

}
57
T

Thank you!
H
A
N
K

Y
O
U

58

You might also like