4

I know that

class A  { } 
class B extends A  { }  
class C extends B { }

is completely legal and I can

C obj = new C();
obj.anyMethodfromA();

is possible. Now question is this What if I don't want to access class A methods in class C only class B methods should be inherited. Is this possible?

C anotherObj = new C();
anotherObj.anyMethodfromA(); //can be illegal?
anotherObj.anyMethodfromB(); //should be legal.
  • 1
    It's only possible if `B` does not extend `A`. Although you could override `A`'s methods in `C` and have them throw an `UnsupportedOperationException`, but this won't make them illegal to call at compile time. – marstran Nov 13 '15 at 09:53

5 Answers5

6

You cannot remove classA methods from classC, all you can do is override the classA method in classC and throw UnsupportedOperationException. like

class C extends B { 

    @override
    public void someMethodWasInClassA() {
        throw new UnsupportedOperationException("Meaningful message");
    }

}
rajuGT
  • 6,224
  • 2
  • 26
  • 44
  • any logical reason for this? –  Nov 13 '15 at 09:54
  • 3
    Why would you ever want it? Sorry but you have C is-a A. If that is not the case, then don't make it so. – Michael Lloyd Lee mlk Nov 13 '15 at 09:56
  • Side note: I the proposed solution this is a terrible idea. If you ever do it you deserve the pain and horror it will inflict on you. – Michael Lloyd Lee mlk Nov 13 '15 at 09:58
  • 1
    @user137124 Also, the best example for this is [Collections.unmodifiableCollection](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#unmodifiableCollection(java.util.Collection)) read the document, you will understand the purpose of this. But as mlk said, why would you want this? this throws Runtime exception. So be careful, and add related javadoc, when your method throws this exception, so the api users will have complete information about your apis. – rajuGT Nov 13 '15 at 10:03
1

Restricting access for certain subclasses is not possible. You could use interfaces instead to add certain a functionality to a specific class in addition to inheritance.

R.G.
  • 831
  • 1
  • 8
  • 16
0

There is no such fine-grained inheritance in Java. Once you've marked A methods protected, that extends down the entire heirarchy.

A workaround would be to reimplement the class A methods in class C, throwing appropriate run-time exceptions. But you cannot enforce a compile time failure.

(Note that you could achieve what you want in C++ with friendships: you'd mark the methods private in class A and make class B a friend of class A.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

At the moment C is-a A, however it sounds like you don't want that. So rather than have that maybe C has-a B or B has-a A.

Prefer composition over inheritance.

Community
  • 1
  • 1
Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81
0

You can use some sleight of hand using interface to hide the methodFromA but you cannot actually remove it.

class A {

    public void methodFromA() {
        System.out.println("methodFromA");
    }
}

class B extends A {

    public void methodFromB() {
        System.out.println("methodFromB");
    }
}

class C extends B {
}

interface D {

    public void methodFromB();

}

class E extends B implements D {

}

public void test() {
    // Your stuff.
    C obj = new C();
    obj.methodFromA();
    // Make a D
    D d = new E();
    d.methodFromB();
    // Not allowed.
    d.methodFromA();
    // Can get around it.
    E e = (E) d;
    e.methodFromA();
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213