how to use super to access superclass fields?

hey guys. im trying to use super to access a field from superclass, but i cant do that. what am i doing wrong? im doing everything the same way like its written in the book.
public class Animal{
private String name = "Animal";
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Dog extends Animal {
private String breed;

public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}

public void myMethod(){
System.out.println(super.getName());
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("DogName", 123,"boxer");
dog.myMethod();//prints DogName
}
}
public class Animal{
private String name = "Animal";
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Dog extends Animal {
private String breed;

public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}

public void myMethod(){
System.out.println(super.getName());
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("DogName", 123,"boxer");
dog.myMethod();//prints DogName
}
}
thanks in advance
3 Replies
JavaBot
JavaBot11mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
bambyzas
bambyzasOP11mo ago
i got mixed up with the constructors. did some cleanup and everythings fine
JavaBot
JavaBot11mo ago
Post Closed
This post has been closed by <@611623200756989972>.
Want results from more Discord servers?
Add your server