aspose file tools*
The moose likes Beginning Java and the fly likes whether the eg overrides or overloads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "whether the eg overrides or overloads" Watch "whether the eg overrides or overloads" New topic
Author

whether the eg overrides or overloads

Praveen palukuri
Ranch Hand

Joined: Feb 10, 2005
Posts: 65
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

Joined: Feb 14, 2006
Posts: 121
Sorry mate, it does neither - it'll throw an exception.
Praveen palukuri
Ranch Hand

Joined: Feb 10, 2005
Posts: 65
may i know the reason to cause exception.
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
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 ]

    With Best Regards,
    Shyam Prasad Murarka
    Rodrigo Alvarez
    Ranch Hand

    Joined: Apr 10, 2006
    Posts: 75
    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


    It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
    Vishnu Prakash
    Ranch Hand

    Joined: Nov 15, 2004
    Posts: 1026

    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


    Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    Unfortunately, this mistake is an all too common one: referring to compile errors as "exceptions."


    [Jess in Action][AskingGoodQuestions]
    Alana Sparx
    Ranch Hand

    Joined: Feb 14, 2006
    Posts: 121
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: whether the eg overrides or overloads