Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default What is Instance, Empty Constructor ....

    Java Code:
    abstract class PAbstract {
    	public abstract String toString();
    }
    
    class Person extends PAbstract{
    	//Person Constructor
    	//Get Set
    	//Overrides
    }
    Last edited by tking88; 10-24-2009 at 12:04 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,355
    Blog Entries
    7
    Rep Power
    20

    Default

    For every class X if you don't explicitly define a constructor for it (with or without parameters) the compiler will define one for you like this: "public X() { }", i.e. it's a no-argument constructor that doesn't do anything.

    An instance is a realization of a class, e.g. you and I are both instantiations of the class HumanBeing and have been created by a constructor of the class.

    If you want to print an object, the print( ... ) method searches for a toString() method; every method has one, either explicitly defined or inherited from the Object class. Most of the time that method (from the Object class) doesn't do what you want so you have to define a toString() for your own class(es).

    kind regards,

    Jos

  3. #3
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    Java Code:
    	Teacher t2 = (Teacher) new Person();
    	System.out.println(t2);
    Last edited by tking88; 10-24-2009 at 12:05 AM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,355
    Blog Entries
    7
    Rep Power
    20

    Default

    A Teacher is a Person but a Person doesn't have to be a Teacher so if you create a new Person you can't just cast it to a Teacher; you have to create a Teacher and leave out that cast. So:

    Java Code:
    Person teacher= new Teacher(); // a Teacher is a Person but:
    Person teacher= (Teacher)new Person(); // a Person is not always a Teacher.

    kind regards,

    Jos

  5. #5
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    Java Code:
    class Teacher extends Person
    {
    	public Teacher() {
    	}
    }
    Hcome I have to create a empty public teacher in teacher extends person and I can't create inside class test ?
    Last edited by tking88; 10-24-2009 at 12:07 AM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,355
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by tking88 View Post
    My Question is how come I have to create a empty public teacher in teacher extends person and I can't create inside class test ?
    Beats me, you came up with that no-args constructor requirement; as you can see from the output neither the name (null) and age (0) are set; maybe you have to set them using a setName( ... ) and setAge( ... ) method. Read your assignment text again.

    kind regards,

    Jos

  7. #7
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    Java Code:
    class Test{
    	public static void main(String[] args)
    	{
    		Person c1 = new Person();
    		System.out.println(c1); 
    	}  
    }
    Last edited by tking88; 10-24-2009 at 12:08 AM.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,355
    Blog Entries
    7
    Rep Power
    20

    Default

    You're calling the no-args constructor and that one doesn't set the name member to any sensible value; call new Person("Jack") in your test class instead.

    kind regards,

    Jos

  9. #9
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    I have change to use protected on ToString method see below

    Java Code:
    abstract class PAbstract {
    	abstract String toString();
    }
    ..
    .
    .
    .
    protected String toString() {
    		String message = Name +" "+Age;		
    		return message;
    	}
    then I get abstract String toString(); the toString() underline red it said can not reduce visibility of
    the inherit method form object.
    Last edited by tking88; 10-24-2009 at 01:30 AM.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Override the toString method in the Person class to return a meaningful string that describes the person.

  11. #11
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    Not sure Question 4 protected ToString() yet, then how about Question 1, 2 and 3 ?

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    1.) You just print the person object c1 not its getClass. If you have correctly overriden the toString method this will print what you have returned from the toString method.
    2.) You did not read Jos' response above. To initialize bot teachers with the same name, create them by calling the constructor that takes a name (a string) not new Teacher() but new Teacher("the name of the teacher");
    3.) Override the equals method and return true if the names are equal.
    4.) Make the toString method public not protected. Also name it correctly. toString not ToString.

  13. #13
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    How come will return false I put in 2 same name it should return true ?

    Java Code:
    		
    Teacher p1= new Teacher("Nelson"); 
    System.out.println(p1); 
    Teacher p2= new Teacher("Nelson"); 
    System.out.println(p2); 
    
    System.out.println(p1.equals(p2));
    Output
    Java Code:
    false

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Did you correctly override the equals method at all like I suggested? Post your equals method.

  15. #15
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    K here my overload equal and it did give me true this time, but is any way I can put this overlaod inside my Test

    Java Code:
    	
    class Teacher extends Person{
    
    	private int x;
    	public Teacher(int x) {
    		this.x = x;
    	}
    	public boolean equals(Object obj) {
    		if (obj instanceof Teacher) {
    			return equals((Teacher) obj);
    		}
    		return false;
    	}
    	public boolean equals(Teacher superclass) {
    		return x == superclass.x;
    	}
    	
    }
    
    class Test{
    	public static void main(String[] args)
    	{
                 .
                 .
                 .
                 System.out.println(teacher1.equals(teacher2));
    	}
    }
    Last edited by tking88; 10-24-2009 at 02:30 AM.

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Hey, you only create one equals method that takes an Object and returns a boolean.
    If the instanceof check for Teacher succeeds, you cast the Object into a Teacher and then compare that teacher's name with the current teacher's name.
    That's how you get the equals method to return true if the names are the same.

  17. #17
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    hmm I got error :(

    Java Code:
        boolean equals(Teacher obj) {
        	if (obj instanceof Teacher) {
    			return equals((Teacher) obj);
    		}
          return this == obj;
        }

    this is so hard :(

  18. #18
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    hmm I add below code to Teacher class again

    Java Code:
    	private int num;
    	private String data;
    
    	public boolean equals(Object obj)
    	{
    		if(this == obj)
    			return true;
    		if((obj == null) || (obj.getClass() != this.getClass()))
    			return false;
    		// object must be Test at this point
    		Teacher test = (Teacher)obj;
    		return num == test.num && 
    		(data == test.data || (data != null && data.equals(test.data)));
    	}


    Teacher teacher1= new Teacher("Nelson1");
    Teacher teacher2= new Teacher("Nelson2");

    System.out.println(teacher1.equals(teacher2));

    it should be false but no matter what I did this time always true

    I think I don't really understand how it work at all can you please help me change the code to make it work ?

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,355
    Blog Entries
    7
    Rep Power
    20

    Default

    Don't just post fragments of your code; you are making a mess out of things, not only here but you also post in at least one other forum. Show us your entire class and don't overcomplicate matters.

    kind regards,

    Jos

  20. #20
    tking88 is offline Member
    Join Date
    Oct 2009
    Posts
    16
    Rep Power
    0

    Default

    I am ok now I got the solution code for the equal override:)

    Java Code:
    System.out.println(o instanceof Moof);
    System.out.println(((Moof)o).getMoofValue());
    System.out.println(this.moofValue); //XXX
    if ((o instanceof Moof) && (((Moof)o).getMoofValue() == this.moofValue))
    {
    return true;
    }
    else
    {
    return false;
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 07:08 PM
  2. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 07:28 PM
  3. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:36 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •