CLASSES AND OBJECTS
INHERITANCE
Access Protection
Inheritance
A subclass is a specialized version of a superclass.
A subclass inherits all the instance variables and methods from the
superclass and adds its own unique elements.
Inheritance is facilitated in Java with the help of the keyword
extends.
1 Single Inheritance
Super class subclass
Parent class child class
Based class derived class
2 Multilevel Inheritance
3 Hierarchical Inheritance
4 Multiple Inheritance
Not supported in java
class subclass-name extends superclass-name
{
// body of class
}
class A
{
class B extends A
{
}
class MyMessage1
class SimpleInheritance
{
{ public static void main(String args[])
void dispMessage1()
{
{
MyMessage2 m2 = new MyMessage2();
System.out.println(“Wel come”);
m2.dispMessage1();
}
m2.dispMessge2()
}
}
class MyMessage2 extends MyMessage1
}
{
void dispMessage2()
{
System.out.println(“MIT, Manipal”);
}
}
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[])
{
B subOb = new B();
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
subOb.sum();
}
}
Member Access and Inheritance
derived cannot access members of the superclass that have
been declared as private.
/* private members remain private to their class */
class A
{ int i;
private int j; // private to A
void setij(int x, int y)
{ i = x;
j = y;
}
}
class B extends A
{ int total;
void sum()
{ total = i + j; // ERROR, j is not accessible here
}
}
//This program uses inheritance to extend Box.
class Box {
double width;
double height;
double depth;
Box(Box ob) // construct clone of an object
{ 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
}
// constructor used when cube is created
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);
}}
//Box is extended to include color
class ColorBox extends Box
{
int color; //color of Box
ColorBox(double w, double h, double d, int c)
{ width = w;
height = h;
depth = d;
color = c;
}
}
A superclass variable can reference a subclass object
• The type of the reference variable (not the type of the object) determines what members can
be accessed.
• when a reference to a subclass object is assigned to a superclass reference variable, we will
have access to only those parts of the object defined by the superclass.
A superclass variable can reference a subclass object
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();
double vol;
plainbox = weightbox;
vol = plainbox.volume();
System.out.println("Volume of plainbox is " + vol);
//following is invaild because plainbox does not define a weight member.
//System.out.println(“Weight of plainbox is " + plainbox.weight);
In the previous example,
weightbox is a reference to BoxWeight objects and plainbox is a reference to Box objects.
Since BoxWeight is a subclass of Box, it is permissible to assign plainbox a reference to the
weightbox object.
The type of the reference variable (not the type of the object) determines what members can
be accessed.
ie, when a reference to a subclass object is assigned to a superclass reference variable, we
will have access to only those parts of the object defined by the superclass.
Hence plainbox can’t access weight even when it refers to a BoxWeight object.
When Constructors Are Called
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.");
}
}
class C extends B
{ C()
{ System.out.println("Inside C's constructor.");
}
}
class CallingCons
{
public static void main(String args[])
{
C c = new C();
}
}
The output from this program is shown here:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor
METHOD OVERRIDING
// 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);
}
}
class Override
{
public static void main(String args[])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
Using super
When a subclass needs to refer to its immediate superclass, it can do so by use
of the keyword super.
super has two general forms.
• To call the superclass’ constructor.
• 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 method defined by its superclass by use of the
following form of super:
super(parameter-list);
Here, parameter-list specifies any parameters needed by the constructor in the
superclass.
super( ) must always be the first statement executed inside a subclass’ constructor.
// 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;
}
}
BoxWeight(double w, double h, double d, double m)
{
width = w;
height = h;
depth = d;
weight = m;
}
A Second Use for super
The second is used to access a member of the superclass that has been hidden by a
member of a subclass.
This usage has the following general form:
super.member
Here, member can be either a method or an instance variable.
applicable to situations in which member names of a subclass hide members by
the same name in the superclass.
class A
{ int i;
}
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();
}
}
Accessing superclass version of an overridden method using super
If we wish to access the superclass version of an overridden function, we can do This
by using super.
class A
{ int i, j;
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()
{ super.show(); // this calls A's show()
System.out.println("k: " + k); } }
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.
// Methods with differing type signatures are overloaded – not overridden.
class A
{ int i, j;
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 Override
{
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
}
}