开发者

overriding and overloading

public class Module
{
    // required instance variables
    private String codeModule, titleModule;
    private int pointsModule;

    //three-argument constructor receiving a code, a title string, and a        
    //number of points, 
    //which sets the code, title and points data of the created object to    
    //the received values.
    public Module(String aCode, String aTitle, int aPoints)
    {
       codeModule = aCode;
       titleModule = aTitle;
       pointsModule = aPoints;
    }

    //set the instance data value codeModule to received argument newCode.
    public void setaCode(String newCode)      
    {
       codeModule = newCode;
    }

    //set the instance data value titleModule to received argument 
    //newTitle.                 
    public void setaTitle(String newTitle)      
    {
       titleModule = newTitle;
    }

    //set the instance data value pointsModule to received argument        
    //newPoints.
    public void setaPoints(int newPoints)      
    {
       pointsModule = newPoints;
    }

    //return the instance data values of codeModule.
    public String getaCode()
    {
       return codeModule;
    }

    //return the instance data values of titleModule.
    public String getaTitle()
    {
       return titleModule;
    }

    //return the instance data values of pointsModule.
    public int getaPoints()
    {
       return pointsMo开发者_JAVA百科dule;
    }

    //returns a string containing the full details of the module, 
    //giving the module code, title and number of points in parentheses
    public String toString()
    {
       return "Module code " + codeModule + ", Module Title " + titleModule + ", Module 
       Points (" + pointsModule + ")";
    }

    //returns true if the module referenced by o has the same instance       
    //variable values 
    //as the object on which this method is invoked; otherwise the method    

    public boolean equals(Object o)
    {
       Module m = (Module)o;
       return codeModule.equals(m.codeModule) && titleModule.equals(m.titleModule) && 
       pointsModule == m.pointsModule;
    }

    //returns true if the code string begins with a capital letter and has      
    //four characters; 
    //otherwise the method returns false.
    public Boolean isCode()
    {
       if (codeModule.length()== 4 && (codeModule.charAt(0) >=  'A'   
       &&(codeModule.charAt(2) <= 'Z')))  
       {
          return true;
       }
       else
       {
          return false;
       }
    }     
}

I have been asked to identify any methods that overload or override any other methods, as far as i understand overloading occurs when a class has more than one method with the same name, but with different signatures (i.e. the type, number or order of their formal arguments must differ) and a method definition in a subclass overrides an inherited method from a superclass or one of its ancestral classes, if the signature of the method in the subclass exactly matches the signature of an inherited method and they have the same return type. Private methods in a superclass are not overridden in a subclass, as they are not inherited.

As I understand it i don't have any methods which have the same names to cant overload and i haven't extended another class so i cant override! Would this be correct or have i missed something.

Thanks bb

(I have also be asked to format correct from previous questions, i hope this is correct)


Your understanding is correct, but remember that all objects implicitly extend java.lang.Object, so you need to check whether you are overriding any methods from that class, too.


You're right with your definitions but toString() overrides the method in Object

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