Results 1 to 10 of 10
Thread: this() vs super()
- 12-02-2008, 10:37 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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()?
- 12-02-2008, 10:51 PM #2
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); }
-
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)
- 12-02-2008, 11:47 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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 } }
Last edited by Hevonen; 12-02-2008 at 11:53 PM.
- 12-03-2008, 12:14 AM #5Java 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
- 12-03-2008, 01:16 AM #6
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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.
- 12-03-2008, 01:43 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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); } }
dpaste.com/95543/
- 12-03-2008, 01:27 PM #8
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"....
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(); } }
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
- 12-03-2008, 02:05 PM #9
Member
- Join Date
- Oct 2008
- Posts
- 13
- Rep Power
- 0
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); } }
Last edited by Hevonen; 12-03-2008 at 02:31 PM.
- 12-04-2008, 01:43 PM #10
suggestions....
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
-
Inheritance and (super)
By pheonix in forum New To JavaReplies: 12Last Post: 10-20-2008, 03:00 PM -
Super CSV 1.20
By JavaBean in forum Java SoftwareReplies: 0Last Post: 11-27-2007, 08:22 PM -
Super CSV 1.15
By JavaBean in forum Java SoftwareReplies: 0Last Post: 10-16-2007, 06:37 PM -
Use super. or this.
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 06:52 AM -
difference between this and super
By mrark in forum New To JavaReplies: 1Last Post: 06-27-2007, 05:23 PM
Bookmarks