How can I override a method in a base class without using the @Override sign? For example the class Human below overrides some methods in the class Mammal. Do print the output correctly without @ signs in objects. Convert them correctly before outputting.

import java.util.*;
public abstract class Human extends Mammal 
{
    int MyAge,Age;
    String mrating; 
        
    Arms[] arms = new Arms[2];
    Leg[] legs = new Leg[2];
  
    void walk()
    {
        legs[0].move("Right Leg"); 
        legs[1].move("Left Leg");    
    }
     
    void swim() 
    {
          arms[0].move("Right hand");  
          arms[1].move("Left hand");  
      
    }
 
	// default constructor
    public Human(String name) 
    {
        super(name);
    	gender = 'M';   // the default is M for Male
    }
    
    @Override
    public void makeSound() 
    {
       System.out.print(name+" Oooops ");
    }
   
    @Override
    public void getOlder(int years) 
    {
         age += years;
    }
  
    @Override
    public void eat()
    {
        System.out.print("Some food please...");
          
    }
    // the getters and setters...
    public int getAge()	
    { 
          return this.MyAge;
    }
    public void setAge(int Age) 
    { 
        this.MyAge=Age; 
    }
     
        
    //methods for iterating through the limbs
    public void getArms()
    {
        Limb limbs = new Arms("");
        ArrayList myArms = new ArrayList();
        // Populate the list using the .add() methods
        myArms.add("Right Hand");
        myArms.add("Left Hand");
        Collection myforeLimbs =myArms;
        if(limbs instanceof Arms)
        {
              System.out.println("\t"+myforeLimbs);
        }
        
    }
    public void getLegs()
    {
        Limb limbs = new Leg("");
         ArrayList myLegs = new ArrayList();
        // Populate the list using the .add() methods
        myLegs.add("Right Leg");
        myLegs.add("Left Leg");
        Collection myhindLimbs =myLegs;
        if(limbs instanceof Leg)
        {
              System.out.println("\t"+myhindLimbs);
        }
        
    }
    public abstract boolean canWatchMovie(String mRt);
 
}

Recommended Answers

All 3 Replies

Just simply remove those symbols?

Really, those tags are only compiler "hints". They are there only for your convenience, as it is an indicator to the compiler that the following method is suppossed to override a method of an inheritied class. So, the compiler will check that it actually does so, and if it does not find a method, with that signature, in an inheritied class it will alert you that the method is not overriding anything, thereby preventing some possible polymorphism problems (i.e. you wouldn't using polymorphism when you thought you should be).

commented: Expert answer =) +4

masijade is correct, i think its more of a convention to follow though

just like your variable names

int MyAge,Age;
//need to be named
int myAge, age;

and with the overrides its much easier to tell what methods are implementing its base class, than sorting through the code

masijade is correct, i think its more of a convention to follow though

It's not just a convention, it's a good programming practice. If you incorrectly attempt to override a superclass's method without the annotation, your error will be silently ignored. If you do include it, you will get a nice compile-time error.

Consider the following class:

public class Test {
    public static void main(String[] args) {
        Object t1 = new Test();
        Object t2 = new Test();

        System.out.println(t1.equals(t2)); // prints false
    }

    public boolean equals(Test t) {
        return true;
    }
}

It is a common mistake to assume that it will print true . After all, we are overriding the Object class's equals method right?

No, we aren't. The method we are trying to override is equals(Object) , yet we wrote the method equals(Test) . Without the @Override annotation, this code will happily compile and never warn you of your mistake. These kinds of errors can be very hard and annoying to track down. With the annotation, you will get a compile-time error saying, "method does not override or implement a method from a supertype." Much better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.