Unit 4
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. */
Contents of subOb:
i and j: 7 8
k: 9
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
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;
}
}
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:
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;
}
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