GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help - Please explain 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 "Help - Please explain" Watch "Help - Please explain" New topic
Author

Help - Please explain

Masood Alam
Greenhorn

Joined: Jul 12, 2007
Posts: 8
Hi All,

The following code is not compiling and the message that I am getting is that "incompatible types" ie at Line 1

"String str = se.doStuff(); //////Line 1"

If am right the object reference "se" should be invoking the method in Class "Second"

I now Iam missing something here, please explain

Thanks in advance

/******************* CODE *******************************/
class Second extends First{

public static void main(String[] args){

First se = new Second();

String str = se.doStuff(); //////Line 1

System.out.println("Printing from main :" + str);

}

public String doStuff(){
System.out.println("Derived");
return new String("Derived");

}

}//close class second


class First {

public Object doStuff(){

System.out.println("Base");
return new String("Base");
}//close doStuff

}//close class First
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
well!!
Actualy what is happening here is that in the First class the method name is doStuff() and in the Second class which extends the First also has the method doStuff but the return types of both the methods are not same hence here overriding is not possible that is it does not invoke the method of class Second and you are getting error incompatible types......

You make the return type of both the methods same than i think you should not get error......i hope it helps.......
Murali Kakarla
Ranch Hand

Joined: Jul 11, 2007
Posts: 80
It is perfect example of overriding from java 5 onwards. This is a special case of overriding and it is called as "covariant types". Please refer to SCJP 5 book by Kathy and Bates.

You are getting compilation error at line 1 because doStuff() method of (First se) returns String. Hence you need to explicitly cast it to String.

Modify line 1 as follows:


At runtime it will call doStuff() method of sub-class as the actual object is of sub-class type.

Murali...
Murali Kakarla
Ranch Hand

Joined: Jul 11, 2007
Posts: 80
Modify line 1 as follows:

Masood Alam
Greenhorn

Joined: Jul 12, 2007
Posts: 8
Hey thanks... it works.

Is there any resource where I can find more info on covariant types...
Amit Kumar Mahajan
Greenhorn

Joined: Jul 09, 2007
Posts: 1
Adding to what Murali said , at compile time the compiler sees that your reference type is First and it's doStuff method returns an Object. Hence it gives a compiler error saying "Incompatible types"
It's only at runtime the objects get resolved.
Masood Alam
Greenhorn

Joined: Jul 12, 2007
Posts: 8
Here is another program, which is confusing...

At compile time c1(reference type) will see its method getObject which returns type A, however according to overriding at runtime the binding will happen with the object on right side ( new subcov() //Line 1) and it has the method getObject which returns type B

However when I run the code the output is "5"... //Line 2

Please explain....
/***************************CODE**************************************/
class A{
int x = 5;
}


class B extends A{
int x = 6;
}


class subcov extends cov2{

public B getObject(){
return new B();
}


}


class cov2{

public A getObject(){
return new A();
}

public static void main(String[] args)
{

cov2 c1 = new subcov(); //Line 1
System.out.println(c1.getObject().x); //Line 2


}
}
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
I believe above code should print 6 as variable 'x' is called from object of class B, but actual output is 5,

Can anyone please explain why is it so?


SCJP 5.0, SCWCD in progress
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Mohit Jain:
Can anyone please explain why is it so?
Variables are selected at compile time, based on the reference type.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok Manfred
i agree variables are selected at compile time based on reference type but in the below shown statement

cov2 c1 = new subcov(); //Line 1

c1 is reference of cov2 but it is object of subcov() ,so i think the method of class subcov will be called,later in print statement
shown below


System.out.println(c1.getObject().x); //Line 2


here the value of x in B must be called ,ie the value of x should be 6 rather then 5.

Then what can be the reason of x value being 5?

i am getting confused.............
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by dhwani mathur:
c1 is reference of cov2 but it is object of subcov()
But the compiler doesn't know that. All he knows is that cov2.getObject() returns an A.
 
GeeCON Prague 2014
 
subject: Help - Please explain