DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2005
    Posts
    5

    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?

  2. #2
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    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.

  3. #3
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    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

  1. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  2. class vector<?>
    By pseudo in forum C++
    Replies: 3
    Last Post: 02-14-2005, 06:48 AM
  3. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  4. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 07:53 PM
  5. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 06:19 PM

Bookmarks

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


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


Sponsored Links