Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • Re: Common Java Problems

    Problem description: Abstract method in non-abstract class
    Problem category: Compile-time Problems

    Diagnosis Difficulty: Easy-medium
    Difficulty to Fix: Easy


    This problem usually occurs when you inherit an abstract class or implement an interface. It can also occur if you forget to declare your class as abstract after you've declared an abstract method.

    public abstract class Base
    {
    	public abstract void doIt();
    }
    public class Child extends Base
    {
     
    }

    Error Messages

    In general, these error messages are very indicative of the problem, though their may be many error messages if you have multiple abstract methods.

    Child.java:1: Child is not abstract and does not override abstract method doIt()
    in Base
    public class Child extends Base
    Suggested fixes

    There are two common fixes to this problem. The more common of the two is to implement the methods. The other fix is to declare your class as abstract. Both fixes fulfill different purposes (one is providing an actual implementation for that method, the second only declares that an implementation exists and will be defined by inheriting classes), and you should pick the fix that fits your situation.

    Fix 1: Implement the method.

    // using the same Base class as above
    public class Child extends Base
    {
    	/**
    	 * 
    	 **/
    	@Override
    	public void doIt()
    	{
    		// sample implementation
    		System.out.println("I did it.");
    	}
    }

    Fix 2: Declare your class abstract.
    // using the same Base class as above
    public abstract class Child extends Base
    {
     
    }
    This article was originally published in forum thread: Common Java Mistakes started by helloworld922 View original post