File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whether the eg overrides or overloads

 
Praveen palukuri
Ranch Hand
Posts: 65
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
    • 0
    • 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
    • 0
    • 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: 24204
    34
    Chrome Eclipse IDE Mac OS X
    • 0
    • 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
    • 0
    • 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.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic