• Register
3 votes
181 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

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,240 points
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 18 views
18 views
Problem: I am new to the OOPS concepts. Is it compulsory for abstract classes to have abstract methods?
asked Sep 1 Raphael Pacheco 4.9k points
0 votes
2 answers 113 views
113 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 mphil 2.3k points
0 votes
1 answer 354 views
354 views
Problem : I am trying to learn Java, I am very newbie however I wanted to know what I do to get the "run" it. I am facing below error: Static Error: This class does not have a static void main method accepting String[]. Below is my code: public class MyCaneirinho { public static void mycontar ... { System.out.println(j + x + y); } else { System.out.println(j + x + y + z); } } } } // MyCarneirinho
asked Dec 10, 2019 alecxe 7.5k points
1 vote
1 answer 16 views
16 views
Problem: Hello all, I just started learning java OOP. Things are getting clear to me gradually. My question is very simple. Can we override a private method in java? Please reply to this thread with a legit explanation. Thanks
asked Apr 9 Gavin 15.3k points
1 vote
2 answers 156 views
156 views
Problem: Hello Mate, I have a simple chunk of code written in Java programming language. I am trying to call a static function inside my main function. Everything is good except for one compile-time error. When I am trying to compile my program it throws back an error ... sampleString(); ^ 1 error Where I've been doing wrong all the day? I am looking forward to hearing from you guys. Thanks.
asked Apr 17 Gavin 15.3k points