aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  An Error In Program. 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 " An Error In Program." Watch " An Error In Program." New topic
Author

An Error In Program.

sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
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 ]

"SCJP5 | SCWCD5| DEVELOPER"
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

You don't have doBarStuff() method in Foo class


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Pradeep Balasubramanian
Ranch Hand

Joined: Jun 16, 2008
Posts: 75


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 ]

Regards,<br />Pradeep Balasubramanian<br /> <br />You have to grow from the inside out. None can teach you, none can make you spiritual. There is no other teacher but your own soul. <br /> <br />- Swami Vivekananda
Chinmay Kant
Greenhorn

Joined: Feb 04, 2008
Posts: 20
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


Thanks and Regards,
Chinmay
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
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

Joined: Jun 12, 2008
Posts: 54

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.


Regards
vinod SCJP 6.0
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
oh...got it.

Thank you vinod and others !!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

"sri dev", please check your private messages. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Alpesh Vesuwala
Greenhorn

Joined: Jan 16, 2008
Posts: 9
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


Sun Certified Java Programmer<br />Sun Certified Mobile Application Developer
Vinod Kumar Kommineni
Ranch Hand

Joined: Jun 12, 2008
Posts: 54

ya it will give an error at compile time itself.
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

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.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: An Error In Program.