Content
1. Simple Inheritance example
public class SimpleInheritance{
public static void main(String[] args) {
SubClass ob1 = new SubClass();
ob1.showmsgSuper();
ob1.showmsgSub();
}
}
class SuperClass
{
int i,j,k, ans;
void showmsgSuper()
{
System.out.println("This msg from Super class..!");
}
}
class SubClass extends SuperClass
{
void showmsgSub()
{
System.out.println("This msg from Sub class..!");
}
}
Output:
This msg from Super class..!
This msg from Sub class..!
2. Simple Inheritance example2
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 InheritanceSimple
{
public static void main(String args[ ])
{
A sup = new A();
B sub = new B();
sup.i = 10;
sup.j = 20;
sup.showij();
sub.i = 7;
sub.j = 8;
sub.k = 9;
sub.showij();
sub.showk();
sub.sum();
}
}
Output:
i and j: 10 20
i and j: 7 8
k:9
i+j+k: 24
3. Program to explain Multilevel Inheritance.
class Box
{
private double width;
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;
height = -1;
depth = -1;
}
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);
weight = m;
}
BoxWeight()
{
super();
weight = -1;
}
BoxWeight(double len, double m)
{
super(len);
weight = m;
}
}
class BoxCost extends BoxWeight
{
double cost;
BoxCost(BoxCost ob)
{
super(ob);
cost = ob.cost;
}
BoxCost(double w, double h, double d, double m, double c)
{
super(w, h, d, m);
cost = c;
}
BoxCost()
{
super();
cost = -1;
}
BoxCost(double len, double m, double c)
{
super(len, m);
cost = c;
}
}
class InheritanceMultilevelBox
{
public static void main(String args[ ])
{
BoxCost b1 = new BoxCost(10, 15, 20, 25.75, 50.5);
BoxCost b2 = new BoxCost(2, 3, 4, 5.6, 10.5);
double vol;
vol = b1.volume();
System.out.println("Volume of b1 is " + vol);
System.out.println("Weight of b1 is " + b1.weight);
System.out.println("Cost: Rs. " + b1.cost);
System.out.println();
vol = b2.volume();
System.out.println("Volume of b2 is " + vol);
System.out.println("Weight of b2 is " + b2.weight);
System.out.println("Cost: Rs. " + b2.cost);
System.out.println();
}
}
Output:
Volume of b1 is 3000.0
Weight of b1 is 25.75
Cost: Rs. 50.5
Volume of b2 is 24.0
Weight of b2 is 5.6
Cost: Rs. 10.5
4. Program to use this keyword
class n {
public int i;
int getI() {
return i;
}
}
class M extends n {
int a, b;
void sum(int a, int b) {
int d = getI();
this.a = a;
this.b = b;
System.out.println("Sum is : "+(d + a + b));
}
}
public class ThisKeyword{
public static void main(String[] args) {
M k = new M();
k.i = 10;
k.sum(4, 5);
}
}
Output:
Sum is : 19
5. Program to use super keyword to call Super class constructor to initialize super class instance variables
class Box
{
private double width;
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;
height = 1;
depth = 1;
}
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);
weight = m;
}
BoxWeight()
{
super();
weight = 1;
}
BoxWeight(double len, double m)
{
super(len);
weight = m;
}
}
class BoxWeightDemo2
{
public static void main(String args[ ])
{
BoxWeight b1 = new BoxWeight(10, 15, 20, 25.75);
BoxWeight b2 = new BoxWeight(2, 3, 4, 2.25);
BoxWeight b3 = new BoxWeight();
BoxWeight b4 = new BoxWeight(5, 6);
BoxWeight b5 = new BoxWeight(b1);
double vol;
vol = b1.volume();
System.out.println("Volume of b1 is " + vol);
System.out.println("Weight of b1 is " + b1.weight);
System.out.println("");
vol = b2.volume();
System.out.println("Volume of b2 is " + vol);
System.out.println("Weight of b2 is " + b2.weight);
System.out.println("");
vol = b3.volume();
System.out.println("Volume of b3 is " + vol);
System.out.println("Weight of b3 is " + b3.weight);
System.out.println("");
vol = b4.volume();
System.out.println("Volume of b4 is " + vol);
System.out.println("Weight of b4 is " + b4.weight);
System.out.println("");
vol = b5.volume();
System.out.println("Volume of b5 is " + vol);
System.out.println("Weight of b5 is " + b5.weight);
}
}
Outout:
Volume of b1 is 3000.0
Weight of b1 is 25.75
Volume of b2 is 24.0
Weight of b2 is 2.25
Volume of b3 is 1.0
Weight of b3 is 1.0
Volume of b4 is 125.0
Weight of b4 is 6.0
Volume of b5 is 3000.0
Weight of b5 is 25.75
6. hierarchical inheritance example
class student1
{
private String name;
private int rno;
void setname(String name, int rno)
{
this.name = name;
this.rno = rno;
}
void display()
{
System.out.println("Name : "+name);
System.out.println("Roll No : "+rno);
}
}
class science1 extends student1
{
private int chem, bio, phy;
void setmarks(int a, int b, int c)
{
super.setname("student 1", 1);
chem = a;
bio = b;
phy = c;
}
void display()
{
super.display();
System.out.println("Chemistry : " +chem);
System.out.println("Biology : "+ bio);
System.out.println("Physics : " +phy+"\n");
}
}
class commerce1 extends student1
{
private int ac, bom, st;
void setmarks(int a, int b, int c)
{
super.setname("student 2", 2);
ac = a;
bom = b;
st = c;
}
void display()
{
super.display();
System.out.println("Account : "+ac);
System.out.println("BOM : "+bom);
System.out.println("ST : "+st+"\n");
}
}
class arts1 extends student1
{
private int sanskrit, psychology, philosophy;
void setmarks(int a, int b, int c)
{
super.setname("student 3", 3);
sanskrit = a;
psychology = b;
philosophy = c;
}
void display()
{
super.display();
System.out.println("Sanskrit : "+sanskrit);
System.out.println("Psychology : "+psychology);
System.out.println("Philosophy : "+philosophy+"\n");
}
}
public class HierarchicalInheritance
{
public static void main(String[] args)
{
student1 stu = new student1();
science1 sci = new science1();
commerce1 com = new commerce1();
sci.setmarks(100, 90, 99);
sci.display();
com.setmarks(87, 85, 80);
com.display();
a.setmarks(89, 93, 98);
a.display();
}
}
Output:
Name : student 1
Roll No : 1
Chemistry : 100
Biology : 90
Physics : 99
Name : student 2
Roll No : 2
Account : 87
BOM : 85
ST : 80
Name : student 3
Roll No : 3
Sanskrit : 89
Psychology : 93
Philosophy : 98
7. Program to use super to access super class instance variables when sub class instance variables hides the super class instance variables.
class A
{
int x;
}
class B extends A
{
int x; // this x hides the x in A
B(int a, int b)
{
super.x = a; // x in A
x = b; // x in B
}
void show()
{
System.out.println("x in superclass: " + super.x);
System.out.println("x in subclass: " + x);
}
}
class HideSuperClassVar
{
public static void main(String args[ ])
{
B ob = new B(1, 2);
ob.show();
}
}
Output:
x in superclass: 1
x in subclass: 2
8. Program to explain Method Overloading.
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 MethodOverriding2
{
public static void main(String args[ ])
{
B b = new B(1, 2, 3);
b.show("This is k : "); // this calls show() in B
b.show(); // this calls show() in A
}
}
Output:
This is k : 3
i and j: 1 2
9. Program to explain Method Overriding.
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()
{
System.out.println("k: " + k);
}
}
class MethodOverriding
{
public static void main(String args[ ])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
Output:
k: 3
10. Using super call Overriding Method.
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();
System.out.println("k: " + k);
}
}
class MethodOverriding1
{
public static void main(String args[ ])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
Output:
i and j: 1 2
k: 3
11. Program to explain Polymorphism or Dynamic Method Dispatch or Run Time Binding.
class Shape
{
void draw()
{
System.out.println("Drawing Shape.");
}
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Drawing Rectangle.");
}
}
class Triangle extends Shape
{
void draw()
{
System.out.println("Drawing Triangle.");
}
}
class PolymorphismTest6
{
public static void main( String args[ ] )
{
Shape s = new Shape();
s.draw();
Rectangle r = new Rectangle();
s = r;
s.draw();
Triangle t = new Triangle();
s = t;
s.draw();
}
}
Output:
Drawing Shape.
Drawing Rectangle.
Drawing Triangle.
12. Program to explain Polymorphism with abstract class.
abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Drawing Rectangle.");
}
}
class Triangle extends Shape
{
void draw()
{
System.out.println("Drawing Triangle.");
}
}
class PolymorphismTest7
{
public static void main( String args[ ] )
{
// Can't instantiate Shape class.
// Shape s = new Shape();
// s.draw();
Shape s;
Rectangle r = new Rectangle();
s = r;
s.draw();
Triangle t = new Triangle();
s = t;
s.draw();
}
}
Output:
Drawing Rectangle.
Drawing Triangle.
13. Program to explain Order of Constructor calling in Inheritance
class A
{
A()
{
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 ConstructorCalling
{
public static void main(String args[ ])
{
C c = new C();
}
}
Output:
Inside A's constructor.
Inside B's constructor.
Inside C's constructor.
14. Program to explain Calling super class constructor using super
class A
{
A()
{
System.out.println("A constructor.");
}
}
class B extends A
{
B()
{
super();
System.out.println("B constructor.");
}
}
class C extends B
{
C()
{
super();
System.out.println("C constructor.");
}
}
public class InheritanceConstructor2
{
public static void main(String[ ] args)
{
C x = new C();
}
}
Output:
A constructor.
B constructor.
C constructor.
15. Program to explain Calling super class parameterize constructor using super.
class A
{
int x;
A(int a)
{
x = a;
System.out.println("A constructor, x : " + x);
}
}
class B extends A
{
int y;
B(int a, int b)
{
super(a);
y = b;
System.out.println("B constructor, y : " + y);
}
}
class C extends B
{
int z;
C(int a, int b, int c)
{
super(a, b);
z = c;
System.out.println("C constructor, z : " + z);
}
}
public class InheritanceConstructor4
{
public static void main(String[ ] args)
{
C x = new C(10, 20, 30);
}
}
Output:
A constructor, x : 10
B constructor, y : 20
C constructor, z : 30
16. Program to explain final class restricts (not allow)
the inheritance from that class.
final class A
{
void display()
{
System.out.println(" A\'s display().");
}
}
// final class can't be inherited.
// class B extends A // Error
class B
{
void display()
{
System.out.println(" B\'s display().");
}
}
class FinalClassTest
{
public static void main( String args[ ] )
{
A a = new A();
B b = new B();
a.display();
b.display();
}
}
Output:
A's display().
B's display().
17. Program to explain final method restricts (not allow) the overriding of that method
class A
{
final void display()
{
System.out.println(" A\'s display().");
}
}
class B extends A
{
// final method can't be override.
// void display()
void show()
{
System.out.println(" B\'s display().");
}
}
class FinalMethod
{
public static void main( String args[ ] )
{
B b = new B();
b.display();
b.show();
}
}
Output:
A's display().
B's display().
1 Comments
Hi
ReplyDelete