Im trying to run this code from a book called "thinking in java"
For some reason the Detergent class is not being called by the program instead it appears to be calling the Cleanser class. Im not sure where is the error b/c everything seems to compile nicely and the cleanser class works fine. Im working on the topic of inheritance.
code->
Code://: Detergent.java
// Inheritance syntax & properties
class Cleanser{
private String s = new String("Cleanser\n");
public void append(String a) { s += a; }
public void dilute(){ append("dilute()\n"); }
public void apply(){ append("apply()\n"); }
public void scrub(){ append("scrub()\n"); }
public void print() { System.out.println(s); }
public static void main(String[] args){
Cleanser x = new Cleanser();
x.dilute();
x.apply();
x.scrub();
x.print();
}
}
public class Detergent extends Cleanser{
//change a method:
public void scrub(){
append(" Detergent.scrub()");
super.scrub(); //Call base-class version
}
//Add methods to the interface:
public void foam(){ append(" foam()"); }
// Test the new class:
public static void main(String[] args){
System.out.println("Inside Detergent class");
Detergent x = new Detergent();
x.dilute();
x.apply();
x.scrub();
x.foam();
x.print();
System.out.println("Testing base class:");
Cleanser.main(args);
}
}
To help you little here
OBJECT !
Code:class Cleanser{ private String s = new String("Cleanser\n"); public void append(String a) { s += a; } public void dilute(){ append("dilute()\n"); } public void apply(){ append("apply()\n"); } public void scrub(){ append("scrub()\n"); } public void print() { System.out.println(s); } public static void main(String[] args){ Cleanser x = new Cleanser(); x.dilute(); x.apply(); x.scrub(); x.print(); } }
THE MAIN !
That's it, now should everything work perfectly normal !Code:public class Detergent extends Cleanser{ //change a method: public void scrub(){ append(" Detergent.scrub()"); super.scrub(); //Call base-class version } //Add methods to the interface: public void foam(){ append(" foam()"); } // Test the new class: public static void main(String[] args){ System.out.println("Inside Detergent class"); Detergent x = new Detergent(); x.dilute(); x.apply(); x.scrub(); x.foam(); x.print(); System.out.println("Testing base class:"); Cleanser.main(args); } }
Hope you enjoy it![]()
Excuse me if im wrong, but its look like u just seperated the code. The Cleanser class and the Detergent class are supposed to be in same file. "Even if you have a lot of classes in a program only the main() for the public class invoked will be called.". By seperating the code i learn nothing. Detergent main() is supposed to call Cleanser main().
A main class is not supposed to Inherit from subclasses... Just by using them separating them the output will be.
The Detergent's output
Code:Inside Detergent class Cleanser dilute() apply() Detergent.scrub()scrub() foam() Testing base class: Cleanser dilute() apply() scrub()
The Cleanser's output
If you don't understand the mistake you did was this.Code:Cleanser dilute() apply() scrub()
The alternative way to do is making it abstract !Code:The public type Detergent must be defined in its own file
But your variables will not be !
BUT IF YOU REALLY WANT TO DO THE WRONG WAY here it is...Code:Cannot instantiate the type Detergent
Try to figure why I did this way, you will understand the error you made later or you will post again here complaining...Code://: Detergent.java // Inheritance syntax & properties class Cleanser{ private String s = new String("Cleanser\n"); public void append(String a) { s += a; } public void dilute(){ append("dilute()\n"); } public void apply(){ append("apply()\n"); } public void scrub(){ append("scrub()\n"); } public void print() { System.out.println(s); } public static void main(String[] args){ Cleanser x = new Cleanser(); x.dilute(); x.apply(); x.scrub(); x.print(); } public void foam() { } } public abstract class Detergent extends Cleanser{ //change a method: public void scrub(){ append(" Detergent.scrub()"); super.scrub(); //Call base-class version } //Add methods to the interface: public void foam(){ append(" foam()"); } // Test the new class: public static void main(String[] args){ System.out.println("Inside Detergent class"); Cleanser x = (Cleanser) new Cleanser(); x.dilute(); x.apply(); x.scrub(); x.foam(); x.print(); System.out.println("Testing base class:"); Cleanser.main(args); } }
Im not complaining bro just trying to understand and i can take critisism, its all in the learning process. If i offended you, my apologies, i did not want just the solution, as in your first response but an explanation for why what was happening was happening as in your second response. Now i think i grasp the concept. Much thanks!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks