• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

annotations to naming patterns

Prefer annotations to naming patterns

I was reading above staemtent. I have not understood the statement, concept behind clearly.
Please advise. Any ideas, sugestions, sample code, links, resources highly appreciated. thanks in advance
0
gudii9
Asked:
gudii9
1 Solution
 
dpearsonCommented:
The idea here is that it's better to use a language feature (annotations) then a convention (how you name things) in order to make your code correct.

For example you could decide whenever you derive one class from another to name each method so it starts with "over" to indicate that it may have been overridden.  You methods would be:

class A {
   public String overGetName() { return "my name is A" ; }
   private void getNumber() { return 6 ; } // Not named over since can't be overridden
}

class B extends A {
    public String overGetName() { return "my name is B" ; }
}

The better approach is to use an annotation.  In this case the language has one - @Override so you'd instead write:

class A {
   public String getName() { return "my name is A" ; }
   private void getNumber() { return 6 ; } // Not named over since can't be overridden
}

class B extends A {
    @Override     <-- This annotation indicates we're overriding the method
    public String getName() { return "my name is B" ; }
}

Since it looks like you're just learning the language I'm not going to explain how to build an annotation - that's fairly complex stuff.  But this should help explain why they're better to use than just naming things carefully.

Doug
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now