• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

An Error In Program.

 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Foo
{
public void doFooStuff()
{
System.out.println("All Foo stuff");
}
}

class Bar extends Foo
{
public void doBarStuff()
{
System.out.println("All Bar Stuff !");
}
}

class TestFooBar
{
public static void main(String[] args)
{
Foo f=new Bar(); //legal as bar is the subclass of foo
f.doBarStuff();

}
}

The above Foo and Bar classes have compiled but the "TestFooBar" class is not compiling and showing the following error , what can be the error?


TestFooBar.java:8: cannot find
symbol : method doBarStuff()
location: class Foo
f.doBarStuff();
^
1 error
[ August 19, 2008: Message edited by: sri dev ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have doBarStuff() method in Foo class
 
Pradeep Balasubramanian
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above form of declaration is correct
But you should note that the Foo class does not contain doBarStuff() method

The Bar class is extending the contents of the Foo class
Mean, the Bar class is extending the doFooStuff() method

But, the Foo class does not extends the Bar class or the doBarStuff() method mentioned in Bar Class

It works if you write

f.doFooStuff() instead of f.doBarStuff() as the Foo class contains doFooStuff() and is already inherited by Bar Class
Hence, is also not altered
It works

If you don`t want to alter the code, add a method name doBarStuff(),with void type in Foo class and make a try.




[ August 19, 2008: Message edited by: Pradeep Balasubramanian ]
 
Chinmay Kant
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sri dev:
class Foo
{
public void doFooStuff()
{
System.out.println("All Foo stuff");
}
}

class Bar extends Foo
{
public void doBarStuff()
{
System.out.println("All Bar Stuff Baby!");
}
}

class TestFooBar
{
public static void main(String[] args)
{
Foo f=new Bar(); //legal as bar is the subclass of foo
f.doBarStuff();

}
}

The above Foo and Bar classes have compiled but the "TestFooBar" class is not compiling and showing the following error , what can be the error?


TestFooBar.java:8: cannot find
symbol : method doBarStuff()
location: class Foo
f.doBarStuff();
^
1 error



Here overriding the doBarStuff method is not done.
Though the object created is of Bar type but it is referenced
as Foo type. So, it will not have knowledge of the
method doBarStuff
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you people,

Actually my idea is that we are creating the bar object here (which is a subclass of Foo) with the help of base class reference Foo f so i thought we can Invoke the subclass bar's method doBarStuff() with the base class reference Foo f.

Well where have i gone in my interpretation ?
[ August 19, 2008: Message edited by: sri dev ]
 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can invoke a subclass methode using a super class reference if the methode is already present in super class and its overridden.
superclass doesn't have an idea of subclass method if its a new method.
A method is invoked on the basis of reference type used not the object type created at runtime.
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh...got it.

Thank you vinod and others !!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"sri dev", please check your private messages. You can see them by clicking My Private Messages.
 
Alpesh Vesuwala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I know all doubts cleared but still I want to add something on this.



according to your code at runtime is legal and perfectly valid but what about compile time??

Here on f.doBarStuff(), knotty compiler finds object ref. f of Foo and tries to find the method doBarStuff() in Foo and hence failed.

Thanks,
Alpesh
 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya it will give an error at compile time itself.
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is an example of Polymorphism

Foo f = new Bar()
f.doBarStuff()

During compilation, all the methods that are invoked through f should be present in Foo Class. It is only at runtime that you come to know that f is referring to Bar Object.

Since at compile time, the compiler is not able to find doBarStuff() method in Foo Class, it gives you an error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic