Defining an abstract class without any abstract me

2019-01-30 10:08发布

Can I define an abstract class without adding an abstract method?

11条回答
何必那么认真
2楼-- · 2019-01-30 10:12

Yes, you can declare a class you cannot instantiate by itself with only methods that already have implementations. This would be useful if you wanted to add abstract methods in the future, or if you did not want the class to be directly instantiated even though it has no abstract properties.

查看更多
欢心
3楼-- · 2019-01-30 10:13

YES You can create abstract class with out any abstract method the best example of abstract class without abstract method is HttpServlet
Abstract Method is a method which have no body, If you declared at least one method into the class, the class must be declared as an abstract its mandatory BUT if you declared the abstract class its not mandatory to declared the abstract method inside the class.

You cannot create objects of abstract class, which means that it cannot be instantiated.

查看更多
Luminary・发光体
4楼-- · 2019-01-30 10:13

You can, the question in my mind is more should you. Right from the beginning, I'll say that there is no hard and fast answer. Do the right thing for your current situation.

To me inheritance implies an 'is-a' relationship. Imagine a dog class, which can be extended by more specialized sub types (Alsatian, Poodle, etc). In this case making the dog class abstract may be the right thing to do since sub-types are dogs. Now let's imagine that dogs need a collar. In this case inheritance doesn't make sense: it's nonsense to have a 'is-a' relationship between dogs and collars. This is definitely a 'has-a' relationship, collar is a collaborating object. Making collar abstract just so that dogs can have one doesn't make sense.

I often find that abstract classes with no abstract methods are really expressing a 'has-a' relationship. In these cases I usually find that the code can be better factored without using inheritance. I also find that abstract classes with no abstract method are often a code smell and at the very least should lead to questions being raised in a code review.

Again, this is entirely subjective. There may well be situations when an abstract class with no abstract methods makes sense, it's entirely up to interpretation and justification. Make the best decision for whatever you're working on.

查看更多
Fickle 薄情
5楼-- · 2019-01-30 10:17

Of course.

Declaring a class abstract only means that you don't allow it to be instantiated on its own.

Declaring a method abstract means that subclasses have to provide an implementation for that method.

The two are separate concepts, though obviously you can't have an abstract method in a non-abstract class. You can even have abstract classes with final methods but never the other way around.

查看更多
祖国的老花朵
6楼-- · 2019-01-30 10:21

yes you can do that.

declaring class abstract means that class will not be instantiated by any other class.

and there should be at least one abstract method inside that and meaning of that you can declare abstract method in that class if you are not declaring method than its ok.

example:

public abstract class abs {

    protected int cx = 0, cy = 0;

    public void p() {
        System.out.print("hello");
    }
}

this will work for sure.

查看更多
干净又极端
7楼-- · 2019-01-30 10:22

Yes you can do it. Why don't you just try doing that?

查看更多
登录 后发表回答