0% found this document useful (0 votes)
56 views47 pages

Unit 4

The document discusses inheritance in Java. It defines inheritance as a form of code reuse where a subclass inherits attributes and behaviors from its parent superclass. The key points are: - A subclass extends and inherits from a superclass, allowing code reuse and specialization. - Subclasses can add their own fields and methods while retaining access to public and protected members of the superclass. - Java only supports single inheritance, where a class can inherit from one direct superclass. - Private members of a superclass cannot be accessed by subclasses. Protected members have intermediate access between public and private.

Uploaded by

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

Unit 4

The document discusses inheritance in Java. It defines inheritance as a form of code reuse where a subclass inherits attributes and behaviors from its parent superclass. The key points are: - A subclass extends and inherits from a superclass, allowing code reuse and specialization. - Subclasses can add their own fields and methods while retaining access to public and protected members of the superclass. - Java only supports single inheritance, where a class can inherit from one direct superclass. - Private members of a superclass cannot be accessed by subclasses. Protected members have intermediate access between public and private.

Uploaded by

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

Unit – 4

Inheritance
Introduction
• one of its primary capabilities—inheritance 🡪 form of software
reuse
• a new class is created by absorbing an existing class’s members
and embellishing them with new or modified capabilities.
• With inheritance, you can save time
• Also system will be implemented and maintained effectively.
• When creating a class, rather than declaring completely new
members, you can designate that the new class should inherit
the members of an existing class.
• The existing class is called the superclass, and the newclass is
the subclass. base class and the subclass as the derived class.
• Super class = base class = parent class
• Sub class = derived class = child class
• Each subclass can become a superclass for future subclasses.
• A subclass can add its own fields and methods. Therefore, a
subclass is more specific than its superclass and represents a
more specialized group of objects.
• The subclass exhibits the behaviors of its superclass and can
modify those behaviors so that they operate appropriately for the
subclass.
• This is why inheritance is sometimes referred to as
specialization.
• In Java, the class hierarchy begins with class Object (in package
java.lang), which every class in Java directly or directly extends
(or “inherits from”).
• Java supports only single inheritance, in which each class is
derived from exactly one direct superclass. Unlike C++, Java
does not support multiple inheritance (which occurs when a class
is derived from more than one direct superclass).
• In java, solution for multiple inheritance is interface
• Some day, most new software likely will be constructed from
standardized reusable components, just as automobiles and
most computer hardware are constructed today.
• This will facilitate the development of more powerful, abundant
and economical software.
Superclasses and Subclasses
• Super classes tend to be “more general”
• Sub classes is “more specific.”
• Because every subclass object is an object of its superclass,
and one superclass can have many subclasses,
• For example, the superclass Vehicle represents all vehicles,
including cars, trucks, boats, bicycles and so on. By contrast,
subclass Car represents a smaller, more specific subset of
vehicles.
University

Shape
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}

class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
class SimpleInheritance {
public static void main(String args []) {
A superOb = new A();
// 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. */

B subOb = new B();


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();
}
}
Output
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
Member access and Inheritance
/* In a class hierarchy, private members remain private to their class.
This program contains an error and will not compile.
*/

class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}
// 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);
}
}
Output
Access.java:19: error: j has private access in A
total = i + j; // ERROR, j is not accessible here
^
1 error

