Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Question How to write a good constructor in a case of heritage?

    Good evening!

    I'm new to java and I'm now learning how to have good costructors. I had an exercise on it which wasn't corrected during class, so I wondered if you could help me know if I'm right.

    Having:

    Class Composant, which is abstract and which defined the characteristics of a named composant. The class is characterized by a name and realized two interfaces Named and Valuable defined below. Two composants are equals if they have same name and same value.

    Class Atome, a composant characterizd by a name and a double value. An atome is equal to another object ig it has the same value. The name of an atome isn't given during creation, it is given by his "parent" (the element which contains it) followed by the character .

    Complete the constructors of Atome and Composant (C1 & C2).


    Java Code:
    /////////////////////////////       ATOME        ////////////////
    package java_td.td6_composite2;
    
    public class Atome extends Composant{
    	private double value;
    
    	public Atome(Named parent, double value){
     			?????????????
     		this.value = value;
      }
    
      public double value(){
        	return value;
      }
    
      public boolean equals(Object object){
    	System.out.println("equals Atome");
      return ??????????;									C1
      }
    
      public String toString(){
           return super.toString()+"\n";
      }
    }//Fin Atome
    
    //////////////////////        COMPOSANT         //////////////
    
    package java_td.td6_composite2;
    public abstract class Composant implements Named, Valuable, Comparable{
      private String name;
    
    	public Composant(String name){
           	this.name=name;
    	}
    
    	public String name(){
        	return name;
    }
    /****************************************************************
    /*                 Interface Valuable                                
    ********************************************************************/
    	public abstract double value();
    
    /**********           **********************************************/
    /*                Redefined methods                                   /****************************************************************/
    
    	public boolean equals(Object object){
     		boolean result=false;
     		System.out.println("equals Objet Composant entrée");
     		if (???){
    		System.out.println("equals Objet Composant cas Named");
       		Named temp = (Named) object;
       		result= equals(temp);System.out.println("equals res. interm. "+result);
      		}
           if (???????){
    	  	System.out.println("equals Objet Composant cas Value");
      	  	result = ????????????????????						C2
     	      }
      	 return result;
      	}//end equals(Object object)
    
    	?????? boolean equals(Named object){
      		System.out.println("equals Named Composant");
      	return name() == (object.name());
    	}
    	?????? boolean equals(Valuable object){
    		System.out.println("equals Valuable Composant");
      	return ????????????????;
      }
         */ Compares this object to another Object. This function behaves
         * of the <code>compareTo(String)</code> function in taking into account
         * the String represention of the both objects. */
      public int compareTo (Object object) {
           if (object instanceof Valuable){
           return compareTo(((Valuable)object));
    		 }
       	return 1;
    	}//end compareTo
    
     	???????????  int compareTo(Valuable object){
    	return        ???????????????????;						C3
     	}
    	public int hashCode(){
    	return toString().hashCode();
     	}
    	public String toString(){
    	return " name : "+name() +"\t value : "+value();
     	}//end toString()
    } // Fin COMPOSANT
    
    interface Named{
    	String name();
    }
    
    interface Valuable{
    	double value();
    }

    I've said that the constructor of Atome was:

    Java Code:
    public Atome(Named parent, double value){
     			super(name)=parent;
     		this.value = value;
      }
    But I didn't found that Composant needed to have is constructor revised as far as it is given in the code?

    Can you tell me if my constructor of Atome is okay and if I misunderstood what were asked for the constructor of composant?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: How to write a good constructor in a case of heritage?

    super(name)=parent; is not valid code. You call a constructor of the parent class with super(), or super(parameter), or super(parameter1, parameter2), whatever the super class requires. So in your case, super(parent) looks like it would do the trick.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  3. #3
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Default Re: How to write a good constructor in a case of heritage?

    Okay, this way:

    Java Code:
    public Atome(Named parent, double value){
     			super(parent);
     		this.value = value;
      }
    Thank you!

    But what about Composant ?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: How to write a good constructor in a case of heritage?

    Two things:
    1) Atome inherits from Composant. The only constructor in Composant is Composant(String) so you need to call the super constructor with a String argument. The previous example uses the Named interface as parameter so that won't work.
    2) Composant inherits from nothing (technically from Object which doesn't have any constructors). So there is no need to call a super constructor.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Question Re: How to write a good constructor in a case of heritage?

    Okay so:

    1) Atome inherits from Composant. The only constructor in Composant is Composant(String) so you need to call the super constructor with a String argument. The previous example uses the Named interface as parameter so that won't work.
    So why isn't it this this way in the code given as a problem statement?

    2) Composant inherits from nothing (technically from Object which doesn't have any constructors). So there is no need to call a super constructor.
    Does it mean that the lecturer who asked to do constructors of both classes were wrong asking for it?
    Thank for your answers!

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: How to write a good constructor in a case of heritage?

    Quote Originally Posted by AntoineCompagnie View Post
    Okay so:
    So why isn't it this this way in the code given as a problem statement?
    In Atome calling super(Named) would work if the super class has a constructor with signature public Composant(Named). But it doesn't. Composant only has constructor public Composant(String). So your subclass Atome has to call the super constructor with a String as parameter.


    Quote Originally Posted by AntoineCompagnie View Post
    Does it mean that the lecturer who asked to do constructors of both classes were wrong asking for it?
    Thank for your answers!
    I am not saying that Composant can't have a constructor. I am saying that Composant does not need to call a super constructor since it inherits from Object which doesn't have any contructors (Technically, when a class has no constructor defined, the compiler will generate the default constructor, the one without any parameters.) You can make 5, or 10, or 100 constructors in Composant if you want, doesn't matter. But your subclasses have to call at least one of the super constructors. (Unless the default constructor is given, then it will automatically call super() ).

    See if you understand this example:
    Java Code:
    class Vehicle {
       protected int numWheels;
    
       public Vehicle(int numWheels) {
          this.numWheels = numWheels;
       }
    }
    
    class Car extends Vehicle {
       public Car() {
          super(4);
       }
    
       public Car(int wheels) {
          super(wheels);
       }
    }
    
    class Motorcycle extends Vehicle { 
    
       public Motorcylce(int wheels) {
          super(wheels);
       }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  7. #7
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Question Re: How to write a good constructor in a case of heritage?

    okay understood for the second constructor, (Composant). And that a sub class has to call for its super constructor. But for the first one, for Atome, I don't understand if, having the following code:

    Java Code:
    public class Atome extends Composant{
        private double value;
     
        public Atome(Named parent, double value){
                ?????????????
            this.value = value;
      }
     
      public double value(){
            return value;
      }
     
      public boolean equals(Object object){
        System.out.println("equals Atome");
      return ??????????;                                    C1
      }
     
      public String toString(){
           return super.toString()+"\n";
      }
    }//Fin Atome
    its constructor, teacher saying is:

    Java Code:
        public Atome(Named parent, double value){
                super(parent.name()+ "_" + value)
            this.value = value;
      }
    where parent.name() is a Name, and a name is a String as far as I can read line 36 in the given code.
    Thus subclass Atome calls the super constructor with a String as parameter.


    But why can't it be

    Java Code:
        public Atome(Named parent, double value){
                super(parent + "_" + value)
            this.value = value;
      }
    why should we add .name()? Can't we try only using "parent" which is given as a name isn't it therefore a String?
    Last edited by AntoineCompagnie; 12-02-2015 at 05:37 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to write a good constructor in a case of heritage?

    Because the constructor of Composant take a String, not a Named.
    It's really as simple as that.

    Though I would question why the concatenation of name and value like that...then again, I'm not sure what's being modeled.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Default Re: How to write a good constructor in a case of heritage?

    Okay, the constructor of Composant take a String, not a Named. But still a Named is a String, isn't it?

    line 91 of the original code:
    Java Code:
    interface Named{
        String name();
    }
    I agree with your doubts about the concatenation, and I'm not sure neither of what was moddeling my teacher.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to write a good constructor in a case of heritage?

    No it's not.
    It's a Named.

    It has a method that returns a String, but then so does Date.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: How to write a good constructor in a case of heritage?

    There is a difference between this:
    Java Code:
    super(parent)
    and
    Java Code:
    super(parent.name()+ "_" + value)
    The first super call uses a Named parameter (Because parent is a Named). The second super call uses the name method, so it's a String parameter now.

    Edit: remove toString() gibberish.
    Last edited by SurfMan; 12-03-2015 at 04:00 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to write a good constructor in a case of heritage?

    Quote Originally Posted by SurfMan View Post
    The first super call uses a Named parameter (Because parent is a Named). The second super call uses the name method, but since it's used in String concatenation, the compiler will automatically do a toString() on the parent.name(). So you're not passing a Named, but a String.
    name() returns a String.
    There is no toString() being done behind the scenes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: How to write a good constructor in a case of heritage?

    Quote Originally Posted by Tolls View Post
    name() returns a String.
    There is no toString() being done behind the scenes.
    I stand corrected. I missed that one.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Similar Threads

  1. How to write constructor?
    By federer in forum New To Java
    Replies: 6
    Last Post: 11-07-2011, 11:33 PM
  2. Replies: 6
    Last Post: 06-23-2011, 05:54 PM
  3. Replies: 2
    Last Post: 02-24-2011, 03:14 AM
  4. Replies: 3
    Last Post: 09-11-2008, 04:00 AM

Tags for this Thread

Posting Permissions

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