• Register
3 votes
1.5k views

Problem:

I am assuming a bunch of helpful souls are roaming here, in Kodlogs. I am trying to figuring out the abstract method in Java and wrote a simple program on it. Unfortunately, my program throws back an error and says,

AbstractClassExample.java:9: error: AbstractClassExample is not abstract and does
not override abstract method setAge(int) in MyClass
public class AbstractClassExample extends MyClass{
       ^
1 error

It may be tough to give the solution before knowing the original program. Please, find my program snippet below:

import java.io.IOException;
abstract class MyClass {
   public abstract void display();
   public abstract void setName(String name);
   public abstract void setAge(int age);
}
public class AbstractClassExample extends MyClass{
   public void display(){
      System.out.println("This is the subclass");
   }
   public static void main(String args[]) {
      new AbstractClassExample().display();
   }
}

Do you have any idea or solution for me? Thanks, guys.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

2 votes

Solution:

First of all, you have to have a clear concept of an abstract method and abstract class. A method is more like a machine that helps us to execute a particular job within a wink in our program. A method that doesn’t have a body is called an abstract method.  It has an abstract keyword before it like this:  public abstract myMethod();

On the other hand, A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.

So, understanding above all these, to make this program work you either need to override all the abstract methods of the super-class or, make the subclass abstract as shown below :

abstract class MyClass {
   public abstract void display();
   public abstract void setName(String name);
   public abstract void setAge(int age);
}
public abstract class AbstractClassExample extends MyClass{
   public void display(){
      System.out.println("This is the subclass");
   }
}

Never stop asking. Please, reply to this thread if you still have any confusion.

Thanks.

13 9 6
94,260 points
But if I declare the subclass as abstract then I cant create any object of that subclass in the main class. How do I solve that ?
0 votes

Solution:

Your class executes an interface WiimoteListener, which has a process onClassicControllerRemovedEvent. But, the process in interfaces are abstract, which implies they are originally only contracts with no executions. You require to do one of the things here:

  1. Execute this system and all the other systems that this interface declares, which create your class concrete, or

  2. Declare your class abstract, so it cannot be applied to instantiate instances, just used as a superclass.

How to reproduce that error as easily as possible:

Java code:

package javaapplication3;
public class JavaApplication3 {
    public static void main(String[] args) {
    }
}
class Cat implements Animal{

}

interface Animal{
    abstract boolean roar();
}

Displays this compile time error:

Cat is not abstract and does not override abstract method roar() in Animal

Why won't it compile?

Because:

  1. You made a class Cat which executes an interface Animal.

  2. Your interface named Animal has an abstract method called roar which should be overridden.

  3. You didn't given for method roar. There are many methods to eliminate the compile time error.

Remedy 1, have Cat override the abstract method roar()

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c = new Cat();
        System.out.println(c.roar());
    }
}
class Cat implements Animal{ 
    public boolean roar(){
        return true;
    }
}

interface Animal{
    abstract boolean roar();
}

Remedy 2, change Cat to be an abstract like this:

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c;
    }
}
abstract class Cat implements Animal{ 

}
interface Animal{
    abstract boolean roar();
}

Which implies you can't instantiate Cat anymore.

Remedy 3, have cat stop executing Animal

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c = new Cat();
    }
}
class Cat{ 

}

interface Animal{
    abstract boolean roar();
}

Which makes roar() no longer a contract for things that animals should know how to do.

Remedy 3, extend a class rather than executing an interface

package javaapplication3;

public class JavaApplication3 {
    public static void main(String[] args) {
        Cat c = new Cat();
        System.out.println(c.roar());
    }
}
class Cat extends Animal{ 

}
class Animal{
    boolean roar(){
        return true;
    }
}

The remedy to use rely on what the best model is to illustrate the problem being illustrated. The error is there to urge you stop "programming by brownian motion".

At the time you execute an Interface you should implement all the systems in that interface. You didn't execute onClassicControllerRemovedEvent.

It seems that WiimoteListener is an interface which identifies an onClassicControllerRemovedEvent system. Your class should identify all methods that an interface declares or it will not compile without errors.

It may also be that this class was designed applying a different version of the WiimoteListener interface (based on an older or newer version of the jar that adds that interface) and that version did not declare the above mentioned method. In case so, it may only need building against the version of the jar that your class was made to use.

10 6 4
31,120 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: is not abstract and does not override
asked Apr 13 jamuna1 30.2k points
0 votes
1 answer 27 views
27 views
Problem: I am new to the OOPS concepts. Is it compulsory for abstract classes to have abstract methods?
asked Sep 1, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 3 views
3 views
Problem: How do I fix 'is not abstract and does not override abstract method'.
asked Apr 13 jamuna1 30.2k points
0 votes
1 answer 121 views
121 views
I want to know how to fix this. Could anyone here help me out?
asked Dec 16, 2020 TeamScript 13.5k points
0 votes
1 answer 14 views
14 views
Solve this problem.
asked Dec 18, 2020 TeamScript 13.5k points
0 votes
1 answer 24 views
24 views
I am new to this. I was doing that but I got into some problems.
asked Dec 3, 2020 TeamScript 13.5k points
0 votes
1 answer 20 views
20 views
Problem: Array is a built in abstract data type in C programming. You can conceptualize it as a collection of similar elements. The element number of an array must be initialized when it’s declared. What if you wanted to store more elements in arrays after you’ve already declared it?
asked Dec 23, 2020 Mashhoodch 13k points
0 votes
1 answer 3 views
3 views
Problem: Method does not override method from its superclass - Android Fragment.How could solve this error?
asked Apr 22 sumaiya simi 43.9k points
0 votes
2 answers 314 views
314 views
Problem : I am beginner in Android Studio and I have written code, but I keep facing below error if I try to run my code: "Error: method does not override or implement a method from a supertype". I have already spent over four hours on this error but I am unable to fix it. Can somebody help me in fixing this issue?
asked Feb 26, 2020 mphil 2.3k points
0 votes
1 answer 3 views
3 views
Problem: Can I define an abstract class without adding an abstract method?
asked Apr 24 sumaiya simi 43.9k points