• 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

Overriding and Co-variant returns doubt

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,



Consider the above program..it gives a compiler error as follows

test4.java:6: incompatible types
found : java.lang.Object
required: car
car c = a.go();
^
1 error



see reference is of type animal and object is of type dog, so at runtime dog's go() will be invoked and its returning a proper car object( considering the fact that car implicitly extends Object and thus acts as a co-variant return )... the why us this error is coming, also if I change the refernce type to dog then the its complies fine and executes properly...

Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja

[ May 28, 2007: Message edited by: Hardik Raja ]
[ May 28, 2007: Message edited by: Hardik Raja ]
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
DownCast the return to car type.
see, basically you are calling animal's go method.
In overriding the return type should be same.
try like this:

public static void main(String[] args)
{
animal a = new dog(); //dog a = new dog(); then no error
car c =(car) a.go();
c.hi();
}
}
You are getting an Object class's reference not car class. you should downcast it to car object.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
class animal
{
public Object go()
{

return new Object();
}
}

class dog extends animal
{
public car go()
{

return new car();
}
}
[code]
----------------------------------------------------------------
You are not overriding the method properly
Just go through the rules of overriding
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi hardik,


animal a = new dog(); //dog a = new dog(); then no error
car c = a.go();




When the program compiles the compiler looks for the reference type. Since the reference type is of Animal and the method in class Animal returns Object compile complains. So, cast it then it will work fine.



Regards
Nik
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i think thats absolutely right!!!
just cast the car object.Just go through the below program ,i got the output.



public class Test4 {


public static void main(String[] args) {
animal a = new dog(); //dog a = new dog(); then noerror
car c = (car) a.go();
c.hi();
}

}

class animal {
public Object go()
{
return new Object();

}
}


class dog extends animal
{
public Object go()
{
return new car();
}
}
class car
{
public void hi()
{
System.out.println("hi() of car");

}
}




output :


hi() of car


 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ravi,

 
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
Hi hardik!!
now i am bit confused....
as per the code shown by you,i tried compiling but i am getting an "error" because the return types of" class animal and class dog 's method go()" are different they must be same ie Object.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello dhwani,

as per the code shown by you,i tried compiling but i am getting an "error" because the return types of" class animal and class dog 's method go()" are different they must be same ie Object.



Well I feel you are using an older version of JAVA, as this feature is called co-variant returns and is added to java language as of ver 1.5 or java 5...
 
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
Hey Thanks for clearing my doubt!!
But one more question to ask is can you please ellaborate and explain the concept of Co-variant returns.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dhwani,
Check the code below

class Animal
{
public Animal display()
{
return new Animal();
}
}
class Dog extends Animal
{
public Dog display()
{
return new Dog();
}

}

In the Aninal class the display method returns Animal. So, the class which overrides the method display can return any sub-type of the Animal. So in the dog class the display method is overridden and Dog
is returned.

Regards
Nik
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at following links:

Bridge Method


Brige Method/ Covariant Return


Thanks,
 
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
Hey Nik thanksss i got the concept!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic