Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whether the eg overrides or overloads

 
Praveen palukuri
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i've some doubt regarding the following eg

class A
{
protected metha()
{...}
}
class B extends A
{
private metha()
{....}
}

whether method in class B overrides/overloads method in class A.

Thank you in advance.
 
Alana Sparx
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry mate, it does neither - it'll throw an exception.
 
Praveen palukuri
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may i know the reason to cause exception.
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Praveen,
Over-riding: This is done when you provide a different implementation of a method (belonging to a super-class) in your class. This over-riding method should have the same signature as your superclass method. And along with that the access modifier that you specify for the over-riding method should have a scope equal to OR higher than the superclass method.
Hope I am not confusing matters here.
As you can see from the example you provided, the method in your subclass had a scope smaller than the superclass, so the compiler throws an error.
Over-loading: This is done when the method signature is the same but either of the following conditions should be met:
  • the number of parameters is different
  • the order of the parameters are different
  • the type of parameters are different


  • I have a feeling I have written a lot of information that may be incorrect. So just wait until an expert comes in and verifies the information. I just wrote my understanding of Overloading/Over-riding methods.
    [ April 20, 2006: Message edited by: Shyam Murarka ]
     
    Rodrigo Alvarez
    Ranch Hand
    Posts: 75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi praveen,

    your metha() in class B is (trying to) override the metha() in class A. Overloading is when you declare a method with the same name but a different signature (e.g.metha(int a) ).

    Anyway, in yuor example it would not work : you cannot reduce the visibility of a method when you override it (and private is a "smaller" visibility than "protected").

    => you would get a compile time error

    Cheers
     
    Vishnu Prakash
    Ranch Hand
    Posts: 1026
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Sorry mate, it does neither - it'll throw an exception.


    WRONG.

    It will NOT throw a runtime exception.

    You will get compile time error stating weaker access privilege
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24208
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Unfortunately, this mistake is an all too common one: referring to compile errors as "exceptions."
     
    Alana Sparx
    Ranch Hand
    Posts: 121
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I consider my slack, slapdash and lackadaisical attitude to semantics duly chastised and promise to sharpen up my attitude forthwith.

    Basically dude, it isnae gonnae wurk.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic