Same method in Interface and Abstract class

前端 未结 4 1452
一个人的身影
一个人的身影 2020-11-30 03:02

I came to situation :

public interface Intr {
    public void m1();
}

public abstract class Abs {
    public void m1() {
        System.out.println(\"Abs.m1         


        
相关标签:
4条回答
  • 2020-11-30 03:08

    @Override ensures you override the method with no difference Interface or abstract superclass. So no error with override.

    On the other hand Interface method is also implemented in the superclass which is enough for Interface contracts.

    0 讨论(0)
  • 2020-11-30 03:13

    You are satisfying both conditions at once; ie. the one implementation is at the same time fulfilling the abstract class requirements and the interface requirements.

    As a note, unless you are using Intr in another inheritance chain, you don't need it. Also, it might make sense to move the implements Intr up to the abstract class definition.

    0 讨论(0)
  • 2020-11-30 03:17

    Here both the interface and abstract class have same method.

    You have one class name is hello and exteds abstract class and implement interface its true and you override meth1 method on hello class its fine and its compile correctly and not given any error but her you can't identify which class method is override like abstract class or interface.

    This is runtime polymorphism you cant create object of abstract class and interface but you can create reference variable of that. Here solution is you can't identify that on compile time its actual override at run time.

    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 讨论(0)
  • 2020-11-30 03:25

    You can only override methods defined in another class.

    Methods declared in an interface are merely implemented. This distinction exists in Java to tackle the problem of multiple inheritance. A class can only extend one parent class, therefore any calls to super will be resolved without ambiguity. Classes however can implement several interfaces, which can all declare the same method. It's best to think of interfaces as a list of "must have"s: to qualify as a Comparable your cluss must have a compareTo() method but it doesn't matter where it came from or what other interfaces require that same method.

    So technically you override Abs.m1() and implement Intr.m1() in one fell swoop.

    Note that this would be fine too:

    public class B extends Abs implements Intr {
    
        //m1() is inherited from Abs, so there's no need to override it to satisfy the interface
    }
    
    0 讨论(0)
提交回复
热议问题