aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions" Watch "Questions" New topic
Author

Questions

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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


Thanks<br />Dinesh
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355


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

Joined: Apr 20, 2006
Posts: 3355


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

Joined: Apr 20, 2006
Posts: 3355


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

Joined: Aug 06, 2007
Posts: 486
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

When you post one or more questions, please use a meaningful subject line instead of just "Question" or "Hello".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions