Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

2 questions

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All
I'm going thorugh the "Overloading and overriding" section from [http://www.jchq.net/certkey/0104certkey.htm]
where a question goes like this :-

Question 2)Given the following class definition
class Base{
public void amethod(){
System.out.println("Base");
}
}

public class Hay extends Base{

public static void main(String argv[]){
Hay h = new Hay();
h.amethod();
}

}


Which of the following methods in class Hay would compile and cause the program to print out the string "Hay"
1) public int amethod(){ System.out.println("Hay");}
2) public void amethod(long l){ System.out.println("Hay");}
3) public void amethod(){ System.out.println("Hay");}
4) public void amethod(void){ System.out.println("Hay");}

I don't have a problem with the right answer [3], but I am quizzed as to why option 2 will compile to give "base" as the output string ?

concluding:-
on [http://www.jchq.net/certkey/0103certkey.htm] a question goes like:-

Question 1)Given the following class definition
class Base{
Base(int i){}

}

class DefCon extends Base{
DefCon(int i){
//XX
}
}

Which of the following lines would be legal individually if added at the line marked //XX?
1) super();
2) this();
3) this(99);
4)super(99);

I'm okay about super()and this(), but what are these 99's ?

TIA
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Netty,

The Hay class does not override the method
amethod(), therefore being a subclass
it inherits it. If you declare the answer 3
method in the Hay class, then you will
override the Base class amethod(), and
cause the program to print out the string
"Hay".

Option 2 will compile to give "base", because
you are overloading the Base method amethod(),
and by calling
in main() you are actually calling the inherited
amethod() not the overloaded one.

With respect to question two. The classes Base and
DefCon have only one constructor with a parameter,
this means that the compiler won't create the default
parameterless constructors Base() and DefCon(),
one would have to write it explicitly in this case.

Given this constructors, the way to call it is with this(99)
or super(99), because this() and super() would not find
the corresponding constructors, since they are not created.

Cheers,

Gian Franco Casula
[ September 21, 2004: Message edited by: Gian Franco Casula ]
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Netty,

Regarding your second question, super() calls to the default consrtuctor of super class. super(99) will call to the contructor of super class that takes int as argument. Same if the case with this(99).

Hope this clears.

Regards
Sandeep Jindal
[ September 21, 2004: Message edited by: Sandeep Jindal ]
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarifications Gian and Sandeep.It helps to clarify.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic