Results 1 to 10 of 10
  1. #1
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Question this() vs super()

    I do not understand the answer to the following question:
    "How are this() and super() used with constructors?
    =>
    this() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor."

    I made my code where I tried to use "this()" as instantiating the constructor but without a success. Similarly, I did not manage to use super();.

    There is my code here: dl.getdropbox.com/u/175564/java%20problem.png

    The superclass is above, while the subclass is below.

    How would you demonstrate this() and super()?

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    your using this wrong. its supposed to just be this.something

    Say your working with a superclass called Person and a subclass called Student. You know a Student is a person and a person has a name so a student must have a name. When you create a new Student instead of having to do this.name = name you can call super(name) and it will create a Student object using the Person constructor.

    You then use this.someValue to add additional stuff to Student that Person doesn't have(like school, grade, etc)

    Java Code:
    class Person{
    	String name;
    	Person(String name){
    		this.name = name;
    	}
    }
    class Student extends Person{
    	Student(String name, int grade, String school){
    		super(name);
    		this.grade = grade;
    		this.school = school;
    	}
    }
    public static void main(String[] args){
    	String name = "leeroy";
    	int grade = 8;
    	String school = "JavaHigh";
    	Student leeroy = new Student(name, grade, school);
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're not doing anything with the constructors in your code but instead trying to mess with two main methods. Get rid of the main methods, ignore them throw them out. Later you can use them to run the program and can even do that from another driver class, but right now you need to read up on what constructors are and how to use them, and the place to do that is in the Sun tutorials. Have a look here:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  4. #4
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    I cleaned up the above code to present the problem:
    Java Code:
    class Person{
    	public String name="Hevonen";  // instance var
    	public Person(){              // empty constructor
    	}
    }
    
    class Student extends Person{
       Student(String name){     // constructor of Student class which superclass is Person
    	super(name);         // trying to use super() ???
       }
    
       public static void main(String[] args){   
          Student S = new Student();           // instance of Student class
          System.out.println(S.name);   // trying to print var of superclass to show how super() works
       }
    }
    Now, the problem should be in super().
    Last edited by Hevonen; 12-02-2008 at 11:53 PM.

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Java Code:
    public class Person{
        static int CountOfPersons=0;
        //
        String name;
        public Person(){
            // non-empty constructor
            ++CountOfPersons;
        }
    }
    class Student extends Person{
        String name;
        // constructor of Student class which superclass is Person
        Student(String name){
        // Now using super()
        super();
        this.name = name;
       }
    }
    
    public class AnotherPerson extends Student{
        public AnotherPerson(String string){
            // empty constructor
            super(string);
        }
    }
    public class NamelessPerson extends AnotherPerson{
        public AnotherPerson(){
            // empty constructor
            super("No Name");
        }
    }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Thx!

    I think that I got a hint about the logic :)
    There is a picture which shows the relations here:
    dl.getdropbox.com/u/175564/java-ex.png

    What kind of the main program should be to demonstrate the code? For instance, should I make an instance of the superclass or of the class which has now the super() command?
    Last edited by Hevonen; 12-03-2008 at 01:30 AM.

  7. #7
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    I made this kind of main program, which however does not work yet:
    Java Code:
    public class Persona{
        static int CountOfPersons=0;        
        //
        String name;
        public Persona(){
            // non-empty constructor
            ++CountOfPersons;
        }
    }
    
    class Student extends Persona{
        String name;         
        // constructor of Student class which superclass is Persona
        Student(String name){
          // Now using super()
          super();                  //
          this.name = name;
        }
    
          public static void main(String[] args){
             Student S = new Student("sami"); 
             
             /* putting sami to name; if this
             works, name in Student and in Persona too should be sami; I assume
             that the value from the subclass goes to the superclass*/
             //so we make now a constructor of Persona to test this
             
             Persona P = new Persona();
             System.out.println(P.name);
       }
    }
    There is the same code here:
    dpaste.com/95543/

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile correct

    Basically, correct. I added some stuff, not tested and not proficient at Lists, but to get main to be of use we will need some way to "print the list"....
    Quote Originally Posted by Hevonen View Post
    I made this kind of main program, which however does not work yet:
    Okay, this jumps ahead and I am not fluent at how I did it but should provide something to work with...
    Java Code:
    import java.util.List;
    import java.util.Arrays;
    /**
    *
    *
    *
    *
    */
    public class PersonaSuperClass{
        static int CountOfPersons=0;        
        //
        String name;
        public PersonaSuperClass(){
            // non-empty constructor
            ++CountOfPersons;
        }
    }
    /**
    * 
    * 
    * 
    * 
    */
    class SuperStudent extends PersonaSuperClass{
        List<String> names;         
        // constructor of Student class which superclass is Persona
        Student(String[] args){
          // Now using super()
          super();
          int counter = 0;
          // Subject to revision by other posters who know collections well.
          this.name = Arrays.asList(args);
        }
        void sortList(){
            Arrays.sort(names);
            
        }
    
        public static void main(String[] args){
          SuperStudent sooperStrudel = new SuperStudent(args);
    
         /* What this can be is reporting.... */
         sooperStrudel.sortList();
         }
    }
    There's an error in the base class implementation of the counter, but we do not want to load you with too much yet. Counter, as we have it here, has to be static. If you call new person for each name in student names list, we have to do some further work disentangling everything but you are supposed to be studying ... there are several ways to do it. It depends on how you think and what the problem domain is.

    I do not want to provide shippable code directly, you can just do new Student(name) and the code is close to working as you had it - the name would be visible. There is no shortage of people who use this typing / class naming and so on to keep things straight. They may provide an approach that is based on inheritance.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  9. #9
    Hevonen is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    Thanks for the reply!

    There is the code which I got ready here:
    Java Code:
    class Persona{
        public int countOfPersons=0; // static vars require static methods: we do not want that
        //
        String name;
        public Persona(){
          countOfPersons+=1;
        }
    }
    
    public class Student extends Persona{
        String name;         
        public int p, q;
        // constructor of Student class which superclass is Persona
        Student(){
          // Now using super()
          super(); // super() takes the constructor which has no parameter in the superclass
          this.p=super.countOfPersons;
        }
    
        Student(int r){
           this();     // this() takes the constructor without parameters above
           this.q=super.countOfPersons +100 + r;
        }
    
        public void print(int i){
           System.out.println(this.p);
           System.out.println(this.q);
           System.out.println(i);
        }
    
          public static void main(String[] args){
             Student S = new Student(66);
             S.print(66);
       }
    }
    Now, Java compiler does not complain. Do you see any mistake in the code?
    Last edited by Hevonen; 12-03-2008 at 02:31 PM.

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile suggestions....

    Quote Originally Posted by Hevonen View Post
    Thanks for the reply! ... Now, Java compiler does not complain. Do you see any mistake in the code?
    Java is a machine, .....

    To think of a machine complaining is allegory, say "diagnostics" or "error messages"

    ( humor )

    Good work, but work remains - Print only prints totals or something, we continue.
    Java Code:
    class Persona{
        // static vars do not require static methods: we want that.
        public static int countOfPersons=0; 
        // Note the logic of the increment operator syntax, it is important
        // in many computer prgrams to give immediate attention to "Off by One."
        String name;
        public Persona(){
          ++countOfPersons;
        }
    }
    
    public class Student extends Persona{
        String name;         
        public int p, q;
        // constructor of Student. superclass is Persona 
        Student(){
          // Now using super() - YES! ta-da...
          // no parameter super() calls no parameter constructor of the superclass. 
          super(); 
          // Correct, now each student has a number...
          // Give them a number, but leave them a Name also....
          this.p=super.countOfPersons;
        }
    
        Student(int r){
            // this() invokes the no arg constructor of Student class, incrementing count of students..
           this();     
           this.q=super.countOfPersons +100 + r;
        }
    
        public void print(int i){
           System.out.println(this.p);
           System.out.println(this.q);
           System.out.println(i);
        }
        // This needs work, I have to go attend regular duties. Can ( will ? )
        // someone provide some static List datatype in base class to keep list of 
        // students, possibly sorting out the way to boogie a'la contemporary practice?
        // Also, you OO guys provide directions on where to put the print, main()?...
        public static void main(String[] args){
         Student S = new Student(66);
         S.print(66);
       }
    }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Inheritance and (super)
    By pheonix in forum New To Java
    Replies: 12
    Last Post: 10-20-2008, 03:00 PM
  2. Super CSV 1.20
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 08:22 PM
  3. Super CSV 1.15
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-16-2007, 06:37 PM
  4. Use super. or this.
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:52 AM
  5. difference between this and super
    By mrark in forum New To Java
    Replies: 1
    Last Post: 06-27-2007, 05:23 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
  •