Professional Documents
Culture Documents
Inheritance
• It is a mechanism by which one class allows to inherit the features (instance variables and
methods) of another class.
• A class that is inherited is called a superclass and the class that does the inheriting is called
a subclass.
• A subclass is a specialized version of a superclass.
• It inherits all of the instance variables and methods defined by the superclass and adds its
own, unique elements.
Syntax
class super1 { class Employee{
float salary=40000;
data members1;
Employee(){
member functions1(){ salary = 0;
} }
} }
class sub1 extends super1{
class Programmer extends Employee{
data members2;
int bonus=10000;
member functions2(){ Programmer(){
} bonus = 0;
} }
}
class demoEmployee{
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
// This program uses inheritance to extend // constructor used when no
Box. dimensions specified
class Box { Box() {
double width;
width = -1;
double height;
height = -1;
double depth;
// construct clone of an object depth = -1;
Box(Box ob) { }
width = ob.width; // constructor used when cube is
height = ob.height; created
depth = ob.depth; Box(double len) {
} width = height = depth = len;
// constructor used when all }
dimensions specified
// compute and return volume
Box(double w, double h, double d) {
double volume() {
width = w;
height = h; return width * height * depth;
depth = d; }
} }
// Here, Box is extended to include class DemoBoxWeight {
weight. public static void
main(String args[]) {
class BoxWeight extends Box { BoxWeight mybox = new
// weight of box BoxWeight(10, 20, 15, 34.3);
double weight; double vol;
vol = mybox.volume();
// constructor for BoxWeight
BoxWeight(double w, double h, System.out.println("Volume
double d, double m) { of mybox is " + vol);
width = w;
height = h; System.out.println("Weight
depth = d; of mybox is " + mybox.weight);
weight = m;
} }
} }
A superclass variable can reference a subclass object
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);
// 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);
}
}
Using super
• Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.
• This causes the Box( ) constructor to be called, which initializes width, height, and depth
using these values.
• BoxWeight no longer initializes these values itself. It only needs to initialize the value
unique to it: weight.