am studying java got stuck here please help me out also this are interview questions it-seems

1)Abstract class :

i know abstract classes have empty methods and full defined methods but am not able to understand what actually empty method means. please give a example to understand the same.

2) what is the main difference between abstract class and normal class?

3) interface : interface also have empty methods, the what is the difference between interface and abstract class? please give any real time example to understand, tried lot of theory to understand but it was not clear

有帮助吗?

解决方案

Abstract classes are essentially skeleton classes that you can extend and complete with sub-classes.

An example of an abstract class, which allows for some easier management down the line:

public abstract class CommandBase {

    public abstract String getName();

    public String getUsage() {
        return "/" + this.getName();
    }

}

You can then extend the class like so:

public class SomeCommand extends CommandBase {

    @Override //This is a required override
    public String getName() {
        return "some";
    }

    @Override //This is an optional override, method was not abstract
    public String getUsage() {
        return super.getUsage() + " <text>"; //refer to superclass for original method invocation
        //Would return '/some <text>'
    }

Interfaces are a "contract" specifying that classes which implement them will have that method.

You can implement multiple interfaces, but only extend one class. Abstract classes and interfaces cannot be directly instantiated.

Try reading up a bit on Java's documentation around them.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top