Multilevel Inheritance in java with public variables and parameterized Constructor

This is a demonstration of Multi-level inheritance in java using parameterised constructor
package com.inheritance;

public class Class1 {
public int a;
public int b;
public Class1(int a, int b) {
super();
this.a = a;
this.b = b;
System.out.println("I am super class called first--->"+(a+b));
System.out.println(a+b);
}
}
---------------------------------------------
package com.inheritance;
public class Class2 extends Class1 {
public int c;
public Class2(int a, int b, int c) {
super(a, b);
this.c=c;
System.out.println("I am the second level subclass--->" +a%c);
}

}
--------------------------------------------------------------
package com.inheritance;

public class Class3 extends Class2 {
public int d;
public Class3(int a, int b,int c, int d) {
super(a, b, c);
this.d=d;
System.out.println("I am thrid level subclass extends second level class-->"+a*b*c*d);
// TODO Auto-generated constructor stub
}
public static void main(String args[]){
Class3 class3=new Class3(1, 2, 3, 4);
}
}
--------------------------------------------
output:
I am super class called first--->3
3
I am the second level subclass--->1
I am thrid level subclass extends second level class-->24

Comments

  1. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon. Slub injected Melange

    ReplyDelete

Post a Comment

Popular posts from this blog

Spring MVC- Redirect from one controller to another

Jquery Table Inline Edititing