Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protected class structure in Java?

I was wondering if there's a language feature in Java in which methods of a superclass would be invisible for members of a subclass:

 public class Subclass extends protected Superclass

or something. I'll give an example.

Here is your superclass.

public class A{
    public String getA(){...}
    public String getB(){...}
    public String getC(){...}
    public void setA(String a){...}
    public void setB(String b){...}
    public void setC(String c){...}
}

If you want to subclass A while protecting some of its methods, and you can't change access modifyers in methods unless you override them, you'd end up with something like this-

public class B extends A{
    private String getA(){return super.getA();}
    private String getB(){return super.getB();}//These four methods have
    private void setA(String a){super.setA(a);}//to be redeclared.
    private void setB(String b){super.setB(b);}

    public String getC(){return super.getC();}//These two methods can be
    public void setC(String c){super.setC(c);}//removed.
    public String getD(){...}
    public void setD(String d){...}
}

Either that or you can keep a private instance of A and have something like this:

public class B{

    private A obj;

    private String getA(){return obj.getA();}
    private String getB(){return obj.getB();}//These four methods can also
    private void setA(String a){obj.setA(a);}//be removed.
    private void setB(String b){obj.setB(b);}

    public String getC(){return obj.getC();}//These two methods are
    public void setC(String c){obj.setC(c);}//redeclared.
    public String getD(){...}
    public void setD(String d){...}
}

Can you have something that takes both in a way that you don't have to redeclare any methods?

like image 376
Bai Li Avatar asked Apr 11 '09 22:04

Bai Li


People also ask

What is a protected class in Java?

Basically, the protected keyword is an access modifier for method and variable of a class. When a method or a variable is marked as protected, it can be accessed from: Within the enclosing class. Other classes in the same package as the enclosing class. Sub classes, regardless of packages.

Is there a protected class in Java?

No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier).

What are protected fields in Java?

Definition and Usage. The protected keyword is an access modifier used for attributes, methods and constructors, making them accessible in the same package and subclasses.

Why protected is not allowed in Java?

Since there is no way to restrict this class being subclassed by only few classes (we cannot restrict class being inherited by only few classes out of all the available classes in a package/outside of a package), there is no use of protected access specifiers for top level classes. Hence it is not allowed.


2 Answers

There is no "non-public" inheritance in Java, unlike the situation in C++.

Inheritance creates a subtyping relation. Any instance of B is also an instance of A, and should respond to the same messages. If instances of B clearly don't respond to all messages that instances of A respond to, then inheritance would be inappropriate anyway.

Your last solution (B does not inherit from A) is the appropriate one: you don't create a subtyping relation, just use one type to (secretly) implement the other.

like image 178
Uri Avatar answered Sep 28 '22 01:09

Uri


I don't think it is or should be possible.

If you use inheritance so that B extends A, you should be able to use a B object as an A object. For instance, this would be possible:

A obj = new B();

Now the program has no way of knowing that you can't call some of A's public methods.

And without inheritance, redefinition of functions is unavoidable.

like image 33
Jordi Avatar answered Sep 28 '22 03:09

Jordi