Having some trouble with class reuse


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Having some trouble with class reuse

  1. #1
    Join Date
    Sep 2005
    Posts
    5

    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?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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

  3. #3
    Join Date
    Sep 2005
    Posts
    5
    I figured it out. I was trying to inherit from the wrong class, unlike in my post. Is there a color for stooopid?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Weeelll,... green I guess..
    eschew obfuscation

Similar Threads

  1. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 04:00 PM
  2. Replies: 5
    Last Post: 10-17-2002, 02:58 PM
  3. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 08:53 PM
  4. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 07:19 PM
  5. Calling base class methods
    By GR in forum Java
    Replies: 1
    Last Post: 11-09-2000, 06: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center