Java extends access to method

37 Views Asked by At

I create a simple code to understand and solve my problem, i create a class Animal and a class Dog witch extend the Animal class. i want to accese the method getFoots() of the class Dog
I have an error at the System.out.println(animal1.getFoots()); Undeclared method
I create o object Dog and an object Animal i set Animal = Dog and after what i want to access i variable tha is at Dog class What should i change in the System.out.println() line ?

public class Animal{
    private String name;
    
    public Animal(){
        name = "NoName";
    }
    
    public String getName(){    
        return name;
    }

}


public class Dog extends Animal{    
    private int foots;
    
    public Dog(int foots){
        this.foots=foots;
    }
    
    public int getFoots(){    //method i want to access
        return foots;
    }
    
}


public class main{
    public static void main(String[] args){
        Dog dog1 = new Dog(4); 
        Animal  animal1;
        animal1 = dog1;
        System.out.println(animal1.getName());
        System.out.println(animal1.getFoots());    //her i have the error Undeclared method
    }
}

Thanks for your time.

0

There are 0 best solutions below