Defining an abstract class without any abstract methods

JavaClassInheritanceMethodsAbstract

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

Yes you can. The abstract class used in java signifies that you can't create an object of the class. And an abstract method the subclasses have to provide an implementation for that method.

So you can easily define an abstract class without any abstract method.

As for Example :

public abstract class AbstractClass{
	
	public String nonAbstractMethodOne(String param1,String param2){
		String param = param1 + param2;
		return param;
	}
	
	public static void nonAbstractMethodTwo(String param){
		System.out.println("Value of param is "+param);
	}
}

This is fine.

Solution 4 - Java

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.

Solution 5 - Java

Yes we can have an abstract class without Abstract Methods as both are independent concepts. Declaring a class abstract means that it can not be instantiated on its own and can only be sub classed. Declaring a method abstract means that Method will be defined in the subclass.

Solution 6 - Java

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.

Solution 7 - Java

yes, we can declare an abstract class without any abstract method. the purpose of declaring a class as abstract is not to instantiate the class.

so two cases

  1. abstract class with abstract methods.

these type of classes, we must inherit a class from this abstract class and must override the abstract methods in our class, ex: GenricServlet class

  1. abstract class without abstract methods.

these type of classes, we must inherit a class from this abstract class, ex: HttpServlet class purpose of doing is although you if you don't implement your logic in child class you can get the parent logic

please check the HttpServlet source code

Solution 8 - Java

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.

Solution 9 - Java

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.

Solution 10 - Java

Yes you can. Sometimes you may get asked this question that what is the purpose doing this? The answer is: sometimes we have to restrict the class from instantiating by its own. In that case, we want user to extend our Abstract class and instantiate child class

Solution 11 - Java

Actually there is no mean if an abstract class doesnt have any abstract method . An abstract class is like a father. This father have some properties and behaviors,when you as a child want to be a child of the father, father says the child(you)that must be this way, its our MOTO, and if you don`t want to do, you are not my child.

Solution 12 - Java

Yes, you can define an abstract class without an abstract method. However, if there is no method inside you might better go with an interface

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVisaMasterCardView Question on Stackoverflow
Solution 1 - JavabiziclopView Answer on Stackoverflow
Solution 2 - JavaMarc WView Answer on Stackoverflow
Solution 3 - JavaShreyos AdikariView Answer on Stackoverflow
Solution 4 - JavaJegsValaView Answer on Stackoverflow
Solution 5 - JavaVivek VermaniView Answer on Stackoverflow
Solution 6 - JavaGordon GustafsonView Answer on Stackoverflow
Solution 7 - JavaV KView Answer on Stackoverflow
Solution 8 - JavaJeremyView Answer on Stackoverflow
Solution 9 - JavaKishan DhamatView Answer on Stackoverflow
Solution 10 - JavaNandkishor GokheView Answer on Stackoverflow
Solution 11 - JavaMohammad DavariView Answer on Stackoverflow
Solution 12 - JavafmucarView Answer on Stackoverflow