In one of the courses for studying JAVA heard the phrase:

"In the class of the heir, you can add fields and methods, thereby expanding the capabilities of the base class."

I understood this phrase so that if a method is declared in the class of the heir, then the method of the heir will be counted as the method of the parent class. This type of money box is obtained. But in fact, it does not work, it means I misunderstood. Explain where I am wrong.

public class Mother { void callSun(){ System.out.println("Игорь! Иди ко мне!"); } public class Sun extends Mother { void searchMother (){ System.out.println("Мама!!!"); } 

With instances of classes in hand, in the above code, the class Sun (heir) can use both its own method and the method of class Mother (parent), and the parent can no longer use the method of heir. Which is completely contrary to how I understood the quote.

  • 2
    sun is the sun . son is a son . - Regent
  • @Regent and Pallin for sure, thank you, but I also think that Sun Microsystems has chosen such a name for itself) - SergioBerluskony

1 answer 1

I understood this phrase so that if a method is declared in the class of the heir, then the method of the heir will be counted as the method of the parent class. This type of money box is obtained.

You misunderstood this phrase.

Suppose you have defined a certain base class ( superclass ) with your own fields and methods. Next, you defined a child class ( subclass ). In this case, the subclass has access to the fields and methods ( adjusted for access modifiers ) of the superclass.

A superclass is some foundation upon which subclasses are built.

Subclasses define their fields and methods - the subclass expands the capabilities of the superclass.

  • I now understood this, maybe some other possibility is expanding at the base class? What could this author mean by stating this? - SergioBerluskony
  • one
    @SergioBerluskony; Nothing extends from the base class. The extension is that the subclass has access to the fields and methods of the superclass and, in doing so, in the subclass defines its own fields and methods . - post_zeew
  • yes, it came at last! It's like ... my children continue my business and develop, I gave them everything I could for this) - SergioBerluskony