KEMBAR78
Module 3 | PDF | Inheritance (Object Oriented Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
4 views10 pages

Module 3

Uploaded by

sreelathatikonda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Module 3

Uploaded by

sreelathatikonda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Object Oriented Programming with JAVA(BCS306A) Module 3

Inheritance Basics
To inherit a class, you simply incorporate the definition of one class into another by using the
extends keyword.

.IN
C
N
SY
U
VT

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 1


Object Oriented Programming with JAVA(BCS306A) Module 3

The general form of a class declaration that inherits a superclass is shown here
class subclass-name extends superclass-name
{ // body of class
}

.IN
C
N
SY
U
VT

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 2


Object Oriented Programming with JAVA(BCS306A) Module 3

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.
This program contains an error and will not
compile.
*/
// Create a superclass.
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}

.IN
// A's j is not accessible here.
class B extends A {
int total;
void sum() {
C
total = i + j; // ERROR, j is not accessible here
}
N
}
class Access {
SY

public static void main(String args[]) {


B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
U

System.out.println("Total is " + subOb.total);


}
VT

Using super
Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword 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.
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

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 3


Object Oriented Programming with JAVA(BCS306A) Module 3

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

.IN
// Using super to overcome name hiding
class A {
int i;
}
C
class B extends A {
int i; // this i hides the i in A
N
B(int a, int b) {
super.i = a; // i in A
SY

i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
U

System.out.println("i in subclass: " + i);


}
VT

}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
This program displays the following:
i in superclass: 1
i in subclass: 2.

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 4


Object Oriented Programming with JAVA(BCS306A) Module 3

Creating a Multilevel Hierarchy


Given three classes called A, B, and C, C can be a subclass of B, which is a subclass of A.
When this type of situation occurs, each subclass inherits all of the traits found in all of its
superclasses. In this case, C inherits all aspects of B and A
(refer textbook for example)
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

.IN
C
N
SY
U
VT

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.

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 5


Object Oriented Programming with JAVA(BCS306A) Module 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.

.IN
C
N
SY
U
VT

This program creates one superclass called A and two subclasses of it, called B and C.
Subclasses B and C override callme( ) declared in A. 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. Had it been determined by the type of the
reference variable, r, you would see three calls to A’s callme( ) method

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 6


Object Oriented Programming with JAVA(BCS306A) Module 3

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. That is, an
abstract class cannot be directly instantiated with the new operator

.IN
C
N
Using final with Inheritance
SY

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
 Using final to Prevent Overriding
U

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
VT

overridden, specify final as a modifier at the start of its declaration. Methods declared
as final cannot be overridden

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

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 7


Object Oriented Programming with JAVA(BCS306A) Module 3

The Object Class


There is one special class, Object, defined by Java. All other classes are subclasses of Object.
That is, Object is a superclass of all other classes.

.IN
C
N
SY
U

Interfaces
VT

Using the keyword interface, you can fully abstract a class’ interface from its
implementation. That is, using interface, you can specify what a class must do, but not how it
does it. Once it is defined, any number of classes can implement an interface. Also, one class
can implement any number of interfaces.

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 8


Object Oriented Programming with JAVA(BCS306A) Module 3

If a class implements more than one interface, the interfaces are separated with a comma.

.IN
C
N
SY
U
VT

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 9


Object Oriented Programming with JAVA(BCS306A) Module 3

.IN
C
N
SY
U
VT

Sharon Dsouza, Assistant Professor, Dept. of CSE, AJIET, Mangaluru 10

You might also like