Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use method from parent class of hierarchy?

Using the following code:

public class Animal {
    public void a() {
        System.out.println("Animal");
    }
}

public class Cat extends Animal {   
    public void a() {
        System.out.println("Cat");
    }
}

public class BlackCat extends Cat {
    public static void main(String[] args) {

        BlackCat blackCat = new BlackCat();
        blackCat.a();
    }
}

How to use in child class BlackCat method a() from Animal but not from Cat?
I want to receive Animal in console.

like image 801
Bohdan Myslyvchuk Avatar asked Jan 31 '26 06:01

Bohdan Myslyvchuk


1 Answers

I guess you could add an addition function with a set prefix (this case _) and use that as your "super accessor" or what ever you want to call it.

public class Cat extends Animal {   
    public void a() {
        System.out.println("Cat");
    }
    public void _a() {
        super.a();
    }
}
like image 132
Olian04 Avatar answered Feb 02 '26 21:02

Olian04