简体   繁体   English

我们可以在java的接口中定义抽象类吗?

[英]can we define abstract class inside the interface in java?

I am new to this concept, i know the property of interface and abstract.我是这个概念的新手,我知道接口和抽象的属性。 when i explain the concept to my friends, they asked me to create abstract class inside the interface.当我向我的朋友解释这个概念时,他们要求我在接口内部创建抽象类。

please tell me, is it possible to create abstract class inside the interface.请告诉我,是否可以在接口内创建抽象类。 i googled, but i am not able find the exact answer for my question.我用谷歌搜索,但找不到我的问题的确切答案。

i tried the below code,but i dont know how to cal the AbstractMethod.我尝试了下面的代码,但我不知道如何调用 AbstractMethod。

interface Student {

    public abstract class Subject {

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

class Data implements Student {

    public void ClassMethod() {
        System.out.println("method 2");
    }
}

public class NewClass {
public static void main(String[] args) {
    Data s=new Data(); 
 Student.Subject obj=new Student.Subject();// compiler error
    s.ClassMethod();
}
}

Wouldn't something like this be better?这样的事情不是更好吗?

interface Student {
    public abstract void sayHi();
}

class Data implements Student {
    @Override
    public void sayHi() {
        System.out.println("method 2");
    }
}

Yes, you can.是的你可以。 Here in the below example, I used Anonymous Class but you can use Lambda Expression also在下面的示例中,我使用了匿名类,但您也可以使用 Lambda 表达式

interface Student {

    public abstract class Subject {

        public abstract void AbstractMethod();
        public void show(){
            System.out.println("Show Method");
        }
    }
}

class Data implements Student {

    public void ClassMethod() {
        System.out.println("method 2");
    }
}

public class NewClass {
public static void main(String[] args) {
    Data s=new Data(); 
 Student.Subject obj=new Student.Subject(){
     public void AbstractMethod(){
            System.out.println("hi");
        }
 };
 obj.show();
 obj.AbstractMethod();
 s.ClassMethod();
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中,为什么在我们定义一个接口时不需要将其标记为抽象? - In Java, why don't we need to mark a class as abstract when we define an interface in it? 我们可以借助Java中的匿名类创建接口和抽象类的实例化吗? - Can we create instantiation of interface and abstract class with the help of anonymous class in Java? 为什么我们不能在没有匿名类方法的情况下在java中实例化接口或抽象类? - Why can't we instantiate an interface or an abstract class in java without an anonymous class method? 当我们在类中定义接口的抽象方法时,我们是“覆盖”或“实现”还是简单地说“定义”那些方法? - When we define the abstract methods of an interface in a class do we 'override' or 'implement' or simply say 'define' those methods? 我们可以在 java 中创建抽象 class 的实例吗 - can we make instance of abstract class in java Java-抽象类和接口 - Java - abstract class and interface 我们如何确定方法是属于抽象类还是接口? - How can we identify if a method belongs to an abstract class or an interface? 为什么我们不能在功能界面中重载抽象方法? (JAVA) - Why can't we overload a abstract method in a functional interface? (Java) 我们可以从接口内部引用 class 吗? - Can we reference a class from inside a interface? 我们是否应该总是定义接口和抽象类,即使只有一个扩展它们的类也是如此? - should we always define interface and abstract classes, even when there will only be only 1 class extending them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM