Results 1 to 10 of 10
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default A question on @Override

    This is from the book Thinking in JAVA:

    “Overriding” can only occur if something is part of the base-class interface. That is, you must be able to upcast an object to its base type and call the same method (the point of this will become clear in the next chapter). If a method is private, it isn’t part of the base-class interface. It is just some code that’s hidden away inside the class, and it just happens to have that name, but if you create a public, protected, or package-access method with the same name in the derived class, there’s no connection to the method that might happen to have that name in the base class.
    And the exercise is:
    Show that @Override annotation solves the problem in this section.
    My question is:

    Can you give me an example for this exercise please ? What am I supposed to solve with @Override ? I have been trying a lot, but I am missing something. ( Please, please do not tell me to read what to book says, I HAVE. Please do not tell me to look the API, or give me links. I just want a simple example. Thank you. )

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on @Override

    This is what I understand:

    Java Code:
    class A
    {	
    	void bark()
    	{
    		System.out.println("BARK! in class A");
    	}
    	
    	void meow()
    	{
    		System.out.println("MEOW! in class A");
    	}
    
    }
    Java Code:
    public class C extends A
    {
    	
    	public static void main(String[] args)
    	{
    		C myC = new C();
    		myC.bark();
    		myC.meow();
    		
    		A myCC = new C();
    		myCC.bark();
    		
    	}
    	
    	void bark()
    	{
    		System.out.println("BARK in class C");
    	}
    	
    	@Override private void meow()
    	{
    		System.out.println("MEOW in Class C");
    	}
    }
    It is not solving anything ?

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: A question on @Override

    First of all, do you understand what it means to override a method?

    Basically, @Override is a hint to the compiler that some new method you have typed is supposed to override another method. It prevents you from accidentally doing something like this:

    Java Code:
    class A {
        public void someMethod() {
            // ...
        }
    }
    
    class B extends A {
        public void someMethd() {
            // perfectly legal but not what you wanted!
        }
    }
    Now if you do this instead:

    Java Code:
    class B extends A {
        @Override
        public void someMethd() {
            // compiler catches your typo
        }
    }
    Get in the habit of using standard Java naming conventions!

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on @Override

    Thanks,

    so Override is for catching typos then ?

    But how is it related to what this book is talking about ?

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on @Override

    I mean @Override is for catching typos then...

    ( I just tried with meow, did not catch my typo )

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: A question on @Override

    Quote Originally Posted by fatabass View Post
    But how is it related to what this book is talking about ?
    What is "the problem in this section" that you mention in the OP?
    Get in the habit of using standard Java naming conventions!

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on @Override

    “Overriding” can only occur if something is part of the base-class interface. That is, you must be able to upcast an object to its base type and call the same method (the point of this will become clear in the next chapter). If a method is private, it isn’t part of the base-class interface. It is just some code that’s hidden away inside the class, and it just happens to have that name, but if you create a public, protected, or package-access method with the same name in the derived class, there’s no connection to the method that might happen to have that name in the base class.

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: A question on @Override

    I guess @Override would make sure you don't think you're overriding a private method of the same name when you're actually not. That's not a situation I've encountered.
    Get in the habit of using standard Java naming conventions!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: A question on @Override

    Essentially, if you know you are overriding a method then you should use @Override to protect against the instances mentioned earlier, typos, mistaken parameters etc, as well as changes as a project evolves.

    For example I have an class:
    Java Code:
    public classGenInterface {
       public void something() {
       }
    }
    And a class (or several) that implement this.
    Java Code:
    public class GenInterfaceImpl1 implements GenInterface {
       @Override
       public void something() {
       }
    }
    Now, without that Override if the parent class changes I will not know that the child should change.
    Java Code:
    public classGenInterface {
       public void something(String code) {
       }
    }
    Will not produce an error in compilation.

    With the Override it will.

    It's a way of giving the compiler more information about what it is you are doing, rather than having it make guesses.

  10. #10
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on @Override

    Thanks. I guess the book wasn't very clear with the exercise.

Similar Threads

  1. Override methods
    By Splanger in forum New To Java
    Replies: 2
    Last Post: 07-07-2011, 03:38 PM
  2. Cannot override PasteAction
    By madroadbiker in forum Advanced Java
    Replies: 8
    Last Post: 06-10-2011, 01:43 PM
  3. Is it necessary to override setValueAt
    By pink123 in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2011, 12:17 AM
  4. @Override Annotation
    By Unsub in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 02:06 PM

Posting Permissions

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