Which is the Best Java Web Framework ?

Java - Can we have an abstract class without abstract methods ?


Declaring a Class Abstract means that we can't instantiate on its own and is only meant to be sub-classed. Declaring a method abstract means that It's definition will be provided in the sub-class. Both are independent concepts. Yes we can declare a class abstract having no abstract methods. 

For example

public abstract class AbstractClass{

    public void method1(String x){
    }

    public String method2(String x){
    }
}

won't give any compile time or runtime errors.

Why such classes are rarely used ?

These are not widely used due to its impact on run time polymorphism. 

Though we can always override any method in the derived object and hence can use runtime polymorphism but that overriding is optional. In case a particular subclass doesn't override a method , the same definition of the base class will be picked. 

For example

public class DerivedClass extends AbstractClass{
    public String method2(String x){
    }
}

public class DerivedClass2 extends AbstractClass{
    public String method2(String x){
    }
}

In this example both Derived Classes have their own implementation of method2 but are carrying implementation of Method 1 from the base class. If we would have been doing run time polymorphism for method1, It wouldn't have given any polymorphic behavior irrespective of the object.


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus




×

Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


×

Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.