I have learned that object should be created something like this Class_name Object_name=new Classname(). In my program, I created a super class named sup and a derived class named der. In my subclass constructor, I have created a object like this sup obc = new der();. Without any errors it compiled and gave output like this:
In Superclass with object passed as reference In Superclass with no constructor In derived class with no constructor I did not understand how I got the output in this order. Why does this happen? Here is the complete code:
class sup { private int a, b, c; sup(sup ob) { System.out.println("In Superclass with object passed as reference"); a = ob.a + 9; b = ob.b + 9; } sup(int a, int b) { this.a = a; this.b = b; } sup() { System.out.println("In Superclass with no constructor"); } } class der extends sup { int d; der(der ob) { super(ob); sup obc = new der(); } der() { System.out.println("In derived class with no constructor"); } der(int a, int b, int c) { super(a, b); d = c; } } public class Test { public static void main(String args[]) { der ob1 = new der(3, 4, 5); der ob2 = new der(ob1); } }
PascalCasefor class names, reservingcamelCasefor variables. Further, please format your code before posting.