The interface and method of the abstract class of the class override

Here is the code:

interface hi { public void meth1(); } abstract class Hullo { public abstract void meth1(); } public class Hello extends Hullo implements hi { public void meth1(){} } 

Question: The code compiles and all that. I wanted to know that meth1 () in the Hello class overrides what meth1 ()? Ont in an interface or in an abstract class and why?

+4
source share
3 answers

The answer is short: both .....

Actually, to be correct: you do not overlap any of them, you implement both of them with one method.

+10
source

We usually redefine an existing method that already has some definition. I mean, we are adding an extra bit to the function in the child class compared to the superclass. Since both methods are abstract, we can say that we are implementing an unrealized method.

You can take the link to create threads in Java, where we prefer to implement the Runnable interface compared to the extension of the Thread class.

0
source

Absolutely the right question.

Here, both the interface and the abstract class have the same method.

You have one class name, this is hello, extends the abstract class and implements its true interface, and you redefine the met1 method to the hello class, and its compilation is correct and no error is given, but it cannot determine which class method is overridden as an abstract class or interface.

This polymorphism at runtime you cannot create an object of an abstract class and interface, but you can create a reference variable. Here's the solution - you cannot determine that at compile time its actual redefinition at runtime.

  interface hi { public void meth1(); } abstract class Hullo { public abstract void meth1(); } public class Hello extends Hullo implements hi { public void meth1(){ System.out.println("hello"); } hi h= new Hello(); h.meth1();//its means interface method is override. and its decide when we call method. hullo hu= new Hello(); hu.meth1();//its means abstract class method is override. } 
0
source

All Articles