#Whys is this showing error ?

1 messages · Page 1 of 1 (latest)

buoyant breach
#
    int age;
    int name;
    public abstract void animalNoise();
    public abstract void printInfo();
}
public class Cat extends Animal{
    public void printInfo(String n){
        @Override
        this.name=n;
        System.out.println(this.name);
    }
}
public class Main
{
    public static void main(String[] args) {
        Cat c=new Cat();
        c.printInfo("Voyla");
    }
}
mental sealBOT
#

<@&987246399047479336> please have a look, thanks.

buoyant breach
#

Could anyone please help ?

warm heath
#

why is @Override there?

blissful helm
#

what is the error ?

pastel matrix
#

Whilst yes... That's an error. but in the tradition of teaching them to fish... What is the compiler error you're getting?

buoyant breach
#

If i dont use oveeride

blissful helm
#

not on a field

#

or a call

buoyant breach
#

This is the error I use override too

blissful helm
#

dude

#

it seems you're absolutely new to java

buoyant breach
pastel matrix
#

The original error about the incompatibility of the type is correct... Adding @Override is invalid and makes no sense there - leaving you with a different error.

shadow wolf
blissful helm
buoyant breach
pastel matrix
#

int name - this is the declaration of name in the parent class. When you say this.name you're referring to this field (of type int) but trying to assign it a reference to a String (n).

buoyant breach
shadow wolf
#

ur animal class declared

public abstract void animalNoise();
public abstract void printInfo();

that means that any implementation of this class, for example Cat, must provide these two methods - exactly as stated

#

but two issues with that:

#
  1. ur cat has no void animalNoise() method yet
  2. u also dont have a void printInfo() method yet, u have void printInfo(String), but thats not the same
#

the @Override annotation is helpful and we will get to that in a second - but note that it doesnt influence the actual override-mechanism in java.

#

the root issue is that ur simply not providing the methods stated by Animal

buoyant breach
#
    int age;
    int name;
    
    public abstract void printInfo(String n);
}
public class Cat extends Animal{
    public void printInfo(String n){
        @Override
        this.name=n;
        System.out.println(this.name);
    }
}
public class Main
{
    public static void main(String[] args) {
        Cat c=new Cat();
        c.printInfo("Voyla");
    }
}
mental sealBOT
# buoyant breach ```public abstract class Animal{ int age; int name; public abst...

Detected code, here are some useful tools:

Formatted code
public abstract class Animal {
  int age;
  int name;
  public abstract void printInfo(String n);
}
public class Cat extends Animal {
  public void printInfo(String n) {
    @Override
    this .name = n;
    System.out.println(this .name);
  }
}
public class Main {
  public static void main(String[] args) {
    Cat c = new Cat();
    c.printInfo("Voyla");
  }
}
shadow wolf
#

yeah, but is that meaningful?

#

why do u want printInfo to take an argument?

#

shouldnt it just print the info of the animal

#

without the user giving an extra string

#

and what about ur animalNoise method, why did u remove it entirely?

mental sealBOT
#

@buoyant breach

MOOC is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.

It consists of two parts, one at beginner, and another at intermediate level. The end of the course is marked by creating your own Asteroids game clone!

Even though the instructions show how to configure and use NetBeans for the course, you can use IntelliJ. To use IntelliJ, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.

Visit MOOC here: https://java-programming.mooc.fi/
(the course is available in both English and Finnish)

About the course - Java Programming

blissful helm
#

take a look at this course it'll help you learn the basics with good exercices

#

or follow your trusted learning platform

#

i see that you miss some essential basics, which may make our assistance difficult to understand it