• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Questions

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,
Need your help, currently i am reading k&b for scjp 1.5 exam. I have come across some questions and have some doubt in it. Hope that you can clear my doubts and help me out.

1. class Top
{
public Top(String s)
{
SOP("B");
}

public Bottom2 extends Top
{
public Bottom2(String s)
{
SOP("D");
}

public static void main(String[]a)
{
new Bottom2("C");
SOP(" ");
}
}

Answer : Compilation fails bcoz The implied super() call Bottom2's constructor cannot be satisfied bcoz there isn't no arg constructor in Top. A default no arg constructor is generated if claas has no constructtor.

Can you please explain me this?
Please
refer pg 159 in kb
--------------------------------------------------------------
2. class Uber
{
static int y=2;
Uber(int x)
{
this ();
y=y*2;
}
Uber()
{
y++;
}

class Minor extends Uber
{
Minor()
{
super();
y=y+3;
}

public static void main()
{
new Minor();
Sop(y);
}
}

Answer : 9 Minor constructor makes explicit call to Ubers 1 arg constructor which makes an expilict call to this() which increments y,this returns to the 1 arg constructor which multiplies y * 2 andd then return to Minor constructor which add 3 to y.

Can someone please explain this?
------------------

3. What is covariant return can someone explain with example?

i will thankful to you all if answer these q's asap

Thanks
Dinesh
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dinesh,

Before answering the quesstions, i would recommend you to please

(1) Please use separate threads per questions if they are unrelated.

(2) Even if not the case, you can use the "Code" tags [which is nothing but pasting the java code between the "[C-O-D-E]" and "[/C-O-D-E]" tags] which will give a good look so that it will be easy to go through.

[Note: Remove the hyphen in between each letters of the "Code" element, i had to use here to prevent it to be treated as a java code]
[ December 29, 2007: Message edited by: Raghavan Muthu ]
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer : Compilation fails bcoz The implied super() call Bottom2's constructor cannot be satisfied bcoz there isn't no arg constructor in Top. A default no arg constructor is generated if claas has no constructtor.

Can you please explain me this?



For your Q1, Every class IS provided a default constructor, unless it does NOT have one of its own. If you have your own, that is called as explicit constructor since it was explicitly defined by the programmer. If you do NOT have one of that kind, the compiler inserts one for you. that's called an Implicit Constructor.

The implicit/default constructor will be a no-arg constructor. In your case, the program has a overloaded one argument constructor [the one that takes a String argument as a parameter].

As such, your class Bottom2 extends the base class "Top" and inside the constructor Bottom2 class, you should have a call to the base class via "super()". If you leave it, the compiler puts it by default unless you have a "this()". But the compiler tries to put the "super()" for a no-arg constructor. In this case, the base class "Top" class does NOT have a matching no-arg constructor as it has already got a one-argument overloaded constructor (reason as already explained above).

That's why the compiler error. Got it?

How to resolve this error? Just think of it how you can do. There are two ways as such. If you are struck somewhere, post it here and we can discuss it later.
[ December 29, 2007: Message edited by: Raghavan Muthu ]
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer : 9 Minor constructor makes explicit call to Ubers 1 arg constructor which makes an expilict call to this() which increments y,this returns to the 1 arg constructor which multiplies y * 2 andd then return to Minor constructor which add 3 to y.



(1) Your program code has some errors. The class Uber is not properly finished.

(2) Your main method does NOT have a proper signature. Check out. If you do NOT correct that, you will get a "java.lang.NoSuchMethodError" when you run the program.

The one argument constructor of the class Uber does NOT get invoked at all.. Check that out.

The answer will be 6 and NOT 9. The explanation you have pasted does NOT match with the program execution flow. I think you have missed out some portion of code.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


3. What is covariant return can someone explain with example?



Covariant return types are introduced from Java 5.0 version. That means a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass. This feature removes the need for excessive type checking and casting.

This link will explain more with an example.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

I have made change to the code

ublic class top
{
public top(String s)
{
System.out.println("B");
}
public class Bottom2 extends top
{

public Bottom2(String s)
{
super(s);
System.out.println("D");


}

public static void main(String[] args)
{

//System.out.println(" ");
new Bottom2("C");
}
}
}

But getting error as " No enclosing instance of top is accessible. Must qualify the allocation with enclosing instance of top.

Can someone suggest.

Thanks
Dinesh
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you post one or more questions, please use a meaningful subject line instead of just "Question" or "Hello".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic