• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Help - Please explain

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.......
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modify line 1 as follows:

 
Masood Alam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks... it works.

Is there any resource where I can find more info on covariant types...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


}
}
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Men call me Jim. Women look past me to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic