I am getting a compile time error with Java:
MyClass is not abstract and does not override abstract method
onClassicControllerRemovedEvent(
wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent)
in wiiusejevents.utils.WiimoteListener)
Here is the class:
import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;
public class MyClass implements WiimoteListener{
public void onButtonsEvent(WiimoteButtonsEvent arg0) {
System.out.println(arg0);
if (arg0.isButtonAPressed()){
WiiUseApiManager.shutdown();
}
}
public void onIrEvent(IREvent arg0) {
System.out.println(arg0);
}
public void onMotionSensingEvent(MotionSensingEvent arg0) {
System.out.println(arg0);
}
public void onExpansionEvent(ExpansionEvent arg0) {
System.out.println(arg0);
}
public void onStatusEvent(StatusEvent arg0) {
System.out.println(arg0);
}
public void onDisconnectionEvent(DisconnectionEvent arg0) {
System.out.println(arg0);
}
public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
System.out.println(arg0);
}
public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
System.out.println(arg0);
}
public static void main(String[] args) {
Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true);
Wiimote wiimote = wiimotes[0];
wiimote.activateIRTRacking();
wiimote.activateMotionSensing();
wiimote.addWiiMoteEventListeners(new MyClass());
}
}
Can I get a better explanation of what this error means?
How to reproduce that error as simply as possible:
Java code:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
}
}
class Cat implements Animal{
}
interface Animal{
abstract boolean roar();
}
Shows this compile time error:
Cat is not abstract and does not override abstract method roar() in Animal
Why won't it compile?
Because:
You created a class Cat which implements an interface Animal.
Your interface called Animal has an abstract method called roar which must be overridden.
You didn't provide for method roar. There are many ways 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 means you can't instantiate Cat anymore.
Remedy 3, have cat stop implementing 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 must know how to do.
Remedy 3, extend a class rather than implementing 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 depends on what the best model is to represent the problem being represented. The error is there to urge you stop "programming by brownian motion".
Your class implements an interface WiimoteListener, which has a method onClassicControllerRemovedEvent. However, the methods in interfaces are abstract, which means they are essentially just contracts with no implementations. You need to do one of the things here:
Implement this method and all the other methods that this interface declares, which make your class concrete, or
Declare your class abstract, so it cannot be used to instantiate instances, only used as a superclass.
When you implement an Interface you must implement all the methods in that interface. You didn't implement onClassicControllerRemovedEvent.
It appears that WiimoteListener is an interface which defines an onClassicControllerRemovedEvent method. Your class must define all methods that an interface declares or it will not compile without errors.
It may also be that this class was designed using a different version of the WiimoteListener interface (based on an older or newer version of the jar that includes that interface) and that version did not declare the above mentioned method. If so, it may just require building against the version of the jar that your class was made to use.
Missing params, inconsistent param types, missing method definitions, check all of this out.
In my case:
public class California {
#override
public String transportation(String transportationType, String transportationId, String transportationArea)
{
return transportationType;
} public static void main(String[] args) {
California c = new California();
}
}
interface Oakland{
String transportation(String transportationType, String transportationId);
}
This did not compile because transportation method missed one of the params!
Related
I am getting a compile time error with Java:
MyClass is not abstract and does not override abstract method
onClassicControllerRemovedEvent(
wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent)
in wiiusejevents.utils.WiimoteListener)
Here is the class:
import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;
public class MyClass implements WiimoteListener{
public void onButtonsEvent(WiimoteButtonsEvent arg0) {
System.out.println(arg0);
if (arg0.isButtonAPressed()){
WiiUseApiManager.shutdown();
}
}
public void onIrEvent(IREvent arg0) {
System.out.println(arg0);
}
public void onMotionSensingEvent(MotionSensingEvent arg0) {
System.out.println(arg0);
}
public void onExpansionEvent(ExpansionEvent arg0) {
System.out.println(arg0);
}
public void onStatusEvent(StatusEvent arg0) {
System.out.println(arg0);
}
public void onDisconnectionEvent(DisconnectionEvent arg0) {
System.out.println(arg0);
}
public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
System.out.println(arg0);
}
public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
System.out.println(arg0);
}
public static void main(String[] args) {
Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true);
Wiimote wiimote = wiimotes[0];
wiimote.activateIRTRacking();
wiimote.activateMotionSensing();
wiimote.addWiiMoteEventListeners(new MyClass());
}
}
Can I get a better explanation of what this error means?
How to reproduce that error as simply as possible:
Java code:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
}
}
class Cat implements Animal{
}
interface Animal{
abstract boolean roar();
}
Shows this compile time error:
Cat is not abstract and does not override abstract method roar() in Animal
Why won't it compile?
Because:
You created a class Cat which implements an interface Animal.
Your interface called Animal has an abstract method called roar which must be overridden.
You didn't provide for method roar. There are many ways 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 means you can't instantiate Cat anymore.
Remedy 3, have cat stop implementing 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 must know how to do.
Remedy 3, extend a class rather than implementing 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 depends on what the best model is to represent the problem being represented. The error is there to urge you stop "programming by brownian motion".
Your class implements an interface WiimoteListener, which has a method onClassicControllerRemovedEvent. However, the methods in interfaces are abstract, which means they are essentially just contracts with no implementations. You need to do one of the things here:
Implement this method and all the other methods that this interface declares, which make your class concrete, or
Declare your class abstract, so it cannot be used to instantiate instances, only used as a superclass.
When you implement an Interface you must implement all the methods in that interface. You didn't implement onClassicControllerRemovedEvent.
It appears that WiimoteListener is an interface which defines an onClassicControllerRemovedEvent method. Your class must define all methods that an interface declares or it will not compile without errors.
It may also be that this class was designed using a different version of the WiimoteListener interface (based on an older or newer version of the jar that includes that interface) and that version did not declare the above mentioned method. If so, it may just require building against the version of the jar that your class was made to use.
Missing params, inconsistent param types, missing method definitions, check all of this out.
In my case:
public class California {
#override
public String transportation(String transportationType, String transportationId, String transportationArea)
{
return transportationType;
} public static void main(String[] args) {
California c = new California();
}
}
interface Oakland{
String transportation(String transportationType, String transportationId);
}
This did not compile because transportation method missed one of the params!
When the functional interface is in the same file where lambda overrides it, it compiles fine.
package test.test;
public class Base {
public static void main(String[] args) {
Interface1 a = n -> System.out.println(2*n);
}
}
interface Interface1 {
void multiplyByTwo(int x);
}
When the functional interface is in a separate file and Base class implements it, it fails to compile with Base is not abstract and does not override abstract method multiplyByFour(int) in Interface3 error.
package test.test;
public class Base implements Interface3 {
public static void main(String[] args) {
Interface3 b = n -> System.out.println(4*n);
}
}
package test.test;
public interface Interface3 {
void multiplyByFour(int x);
}
Is here something wrong? Why does lambda not override the method in the second case?
Your first example has:
public class Base {
which does not implement Interface1
However, your second example has:
public class Base implements Interface3 {
which DOES implement Interface3
Not sure what you are trying to do here, but this is intended behaviour:
Interfaces
When a class implements an interface, you must implement all of the methods into the class
For example:
public interface IFoo {
void bar();
}
and class:
public class FooImpl implements IFoo {
// must implement bar method in IFoo
public void bar() {
System.out.println("I did something");
}
}
Having a lambda in the main method does not constitute implementing interface methods.
Fix?
Just delete implements Interface3, you don't need to implement the interface in your class to be able to use it.
Please help me in this.
I created an interface with m1,m2 methods. Let say I want flexibility for the class who wish to override these methods, Let say, either they want to override m1, m2, both or none. So My objective is to find out name of respective design pattern.
Can this example be a name of any design pattern? e.g Adapter etc.?
Interface
public interface AInterface {
public void m1();
public void m2();
}
abstract class
public abstract class AAbstract implements AInterface {
public void m1() { }
public void m2() { }
}
main
public class Main extends AAbstract {
#Override
public void m1() {
System.out.println("I am m1");
}
public static void main(String[] args) {
Main main = new Main();
main.m1();
}
}
Thanks in advance!!
I have 3 classes. It seems basic question. But I can'nt find answer by googling.
public abstract class Test {
void t1()
{
System.out.println("super");
}
}
public class concret extends Test{
void t1()
{
System.out.println("child");
}
void t2()
{
System.out.println("child2");
}
}
public class run {
public static void main(String[] args) {
Test t=new concret();
t.t1();
}
}
How do I call abstract class t1 method? Since I cant create object from abstract class how do I call t1 in abstract class?
Thank you.
Either you create a concrete class which doesn't override the method, or within a concrete class which does override the method, you can call super.t1(). For example:
void t1()
{
super.t1(); // First call the superclass implementation
System.out.println("child");
}
If you've only got an instance of an object which overrides a method, you cannot call the original method from "outside" the class, because that would break encapsulation... the purpose of overriding is to replace the behaviour of the original method.
you should be able to do it using
Test test = new Test(){};
test.t1();
Abstract class means the class has the abstract modifier before the class keyword. This means you can declare abstract methods, which are only implemented in the concrete classes.
For example :
public abstract class Test {
public abstract void foo();
}
public class Concrete extends Test {
public void foo() {
System.out.println("hey");
}
}
See following tests:
public abstract class BaseClass {
public void doStuff() {
System.out.println("Called BaseClass Do Stuff");
}
public abstract void doAbstractStuff();
}
public class ConcreteClassOne extends BaseClass{
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassOne Do Stuff");
}
}
public class ConcreteClassTwo extends BaseClass{
#Override
public void doStuff() {
System.out.println("Overriding BaseClass Do Stuff");
}
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassTwo Do Stuff");
}
}
public class ConcreteClassThree extends BaseClass{
#Override
public void doStuff() {
super.doStuff();
System.out.println("-Overriding BaseClass Do Stuff");
}
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassThree Do Stuff");
}
}
public class Test {
public static void main(String[] args) {
BaseClass a = new ConcreteClassOne();
a.doStuff(); //Called BaseClass Do Stuff
a.doAbstractStuff(); //Called ConcreteClassOne Do Stuff
BaseClass b = new ConcreteClassTwo();
b.doStuff(); //Overriding BaseClass Do Stuff
b.doAbstractStuff(); //Called ConcreteClassTwo Do Stuff
BaseClass c = new ConcreteClassThree();
c.doStuff(); //Called BaseClass Do Stuff
//-Overriding BaseClass Do Stuff
c.doAbstractStuff(); //Called ConcreteClassThree Do Stuff
}
}
use keyword 'super' to do that
void t1()
{ super.t1();
System.out.println("child");
}
Make sure you use that in the overriden method though.
Your code seems to call t1(). However this is calling the concrete t1() because the abstract t1() has been overridden by the concrete class.
If you wish to call the abstract t1 method from main code, do not override the t1() in concrete.
Or you can create a method in the concrete class for example:
public void invokeSuperT1(){
super.t1();
}
Create an anonymous Inner class,
Abstract class:
abstract class Test{
abstract void t();
public void t1(){
System.out.println("Test");
}
}
Here is how to create anonymous inner class:
Test test = new Test() {
#Override
void t() {
//you can throw exception here, if you want
}
};
Call the class via the object created for abstract class,
test.t1();
An abstract class is used when we want that every class that inherited from our abstract class should implement that abstract method, so it is must to implement method otherwise it gives the compile-time error.
void t1()
{
super.t1; // means the parent methods
System.out.println("child");
}
For example: Bird class has method sing() and there other classes that inherited from it like the sparrow, Pigeon, Duck, so these all have sing method so we make Bird class Abstract and make the sing() method abstract in it so every child of bird that implements Bird class should have a method of sing() with its on implementation.
First Create abstarct class like as shown in link: Creating Abstract Class
Create Sub-Classs like as shown in link: Sub-class extending
Creating main method for executing this as show in link: Instanciate the subclass to access
Result as shown here: Result
I know that multiple inheritances between Interfaces is possible, e.g.:
public interface C extends A,B {...} //Where A, B and C are Interfaces
But is it possible to have a regular Class inherit from multiple Interfaces like this:
public class A implements C,D {...} //Where A is a Class and C and D are interfaces
A Java class can only extend one parent class. Multiple inheritance (extends) is not allowed. Interfaces are not classes, however, and a class can implement more than one interface.
The parent interfaces are declared in a comma-separated list, after the implements keyword.
In conclusion, yes, it is possible to do:
public class A implements C,D {...}
In a word - yes.
Actually, many classes in the JDK implement multiple interfaces. E.g., ArrayList implements List, RandomAccess, Cloneable, and Serializable.
public class A implements C,D {...} valid
this is the way to implement multiple inheritence in java
Yes, a class can implement multiple interfaces. Each interface provides contract for some sort of behavior. I am attaching a detailed class diagram and shell interfaces and classes.
Ceremonial example:
public interface Mammal {
void move();
boolean possessIntelligence();
}
public interface Animal extends Mammal {
void liveInJungle();
}
public interface Human extends Mammal, TwoLeggedMammal, Omnivore, Hunter {
void liveInCivilization();
}
public interface Carnivore {
void eatMeat();
}
public interface Herbivore {
void eatPlant();
}
public interface Omnivore extends Carnivore, Herbivore {
void eatBothMeatAndPlant();
}
public interface FourLeggedMammal {
void moveWithFourLegs();
}
public interface TwoLeggedMammal {
void moveWithTwoLegs();
}
public interface Hunter {
void huntForFood();
}
public class Kangaroo implements Animal, Herbivore, TwoLeggedMammal {
#Override
public void liveInJungle() {
System.out.println("I live in Outback country");
}
#Override
public void move() {
moveWithTwoLegs();
}
#Override
public void moveWithTwoLegs() {
System.out.println("I like to jump");
}
#Override
public void eat() {
eatPlant();
}
#Override
public void eatPlant() {
System.out.println("I like this grass");
}
#Override
public boolean possessIntelligence() {
return false;
}
}
public class Lion implements Animal, FourLeggedMammal, Hunter, Carnivore {
#Override
public void liveInJungle() {
System.out.println("I am king of the jungle!");
}
#Override
public void move() {
moveWithFourLegs();
}
#Override
public void moveWithFourLegs() {
System.out.println("I like to run sometimes.");
}
#Override
public void eat() {
eatMeat();
}
#Override
public void eatMeat() {
System.out.println("I like deer meat");
}
#Override
public boolean possessIntelligence() {
return false;
}
#Override
public void huntForFood() {
System.out.println("My females hunt often");
}
}
public class Teacher implements Human {
#Override
public void liveInCivilization() {
System.out.println("I live in an apartment");
}
#Override
public void moveWithTwoLegs() {
System.out.println("I wear shoes and walk with two legs one in front of the other");
}
#Override
public void move() {
moveWithTwoLegs();
}
#Override
public boolean possessIntelligence() {
return true;
}
#Override
public void huntForFood() {
System.out.println("My ancestors used to but now I mostly rely on cattle");
}
#Override
public void eat() {
eatBothMeatAndPlant();
}
#Override
public void eatBothMeatAndPlant() {
eatPlant();
eatMeat();
}
#Override
public void eatMeat() {
System.out.println("I like this bacon");
}
#Override
public void eatPlant() {
System.out.println("I like this broccoli");
}
}
Of course... Almost all classes implements several interfaces. On any page of java documentation on Oracle you have a subsection named "All implemented interfaces".
Here an example of the Date class.
It is true that a java class can implement multiple interfaces at the same time, but there is a catch here.
If in a class, you are trying to implement two java interfaces, which contains methods with same signature but diffrent return type, in that case you will get compilation error.
interface One
{
int m1();
}
interface Two
{
float m1();
}
public class MyClass implements One, Two{
int m1() {}
float m1() {}
public static void main(String... args) {
}
}
output :
prog.java:14: error: method m1() is already defined in class MyClass
public float m1() {}
^
prog.java:11: error: MyClass is not abstract and does not override abstract method m1() in Two
public class MyClass implements One, Two{
^
prog.java:13: error: m1() in MyClass cannot implement m1() in Two
public int m1() {}
^
return type int is not compatible with float
3 errors
Yes, it is possible. This is the catch: java does not support multiple inheritance, i.e. class cannot extend more than one class. However class can implement multiple interfaces.
An interface can extend other interfaces. Also an interface cannot implement any other interface.
When it comes to a class, it can extend one other class and implement any number of interfaces.
class A extends B implements C,D{...}