-
Can't use an inherited class
Background: I am using Bruce Eckel's eBook, "Thinking in Java", 3rd ed.
Problem: I try to use inheritance to extend a class, but a method that *should* be available seems not to be:
class Cleanser {
private String s = new String("Cleanser");
public void append(String a) { s += a; }
public void dilute() { append(" dilute()"); }
public void apply() { append("apply()"); }
public void scrub() { append("scrub()"); }
public String toString() { return s; }
public static void main (String[] args) {
Cleanser x = new Cleanser();
x.dilute(); x.apply(); x.scrub();
System.out.println(x);
}
}
class Detergent extends Cleanser {
// change a method:
public void scrub() {
append ("Detergent.scrub()");
super.scrub();
}
// add methods to the interface:
public void foam() { append(" foam()"); }
// test the new class:
public static void main (String[] args) {
Detergent x = new Detergent();
x.dilute(); x.apply(); x.scrub(); x.foam();
System.out.println (x);
System.out.println ("Testing the base class" );
Cleanser.main(args);
}
}
//++++++++++
//
// my extension:
//
+++++++++++
public class Autoclave extends Detergent {
public void scrub() {
System.out.println("Autoclave Scrub");
}
public void sterilize() {
System.out.println ("Sterilizing");
}
public static void main (String[] args) {
Autoclave a = new Autoclave();
Cleanser.dilute();
a.apply(); a.scrub(); a.foam(); a.sterilize();
//System.out.println ("Calling Cleanser scrub()");
}
}
//++++++++++++++++++++++++
When I compile Autoclave.java , javac complains that it cannot find the symbol foam()
What am I doing wrong?
-
Code:
public class Autoclave extends Detergent {
public void scrub() {
System.out.println("Autoclave Scrub");
}
public void sterilize() {
System.out.println ("Sterilizing");
}
public static void main (String[] args) {
Autoclave a = new Autoclave();
a.dilute(); // it was: Cleanser.dilute();
a.apply(); a.scrub(); a.foam(); a.sterilize();
}
}
You were trying call Cleanser.dilute(), which is a non-static method, from a static context. Read http://mindprod.com/jgloss/static.html if you find this static-thing confusing.
Good luck.
-
Don't understand your reference to foam(). Was that just a test?
Autoclave.java(50,10) : error J0242: Cannot make static call to non-static method 'void dilute()'
Similar Threads
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 3
Last Post: 02-14-2005, 06:48 AM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
By Shailesh C.Rathod in forum .NET
Replies: 2
Last Post: 03-13-2002, 07:53 PM
-
By Patrick Ireland in forum .NET
Replies: 5
Last Post: 05-10-2001, 06:19 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks