Interface hybrid inheritance does not throw error in java? - java

I have three interfaces A, B and C and one class D. B and C extend A and D implements B and C. Interface A has a default method called "does". I was expecting to have a compile-time error or run-time error, however, the program was executed without error.
Both B and C inherit from A. Shouldn't the compiler be confused as to which inherited method be called on D since D inherits two default methods one from C and one from B.
All of the three interfaces and the class are defined in separate files. I have placed the code sequentially in the code area below for demonstration.
public interface A {
default public void does(){
System.out.println("A");
}
}
public interface B extends A {
}
public interface C extends A {
}
public class D implements B, C {
}
public class Sample {
public static void main(String[] args) {
new D().does();
}
}

Related

Inheritance in java(subclass of a subclass)

say we have three classes: class a , class b , class c;
class b inherits class a , if we define that class c inherits class b(which inherits class a) will the code give an error .If not the can we say that class c inherits class a;
in all i ask that can there be a subclass of a subclass??
In short, yes, you could definitely have a "chain" of inheritance. When you have a class A that inherits another class B, then it doesn't matter whether class B inherits from another class.
Though, you should keep in mind that a class is not able to inherit from multiple classes (it would throw a compiler error). Multiple inheritance in Java is achievable through the use of interfaces.
Yes, Multilevel inheritance refers to a mechanism where one can inherit from a derived class, thereby making this derived class the base class for the new class.
for example
Class A
{
public void methodA()
{
System.out.println("Class A method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
Class C extends B
{
public void methodC()
{
System.out.println("class C method");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA(); //calling grand parent class method
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}

Restricted inheritance in java

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.
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");
}
}
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.
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();
}
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.)
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.

Trying to understand the basics of polymorphism in Java.

Can somebody please tell me if the keyword 'extends' must be an used (in the syntax) of child classes that overide methods of their super class.
The word extends is used to indicate for the whole class that this class is a sub-class of another class. It is not related to whether the sub-class overrides certain methods or not, that is entirely up to the sub-class class. The sub-class may decide to override none, some, or all of the methods of the super-class. The sub-class may override only methods which are not marked as final in the super-class.
Here is a somewhat trivial example:
class A {
// This is the super-class.
public void myMethod() {...};
}
class B extends A {
// This extends above says: B is sub-class of A.
// So this class B is the sub-class of A.
// You can override methods of A here, like this
public void myMethod() {...};
// but you're not required to override them.
}
Polymorphism in java is a concept by which we can perform a single action by different ways.it uses 2 concepts such as method overloading and method over riding.
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilising the method's name.
the method over riding concepts uses the key word 'extends'.
We can extend a class by using the extends keyword in a class declaration after the class name and before the parent class.
public class ParentClass {
}
and we define child class like
public class ChildClass extends ParentClass {
}
// example of extending a class
class B {
int x = 0;
void f1 () { x = x+1;}
}
class C extends B {}
public class Test1 {
public static void main(String[] args) {
B b = new B();
b.f1();
System.out.println( b.x ); // prints 1
}
}
// example of extending a class, overwriting a method
class B {
int x;
void setIt (int n) { x=n;}
void increase () { x=x+1;}
}
class C extends B {
void increase () { x=x+2;}
}
public class Test2 {
public static void main(String[] args) {
B b = new B();
b.setIt(2);
b.increase();
System.out.println( b.x ); // prints 3
C c = new C();
c.setIt(2);
c.increase();
System.out.println( c.x ); // prints 4
}
}

Enforcing interface implementation in order to use another class

SITUATION: Say there is a class A and an interface B.
REQUIREMENT: If any class, say C, wants to create objects of A and use them, then that class will also have to implement interface B.Is there any way to enforce this condition?
WHY: Now a question may arise as to why I want to do such a thing. The reason is that when a class C creates objects of A and uses them, then those objects call certain methods of C. I want to declare those methods in interface B, so that C will invariably implement those methods.
Try this snippet:
public interface B {
// methods
}
public class A {
private final B b;
public A(B b) {
this.b = b;
}
...
}
public class C implements B{
// implement B's methods
public static void main(String[] arg) {
C c = new C();
A a = new A(c);
}
}
Since you say that objects of class A will call methods on C, they will have to keep reference to C somehow. Make this reference of type B and you are done.
That is
public class A {
public A(B arg) {
....
}
}
Then in C:
A a = new A(this);
That will force class C to implement interface B.

Runtime Java Class extension not working

Good day,
I have the following problem:
class B extends class A and methods of both are called by another method in another class after instantiating class B (example follows):
public class A{
//fields
//constructors
//methods
}
public class B extends A{
//fields
//constructors
//methods
}
public class CALLER{
public A getA(enum E){
return Factory.getB(otherobject,E);
}
}
public class Factory{
public static B getB(object o,enum e){
//do something with enums and get B
b = new B();
//populate b
return b;
}
}
Class B does not override any method of class A.
Somehow at compile time this doesn't get any error but at runtime class CALLER excepts: java.lang.NoSuchMethodError: Factory.getB(object,enum) A
My question is: if B extends A why a method from a different class can't return A even if its return clause returns a B object directly?
In fact changing:
public static B getB(object, enum);
with
public static A getB(object, enum);
solves the exception but then I get another exception (classCast) because obviously in other parts of the code it is awaiting a B type object, not an A.
Thanks in advance.
You would get this exception if you had compiled CALLER.java with another version of Factory.java that would have getB returning A, then updated Factory.java so that getB returns B, then recompiled Factory.java but not CALLER.java
UPDATE:
Perhaps you want to do something like this:
public abstract class Factory {
public abstract A getInstance(object o, enum e);
}
public class FactoryB extends Factory {
#Override
public B getInstance(object o,enum e){
//do something with enums and get B
b = new B();
//populate b
return b;
}
}
But the factory would then need to be instanciated.
The first one looks like a reflection error. The java reflection classes look for the exact method signature "A getB(Object,Enum)" and not "B getB(Object,Enum)".
The second, as long as you actually create an object of type B in your getB(..) method, it will return this object. The classCast exception will only be thrown if you create a new A instead of a new B.

Categories