Hello everybody, I am sorry the code is not aligned properly. I have these questions regarding this code. The output of the program is i 10 num 5 num 5 num 6 I am methodA of Sub When I do Super sup = new Sub(); 1. Why does it associate variables(num) with the reference type ie Super.When I say sup.num it prints out the num in parent class. 2. It applies the same rule with methods also, it doesn't allow me to call any mehtods which exists only in child class.See the commented call to methodB() 3. If that's so, I mean you have to have methods existing in the parent class, why doesn't it apply the same rule to constructors? I am allowed to do Super sup = new Sub() eventhough I don't have a constructor with no arguments in the parent.
I hope I am clear in my questions Thanks Sudha
[I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future. Learn more about UBB codes here - Ajith ] [This message has been edited by Ajith Kallambella (edited November 22, 2000).]
Hi, Here is the explanation. Super sup = new Sub(); Here sup refers to a new object of subclass "Sub" and is defined to be of type "Super". The variable that is accessed depends on the type and the method that is invoked depends on the Object that is refered to. Here sup is of type "Super" and so the call sup.num accesses the variable "num" of the Super class. Similarly, sup refers to the "object of Sub" and hence the call sup.methodA() invokes the methodA() of Subclass. Also the method, methodA() in Sub class overrides the methodA() of the Super class. The methodA() of Superclass can be called only by an explicit call, super.methodA(); You can invoke the methodB() of Subclass from within the subclass as mentioned below; Sub sub1 = new Sub(); sub1.methodB(); - Suresh Selvaraj
Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
1 . You have not explicitly changed the value of num declared in the base class so the original value holds. However if you had created the object as Sub sup = new Sub() then the variable you declared in class Sub becomes visible. 2. You have created an object reference sup of type Super. In other words you are telling the compiler I am creating an object reference of type Super which may come up as an instance of Super or Sub depending on the inter-action betweeen the program and the user. The base class does not have a methodB() hence it will reject your request to run methodB() on that object. 3. The compiler implicitly adds a constructor without paramters when you create an object with new Sub() because the derived class has a constructor without parameters. However you may not create an instance of Super with Super sup = new Super() because you have explicitly defined a contructor that takes an int as argument.
Joined: Oct 31, 2000
Hello friends, Thanks for the explanation, 1 st two explanation is same as what I also had in mind.But I beg to differ from the 3rd point of Mr Shah. The compiler implicitly adds a constructor without paramters when you create an object with new Sub() because the derived class has a constructor without parameters. However you may not create an instance of Super with Super sup = new Super() because you have explicitly defined a contructor that takes an int as argument.
I don't think so, if I have not given super(4) an explicit call to the superclass constructor it will add a call super() to subclass constructor. But as I have it shouldn't add & if it adds also it will be a problem as I don't have a superclass constructor without a parameter. So can anybody explain this. Thanks Sudha
Joined: Nov 05, 2000
3. If that's so, I mean you have to have methods existing in the parent class, why doesn't it apply the same rule to constructors? I am allowed to do Super sup = new Sub() eventhough I don't have a constructor with no arguments in the parent.
I am sorry, I meant implicit constructor call. Yes you do have a constructor without arguments in the parent class. You may not have added one, but yet a parent class does have a contructor without arguments. It does not have to be in the immediate parent class , it could be further up the inheritance tree. Rgds Sahir
subject: Super s = new Sub() - strange behaviours.