0% found this document useful (0 votes)
2 views33 pages

Basics of Java Unit-4

The document provides an overview of Java programming concepts related to inheritance, including class creation, member access, and method overriding. It discusses the use of the 'super' keyword, dynamic method dispatch, and abstract classes, as well as the final modifier to prevent method overriding and inheritance. Additionally, it highlights the Object class as the superclass for all Java classes.
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)
2 views33 pages

Basics of Java Unit-4

The document provides an overview of Java programming concepts related to inheritance, including class creation, member access, and method overriding. It discusses the use of the 'super' keyword, dynamic method dispatch, and abstract classes, as well as the final modifier to prevent method overriding and inheritance. Additionally, it highlights the Object class as the superclass for all Java classes.
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/ 33

Basics of Java Programming

Unit-IV
22PLC25C

K.S.Mathad
Inheritance Basics
• To inherit a class, you simply incorporate the definition of one class into another by
using the extends keyword.
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
K.S.Mathad
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A()
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
/* The subclass has access to all public members of
its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
K.S.Mathad
The output from this program is shown here:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24

K.S.Mathad
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.

K.S.Mathad
/* 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;
}
}

K.S.Mathad
// A's j is not accessible here.
class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total);
}
}
K.S.Mathad
// This program uses inheritance to extend Box.
class Box {
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
K.S.Mathad
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}

// compute and return volume


double volume() {
return width * height * depth;
}
}
// Here, Box is extended to include weight.
class BoxWeight extends Box {
double weight; // weight of box
// constructor for BoxWeight
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
} K.S.Mathad
A Superclass Variable Can Reference a Subclass Object
• A reference variable of a superclass can be assigned a reference to any subclass
derived from that superclass.
class RefDemo {
public static void main(String args[]) {
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();
double vol;
vol = weightbox.volume();
System.out.println("Volume of weightbox is " + vol);
System.out.println("Weight of weightbox is " +
weightbox.weight);
System.out.println();
// assign BoxWeight reference to Box reference
plainbox = weightbox;
vol = plainbox.volume(); // OK, volume() defined in Box
System.out.println("Volume of plainbox is " + vol);
/* The following statement is invalid because plainbox
does not define a weight member. */
// System.out.println("Weight of plainbox is " + plainbox.weight);
}
}
K.S.Mathad
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.

K.S.Mathad
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);

K.S.Mathad
// 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;
}
}

K.S.Mathad
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.

K.S.Mathad
// Using super to overcome name hiding.
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();
}
}
K.S.Mathad
Creating a Multilevel Hierarchy
class Shipment extends BoxWeight {
double cost;
// construct clone of an object
Shipment(Shipment ob) { // pass object to constructor
super(ob);
cost = ob.cost;
}
// constructor when all parameters are specified
Shipment(double w, double h, double d,
double m, double c) {
super(w, h, d, m); // call superclass constructor
cost = c;
}
// default constructor
Shipment() {
super();
cost = -1;
} K.S.Mathad
// constructor used when cube is created
Shipment(double len, double m, double c) {
super(len, m);
cost = c;
}
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 = new Shipment(10, 20, 15, 10,
3.41);
double vol;
vol = shipment1.volume();
}
} K.S.Mathad
When Constructors Are Called
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
// Create another subclass by extending B.
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C(); K.S.Mathad
}
o/p
The output from this program is shown here:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor

K.S.Mathad
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.

K.S.Mathad
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
} K.S.Mathad
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}

K.S.Mathad
• 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.

K.S.Mathad
// Dynamic Method Dispatch
class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}

K.S.Mathad
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); // calls C's version of callme
}
}

K.S.Mathad
o/p
Inside A’s callme method
Inside B’s callme method
Inside C’s callme method

K.S.Mathad
Using Abstract Classes
• There are situations in which you will want to
define a superclass that declares the structure of
a given abstraction without providing a complete
implementation of every method.
• sometimes you will want to create a superclass
that only defines a generalized form that will be
shared by all of its subclasses, leaving it to each
subclass to fill in the details.
• To declare an abstract method, use this general
form:
abstract type name(parameter-list);
K.S.Mathad
// A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract classes
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
class B extends A {
void callme() {
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
} K.S.Mathad
Using final with Inheritance
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.

K.S.Mathad
class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() { // ERROR! Can't override
System.out.println("Illegal!");
}
}

K.S.Mathad
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.
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}

K.S.Mathad
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.

K.S.Mathad
K.S.Mathad

You might also like