Why and why can there be different names in the definition of an object?

  • The short answer is inheritance and interfaces. You can create an object of a class of the successor and assign a reference to it to the base class. - KoVadim
  • I will give an example, there is IDbConnection conn = new SqlConnection("constr") . If it is written like this, then at any time you can change SqlConnection to ODBCConnection, and if you write correctly, you can still use which database interface, using ready-made interfaces, you can deliver different implementations, for example, an MSSQL or ODBC implementation. And if SqlConnection с = new SqlConnection() is written SqlConnection с = new SqlConnection() then you will have to edit a bunch of code if you switch to ODBC. - nick_n_a
  • This polymorphism is one of the principles of OOP. Read the waste paper, everything is written there. Then tell us in your answer if you wish. - Sergey
  • I advise you to read about the design patterns - most of them are based on such things - Vlad Bayrak

3 answers 3

The answer to the question “why” is simple: because it is so stated in the language specification .

Each class can be inherited from other classes (and implement interfaces), and in the left part of the assignment there can be an ancestor class or an interface.

The more interesting question is "why." See, it's polymorphism . It often happens that several different things have much in common, are similar to each other, and you want to process them in the same way. For example, you have classes that represent an airplane and a helicopter, and you want to check for each of them whether repairs are needed. The trick is that you declare a common parent class “aircraft”, bring the general functionality there. Now you can

  • declare a list ( List<> ) of your aircraft, and contain in it both airplanes and helicopters
  • declare a function that takes an aircraft at the entrance and performs actions with it that are the same for an airplane and a helicopter (for example, an inventory).

Why immediately after calling the constructor to declare a variable of a more narrow type? There may be several options too:

  • You may want to emphasize that you treat an object as one of a group of similar objects. For example, you can create a plane, but emphasize that you use it as an aircraft, and you are not interested in whether it is a plane or a helicopter.
  • You can thus access data that is not available from the base class : for such classes

     class Base { public String name = "Base"; } class Derived extends Base { public String name = "Derived"; } 

    you will not get access to Base.name using the Derived link.

  • Hmm. An excellent answer, but I fully appreciated and understood it only now, when I went to edit my old questions, so that they would at least look less. - Anton Sorokin
  • @AntonSorokin: Thank you! - VladD

If I understand correctly, then you mean a consequence that is associated with inheritance.

For example, you want to write a class that displays a formatted message when the printFormatted(String) method is printFormatted(String) , but you want a lot of such formatters. Then you create the base class or FormatPrinter interface, describe in it the signature of the printFormatted method. Then you inherit from it, make your SuperFormatPrinter and implement the base class method in it as you like. And in your code, you have a FormatPrinter fprinter field. In this field, you can specify a link to any child class to FormatPrinter , and call only printFormatted , while the code being called does not know which method will be called.

This allows you to do something like modularity.

Read about inheritance.

    when using interfaces, you create a class object with, but the implementation of the methods will be from class A

    • for what? Those. I can implement methods from class C and call constructor A? - Anton Sorokin
    • This answer is a little confusing. It seems that the respondent missed and confused part of the words. - KoVadim