Results 1 to 11 of 11
  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Change of access modifier of overloaded method

    Hi all,

    I have doubt with regards to the change of access modifier of a overloaded method.

    I defined a method or overloaded method to be precise in the same class with the access modifier as private as opposed to the public of the original method. now when i call the private method, am thrown compile time error saying the method already exists!!!

    My question is if i put same method with different access modifier in the same class or outside the class that is in subclass then which one will be called and will not the compiler know that that is over loaded method?

    sample code below:

    class Somex

    {

    public void disp()


    {

    System.out.println("display!!");

    }

    private or protected void disp()

    {

    System.out.println("display this");

    }

    public static void main(String[] args)

    {

    Somex som = new Somex();

    som.display(); ????

    }

    }

    Thank you. --> this ones not the part of the code, this one for u all ..... :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Change of access modifier of overloaded method

    What happens when you compile and execute your code?

  3. #3
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Change of access modifier of overloaded method

    Java Code:
    public class Random {
    	public void disp1()
    	{
    		System.out.println("display!!");
    
    	}
    
    	private void disp()
    	{
    		System.out.println("display this");
    	}
    
    	public static void main(String[] args)
    	{
    	Random som = new Random();
    	som.disp1();
    	som.disp();
    	}
    }
    I don't know what you did, but chances are you already have a class using the 'display' method. Because I know I did and it came back as an error, but for me this method works.
    Last edited by adjit; 01-02-2012 at 07:57 PM.

  4. #4
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Change of access modifier of overloaded method

    it say the method is already defined.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Change of access modifier of overloaded method

    Then you must spell it differently or give it different arguments.

  6. #6
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Change of access modifier of overloaded method

    ok will try .thanks

  7. #7
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Change of access modifier of overloaded method

    Also, try using Eclipse. It would have highlighted that error as soon as you typed it and offered a correction

  8. #8
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Change of access modifier of overloaded method

    ok now it tells that the class cannot override the method. attempting to assign weaker access privilege. Wasnt i overloading it???

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Change of access modifier of overloaded method

    Post the code and the error messages.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Change of access modifier of overloaded method

    Quote Originally Posted by java4amanda View Post
    ok now it tells that the class cannot override the method. attempting to assign weaker access privilege. Wasnt i overloading it???
    Overloadeds method have different types of parameters; the methods from your original post are just identical, that's why the compiler was complaining. Trying to do the same in super and sub classes, where the one in the sub class has weaker access is also not allowed: a method that overrides (not overloads) another method can only have wider access.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Change of access modifier of overloaded method

    kool.

    Thanks for the enlightenment.

Similar Threads

  1. access modifier in constructor
    By javaPower in forum New To Java
    Replies: 1
    Last Post: 06-22-2010, 09:56 AM
  2. default access modifier
    By vishnugch in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 12:48 PM
  3. Regarding default access modifier?
    By makpandian in forum New To Java
    Replies: 2
    Last Post: 03-14-2009, 08:21 AM
  4. Annotations to change access level of method
    By jwilley44 in forum Advanced Java
    Replies: 1
    Last Post: 02-16-2009, 06:52 AM
  5. why we use public access modifier?
    By vichet in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 07:04 AM

Posting Permissions

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