It's UWAweek 17 (1st semester, week 8)

help2005

This forum is provided to promote discussion amongst students enrolled in CITS2005 Object Oriented Programming.

Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

How do I ask a good question?
Displaying the 3 articles in this topic
Showing 3 of 94 articles.
Currently 1 other person reading this forum.


 UWA week 17 (1st semester, week 8) ↓
SVG not supported

Login to reply

👍?
helpful
12:17pm Mon 22nd Apr, Tin CP.

Annotations like @Override don't really change the code if I remember correctly, they just provide information to the compiler for error catching.

If a child method is intended to override a parent method, you should ALWAYS use @Override so the compiler can raise an error if it doesn't actually override anything (e.g. due to spelling error). Below, the two classes A and B have the same behaviour - in that they override the method hello() in the parent class Base. However, notice the intentional spelling mistake in A where we typed hwllo(). The code will not raise any error and the output will appear correct! But if we make the same mistake for class B then an error will be raised.

class Base {
    public void hello() {
        System.out.println("Base hello()");
    }
}

class A extends Base {
    public void hwllo() {
        System.out.println("A hello()");
    }
}

class B extends Base {
    @Override public void hello() {
        System.out.println("B hello()");
    }
}

class Main {
    public static void main(String[] args) {
        Base parent = new Base();
        A childA = new A();
        B childB = new B();
        parent.hello();
        childA.hwllo();
        childB.hello();
    }
}

You can find more annotations here https://www.geeksforgeeks.org/annotations-in-java/, some like @Deprecated tells the user that they shouldn't use a particular method (perhaps due to vulnerability or bad coding practice), but it doesn't change the behaviour of the code at all.


SVG not supported

Login to reply

👍?
helpful
4:28pm Mon 22nd Apr, Andrew G.

ANONYMOUS wrote:

Hi,

This can be seen as a general question or a question related to the assessed lab, but I was wondering about the appropriate uses of the @Override annotation.

Should we use @Override every time we try to override a method to catch errors, or is that unnecessary for some of the simpler methods? In other words, how do you determine when we should use the annotation before we override a method?

Also, are there any other annotations we should pay attention to, apart from @Override? Maybe I have terrible memory, but I don't recall us covering other relevant annotations in the unit so far. Thank you so much!

You do not need to use anything that we have not covered in the lectures, previous labs, or this lab sheet.

As was said in one of the lectures: The purpose of @Override is to express your intent that this method should override one from the parent class, so the compiler can warn you if it does not, as that likely indicates an error. As Tin Chi says, if it is your intent to override a method, you should probably say as such.


SVG not supported

Login to reply

👍?
helpful
10:22pm Mon 22nd Apr, ANONYMOUS

Thank you!! This helps.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  5:07AM Sep 06 2023
Privacy policy