Abstraction (training)
-
Please help me to understand how inheritance works in the context of work with abstract classes and their heirs, to challenge the methods and designers of both, and to identify a specific example with me.
There's an abstract class. Animaland there's his 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!"); }
}
♪ Animal Yes.
переменная nLegs
(legs)метод numLegs
, the requisition of this variable,геттер getLegs
to obtain its meaning andконструктор
, rear valueпеременной nLegs
byметода numLegs
♪♪ Monkey We are.
реализуем геттер
(exhaustive method to be implemented),создаем конструктор
which causesконструктор Animal
, assigning its local variable value2
I'm also writing an extra one.метод saySome
that puts the line in the console.Is that right? Then let's go on.
In the main class, write:
public class MainClass{
public static void main(String []args){ Animal jay = new Monkey (); System.out.println(jay.nLegs); /* jay.saySome()*/ ((Monkey) jay).saySome(); }
}
Issues:
What does that mean when the object is jay? Monkey♪
But how do I read the logic of that expression?Animal jay
like,
= new Monkey ();Класса Animal объект jay = новый объект jay класса Monkey
Why is the method I've
jay.saySome()
doesn't work? Why does it work?((Monkey) jay).saySome();
?
-
The ghetter is not reappointed but is being implemented. ♪
Animal
It's only declared but not implemented. ♪Monkey
You do it. Use the right terms, they don't just exist. Like the designer, he's not gonna work like a designer.Animal
"and he's inside just calling the designer.Animal
♪Expression
Animal jay = new Monkey();
means you're creating an object.Monkey
and assign it to a variable typeAnimal
becauseMonkey
It's too.Animal
♪ And then you work with the variable.jay
cAnimal
and in the head knowing that it's actually true.Monkey
♪The commented method doesn't work exactly because
jay
- it's common.Animal
without specifying. In classAnimal
No methodsaySome
♪ But since you know thatjay
YesMonkey
, you clearly lead the type and cause the method from class.Monkey
UPD
Responses to questions from the commentary:
(2) For the object of the class, the descendant inherits all the fields of the ancestor class and the object of the class is both the object of the descendant class and the object of the ancestor class (I don't get Class(), you don't need to know this at the current stage). The class in the declaration of a variable can be understood as a type, yes.
(3) He cannot inherit both structures. He has one of his own, his own, as described in the class.
Monkey
and he inherits the fields and methods of the class.Animal
♪(4)
Monkey jay = new Monkey();
- that's why not. The question is what you want. For example, if you have another class,Elephant extends Animal
it also implemented the methodgetLegs
and you'll want to put all the animals on the same list in your code and then find out how many legs they have:List<Animal> animals = fillList(); //Заполним список как-нибудь for(Animal animal : animals){ System.out.println(animal.getLegs()); }
In this piece of code, you don't care where the elephant is, and where the monkey is, you just know what they have in common (all have legs) and you use it. In case within the cycle
animal
it'll be a elephant, it'll be a elephant.getLegs
ClassElephant
ifanimal
It'll be a monkey, it'll be called.getLegs
ClassMonkey
♪