Please help me understand how inheritance works in the context of working with abstract classes and their heirs, calling methods and constructors of both, and analyzing a concrete example with me.

There is an abstract class Animal in the example, and its descendant Monkey .

abstract public class Animal { public int nLegs = 0; public void numLegs(int nLegs) { this.nLegs = nLegs; } public abstract int getLegs(); //Constructor public Animal (int nLegs) { numLegs(nLegs) } } 

 public class Monkey extends Animals { public int getLegs(){ return this.nLegs; } public monkey(){ super(2); } public void saySome(){ System.out.println("Hello, I am a monkey!"); } } 

In Animal, there is a переменная nLegs (number of legs), метод numLegs that sets the value of this variable, a геттер getLegs to get its value, and a конструктор that sets the value of the переменной nLegs using метода numLegs .

In Monkey, we реализуем геттер (an abstract method must be implemented), создаем конструктор that calls конструктор Animal , assigning the value 2 to its local variable, and also write an additional метод saySome , that outputs a string to the console.

Is that right? Then we go further:

In the main class we write:

 public class MainClass{ public static void main(String []args){ Animal jay = new Monkey (); System.out.println(jay.nLegs); /* jay.saySome()*/ ((Monkey) jay).saySome(); } } 

Questions:

  1. What does such an expression mean when creating a jay object? jay cannot belong to an abstractive class, he knows Monkey , but then how to read the logic of this expression (now I'm reading Animal jay = new Monkey (); as a Класса Animal объект jay = новый объект jay класса Monkey

  2. Why jay.saySome() method, jay.saySome() I comment on, does not work? Why does ((Monkey) jay).saySome(); ?

    1 answer 1

    The getter is not reassigned, but implemented. In Animal it is only declared, but not implemented. In Monkey you implement it. Use the correct terms, they do not just exist. Similarly, with a constructor, it does not "work as an Animal constructor", but inside it only calls the Animal constructor.

    Animal jay = new Monkey(); means that you create a Monkey object and assign it to a variable of type Animal , since Monkey is also an Animal . And then you work with the jay variable as with Animal , while in your head knowing that this is actually also Monkey .

    The commented method does not work precisely because jay is a generic Animal , without clarification. And in the Animal class there is no saySome method. But since you know that jay is Monkey , you explicitly cast the type and call a method from the Monkey class.

    UPD

    Answers to questions from the Commander:

    2) For a descendant class object, all the ancestor class fields are inherited and the descendant class object is both an object of the descendant class and an ancestor class object (I do not go into the subtleties of getClass () now, you do not need to know this at the current stage). Class when declaring a variable can be understood as a type, yes.

    3) It cannot inherit both structures. He has his own, his own, described in the Monkey class, and he inherits the fields and methods of the Animal class.

    4) Monkey jay = new Monkey(); - so it is possible, why not. The question is what you want to get. For example, if you have another class, Elephant extends Animal , it also implements the getLegs method, and you want to collect all the animals into one list in your code, and then find out how many legs you have:

     List<Animal> animals = fillList(); //Заполним список как-нибудь for(Animal animal : animals){ System.out.println(animal.getLegs()); } 

    In this piece of code, you absolutely do not care where the elephant is, and where the monkey is, you just know what they have in common (everyone has legs) and use it. If inside an animal cycle it turns out to be an elephant, then getLegs of the Elephant class will be getLegs , and if the animal will be a monkey, then getLegs of the Monkey class will be getLegs .

    • 1) Thanks for the amendment in terms of learning. 2) That is, the parent class, passing its fields and methods to a descendant by inheritance, automatically becomes a “type” of a variable, which assigns the descendant objects? As a data type of a variable (int, String) but created by a user? 3) How to understand "at the same time knowing in your head that this is actually also Monkey." - does the jay object inherit both structures from the parent and descendant, or only from the descendant Monkey (which implements inherited from Animal)? 4) And why not so Monkey jay = new Monkey (); - Ved Monkey already registered as heir Animal? - Rumata
    • @MaximVelichkin Updated the answer - iksuy