• 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.
• Above example show this error :
class Box {
Program
double width, height, depth;
Box(Box ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1;
height = -1;
depth = -1;
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}

class BoxWeight extends Box {


double weight;
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
class DemoBoxWeight {
public static void main(String args[ ]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
}
}
Output:

Volume of mybox1 is 3000.0


Weight of mybox1 is 34.3

Volume of mybox2 is 24.0


Weight of mybox2 is 0.076
class Box {
Program
double width, height, depth;
Box(Box ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1;
height = -1;
depth = -1;
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}

class BoxWeight extends Box {


double weight;
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
class RefDemo {
public static void main(String args[]) {
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();
double 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);
}
}
Output:

Volume of weightbox is 105.0


Weight of weightbox is 8.37

Volume of plainbox is 105.0


protected Members
• A class’s
✔ public members are accessible wherever the program has a
reference to an object of that class or one of its subclasses.
✔ private members are accessible only within the class itself.
✔ protected access offers an intermediate level of access
between public and private.
• A superclass’s protected members can be accessed by
members of that superclass, by members of its subclasses
and by members of other classes in the same package—
protected members also have package access.
• All public and protected superclass members retain their
original access modifier when they become members of the
subclass
• Subclass methods can refer to public and protected members
inherited from the superclass simply by using the member
names.
• When a subclass method overrides an inherited superclass
method, the superclass method can be accessed from the
subclass by preceding the superclass method name with
keyword super and a dot (.) separator.
class Box {
private double width;
Program
private double height;
private double depth;
Box(Box ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}

class BoxWeight extends Box {


double weight;
BoxWeight(BoxWeight ob) {
super(ob);
weight = ob.weight;
}
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
BoxWeight() {
super();
weight = -1;
}
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
class DemoSuper {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
BoxWeight mybox3 = new BoxWeight(); // default
BoxWeight mycube = new BoxWeight(3, 2);
BoxWeight myclone = new BoxWeight(mybox1);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
vol = mybox3.volume();
System.out.println("Volume of mybox3 is " + vol);
System.out.println("Weight of mybox3 is " + mybox3.weight);
System.out.println();
vol = myclone.volume();
System.out.println("Volume of myclone is " + vol);
System.out.println("Weight of myclone is " + myclone.weight);
System.out.println();
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
System.out.println("Weight of mycube is " + mycube.weight);
System.out.println();
}
}
Output
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3

Volume of mybox2 is 24.0


Weight of mybox2 is 0.076

Volume of mybox3 is -1.0


Weight of mybox3 is -1.0

Volume of myclone is 3000.0


Weight of myclone is 34.3

Volume of mycube is 27.0


Weight of mycube is 2.0
class A {
int i;
Program
}
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);
Systemout.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
Creating a Multilevel Hierarchy

Box

BoxWeight

Shipment
class Box {
private double width;
Program
private double height;
private double depth;
Box(Box ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}

class BoxWeight extends Box {


double weight;
BoxWeight(BoxWeight ob) {
super(ob);
weight = ob.weight;
}
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
BoxWeight() {
super();
weight = -1;
}
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
class Shipment extends BoxWeight {
double cost;
Shipment(Shipment ob) {
super(ob);
cost = ob.cost;
}
Shipment(double w, double h, double d, double m, double c) {
super(w, h, d, m);
cost = c;
}
Shipment() {
super();
cost = -1;
}
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);
Shipment shipment2 = new Shipment(2, 3, 4, 0.76, 1.28);
double vol;

vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is "+ shipment1.weight);
System.out.println("Shipping cost: $" + shipment1.cost);
System.out.println();

vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is "+ shipment2.weight);
System.out.println("Shipping cost: $" + shipment2.cost);
}
}
Output:

Volume of shipment1 is 3000.0


Weight of shipment1 is 10.0
Shipping cost: $3.41

Volume of shipment2 is 24.0


Weight of shipment2 is 0.76
Shipping cost: $1.28
When Constructors Are Executed
• In a class hierarchy, constructors complete their execution in
order of derivation, from superclass to subclass.
• Further, since super( ) must be the first statement executed in
a subclass’ constructor, this order is the same whether or not
super( ) is used.
– If super( ) is not used, then the default or parameterless
constructor of each superclass will be executed.
• following program illustrates when constructors are executed:
class A {
A() { Program
System.out.println("Inside A's constructor.");
}
}
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}
Output:
Inside A's constructor.
Inside B's constructor.
Inside C's constructor.
Method Overriding
class A {
int i, j;
Program
A(int a, int b) {
i = a;
j = b;
}
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;
}
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
}
}

Output

k: 3
class A {
int i, j;
Program
A(int a, int b) {
i = a;
j = b;
}
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;
}

void show(String msg) {


System.out.println(msg + k);
}
}

class Override1 {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
}
Output

This is k: 3
i and j: 1 2

You might also like