Closed Thread
Results 1 to 6 of 6

Thread: Inheritance

  1. #1
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Inheritance

    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) { += 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);
        }


  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Inheritance

    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 !
    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);
        }
    }
    That's it, now should everything work perfectly normal !
    Hope you enjoy it

  4. #3
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Re: Inheritance

    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().

  5. #4
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Inheritance

    Quote Originally Posted by fread View Post
    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

    Code:
    Cleanser
    dilute()
    apply()
    scrub()
    If you don't understand the mistake you did was this.
    Code:
    The public type Detergent must be defined in its own file
    The alternative way to do is making it abstract !
    But your variables will not be !
    Code:
    Cannot instantiate the type Detergent
    BUT IF YOU REALLY WANT TO DO THE WRONG WAY here it is...

    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);
        }
    }
    Try to figure why I did this way, you will understand the error you made later or you will post again here complaining...

  6. #5
    fread's Avatar
    fread is offline Programming God
    Join Date
    Nov 2008
    Location
    Caribbean
    Posts
    654
    Blog Entries
    1
    Rep Power
    16

    Re: Inheritance

    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!

  7. #6
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Inheritance

    Quote Originally Posted by fread View Post
    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!
    Well no worries, I am just kinda tired with my fever and school...

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Use Constuctor In Inheritance
    By VakhoQ in forum Java Help
    Replies: 11
    Last Post: 08-01-2011, 09:11 AM
  2. C++ Inheritance Question!
    By Yuriy M in forum C and C++
    Replies: 3
    Last Post: 11-25-2010, 08:22 AM
  3. need small help..inheritance
    By iostream in forum C and C++
    Replies: 6
    Last Post: 10-03-2010, 12:51 PM
  4. A few problems with Inheritance
    By brutetal in forum Java Help
    Replies: 5
    Last Post: 05-23-2010, 03:03 AM
  5. C++ Inheritance help!?
    By rossen in forum C and C++
    Replies: 7
    Last Post: 06-11-2008, 10:18 AM

Tags for this Thread

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