-
Having some trouble with class reuse
Background: using Bruce Eckle's "Thinking in Java", 3rd ed.
Problem: when inheriting from a base class, some methods can't be accessed.
in particular:
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);
}
}
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(); // compiles fine when commented out.
a.sterilize();
//System.out.println ("Calling Cleanser scrub()");
}
}
++++++++++++++++++
In Autoclave.main() if I comment out the line
a.foam();
the class compiles fine and everything works. Otherwise, the compiler complains that it cannot see that method.
What am I doing wrong?
-
Oh ?, the only error I get when compiling this is the usage of dilute() as a
static method (in class Autoclave) which it is not.
If a method of a parent class is invisible to its descendants then that method
is declared as private in the parent class, foam() is public,
eschew obfuscation
-
I figured it out. I was trying to inherit from the wrong class, unlike in my post. Is there a color for stooopid?
-
Weeelll,... green I guess..
eschew obfuscation
Similar Threads
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
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
-
Replies: 1
Last Post: 11-09-2000, 05:38 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