Reputation: 292
Getting error "The inherited method in Abs.show() cannot hide the public abstract method in iface" for the below written code.
package com.xprodev;
abstract class Abs {
void show(){
System.out.println("in show");
}
abstract public void print();
}
interface iface {
void show();
void print();
}
public class BasicAbs extends Abs implements iface {
public static void main(String[] args){
BasicAbs a = new BasicAbs();
a.show();
a.print();
}
@Override
public void print() {
System.out.println("in print");
}
}
I can resolve the error by making show in Abs as public. But I have few questions
Upvotes: 1
Views: 653
Reputation: 178481
Why the language support for this usage?
It's not - that's why you get an error
What can be achieved if this is allowed?
Recall that BasicAbs
is-a Abs
.
If you allow "auto conversion" of package protected
to public
in case of a conflict with interface, you damage encapsulation, you can basically create some interface with a method for every method you want to expose from the base class (which might be someone else's class), and that's not a good idea.
on calling a.print() only implementation of print will be called . Is that make sense to call this implementation is of method in Abs class or iface interface. Can we know that?
Method m = BasicAbs.class.getMethod("print");
System.out.println(m.getDeclaringClass());
Will yield:
class BasicAbs
Upvotes: 2