0

error: mul(int,int) has protected access in Multiplication

Thoughts on what I'm doing wrong?

public class Multiplication{
            protected int mul(int a,int b){
                return (a*b);
            }
        }

        class ImplimentPkg extends Multiplication {
            public static void main(String args[]){

                Multiplication obj=new ImplimentPkg();
               //Here's the error
                System.out.println(obj.mul(2,4));

            }
            protected int mul(int a,int b){

                    System.out.println("");
                    return 0;
                }
        }
4
  • What do you mean exactly "Here's the error"? Commented Jan 13, 2016 at 13:05
  • System.out.println(obj.mul(2,4)) is not able to access its own method..which is overridden..! Commented Jan 13, 2016 at 13:05
  • Is the class ImplimentPkg in a different package from Multiplication? If yes, the error occurs because mul is protected and you cannot access it from another package. Commented Jan 13, 2016 at 13:06
  • why cant we acces form other package?? if i am not wrong we can use inheritance with procted method? Commented Jan 13, 2016 at 13:08

2 Answers 2

2

Java tutorial says:

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

You may think you have matched the second case(inheritence).

Multiplication obj = new ImplimentPkg();
System.out.println(obj.mul(2, 4));

This means you are using an instance of parent class Multiplication. Although the code is inside a method of subclass of Multiplication. It doesn't mean the instance obj has something to do with inheritance. And of course the mul(...) method is invisible. What you can do is: use keyword super.

public void bar() {
    Multiplication mul = new Multiplication();
    mul.mul(1, 2);  // <- error
    super.mul(1, 2); // correct
    Multiplication.foo(); // correct
}

Note: if you have protected static method in parent class, like:

public class Multiplication {
    private String name = "some";

    protected int mul(int a, int b) {
        return (a * b);
    }

    protected static void foo() {
        System.out.println("foo");
    }
}

Here the foo() method can be accessed everywhere in subclass, but not other classes. By the way, you shouldn't use protected static method, see reasons here.

Another related topic may be interested to you, see here.

Sign up to request clarification or add additional context in comments.

Comments

-1

Create each class in its own Java file

ImplimentPkg.java

package my;

class ImplimentPkg extends Multiplication {
    public static void main(String args[]) {

        Multiplication obj = new ImplimentPkg();
        System.out.println(obj.mul(2, 4));

    }

    protected int mul(int a, int b) {

        System.out.println("");
        return 0;
    }
}

Multiplication.java

package my;

public class Multiplication {
    protected int mul(int a, int b) {
        return (a * b);
    }
}

3 Comments

I am doing this in different package. I want that way .whare is the problem please help?
if they are in different packages, then you cannot use protected by definition. You'll have to use public instead.
@Leo that's not the answer what Pushpak is asking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.