• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructors.

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
Hope we all are enjoying java,
Question 1
Well my quest is, the this() call invokest the constructor with the corresponding parameter list.
Please ellaborate the idea. I think the decision is made at the time when we are instantiating the constructor.
Question 2
Given the Code below is in file SubClass.java, What is the result of compiling and running SubClass.java
class BaseClass{
public BaseClass(String s){
System.out.println(s);
}
public BaseClass(int i){
this("I am BaseClass Integer");
}
}
public class SubClass extends BaseClass{
public SubClass(int i){
System.out.println("I am SubClass Integer");
}
public static void main(String[] arg){
SubClass sc = new SubClass();
}
}
A.Does't Compile
B.Compiles but generates runtime error
C.Compiles & runs successfully and the output is :
I am SubClass Integer
I am BaseClass Integer
D.Compiles & runs successfully and the output is :
I am BaseClass Integer
I am SubClass Integer
Answer is not available please suggest the most appropriate answer.
Thanx in advance.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ans2. It will not compile as there is no default constructor for SubClass.
so the correct answer is 'A'
Ans1.
I also think that this([param]) is called when we try to instantiate an object.(if there is call to this() in the respective constructor.)
correct me if wrong.
------------------
Regards
Ravish
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About second question -
Answer is A, without a doubt it will not compile. There's no default constructor in the subclass.
- Manish
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nisheeth Kaushal:

Question 2
Given the Code below is in file SubClass.java, What is the result of compiling and running SubClass.java
[snip]
Answer is not available please suggest the most appropriate answer.
Thanx in advance.


Actually, the answer is available. In fact, it's pretty easy to figure out. Just take the source code, put it in a file (SubClass.java), and compile it. Then see what happens. If it compiles, then run it.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill is right...
the compiler is your friend...
the compiler is your master...
use it, and when there is something you can't understand we will try to put you right.
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i got it Jose.
Anyone interested in discussing synchronization here.
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic