The screenshot shows a section from Blinov’s book "Java Industrial Programming". Screen

It is not specified in what relation is the Course class with the CourseHelper class. This is a mistake and the author just forgot to add that CourseHelper extends Course or I understand something wrong?

  • one
    Stand method is a masterpiece, of course. Take a normal tutorial ) - Nofate

3 answers 3

The following happens

enter image description here

  • BaseCourse inherited from Course
  • BaseCourseHelper inherited from CourseHelper
  • CourseHelper in the CourseHelper method returns an instance of Course
  • BaseCourseHelper in the BaseCourseHelper method returns an instance of BaseCourse

Actually, the author of the textbook shows the possibility in the child class ( BaseCourseHelper ) to override in the method ( getCourse ) the return type ( Course ), but within its child classes ( BaseCourse ).

  • I can not say many thanks) helped a lot) - Marty McFly
  • And why is all this done? If I understand correctly, CourseHelper bch = new BaseCourseHelper (); will call the getCourse method with a BaseCourseHelper, after which this called method will return a BaseCourse object that will be assigned to the Course link here course = bch.getCourse, and here System.out.println (bch.getCourse (). id); prints the value of the id field for Course, which it would do if getCourse () returned not BaseCourse, but Course. - Marty McFly
  • Well, it's just a learning example to show what the principle will compile. In fact, Course can have methods that are overridden in BaseCourse. And the caller will work with the object, no matter how this Course was obtained. - Nofate

Most likely, CourseHelper is not related to the Course inheritance relationship. They are simply interconnected, so the class is written in “help” to make it easier to handle. BaseCourseHelper inherits CourseHelper, this is what can be derived from this code.

  • Then I just do not understand what is happening in this code) - Marty McFly
  • The first paragraph says that the Course class must be inherited from the source type (as I understand it, the source type will be our CourseHelper). - Marty McFly
  • Nothing of the sort, there is just a function definition with the return value of Course. - HasmikGaryaka
  • already understood, thanks - Marty McFly

Course is a subclass of CourseHelper class.

  • Now I'm a little confused. Is BaseCourse a subclass of Course? Can you please briefly describe all the relationships between the BaseCourse, Course, CourseHelper and BaseCourseHelper classes? - Marty McFly
  • Inheritance is an integral part of Java. ... The main inherited class in Java is called the superclass. The next class is called a subclass. It turns out that a subclass is a specialized version of the superclass that inherits all members of the superclass and adds its own unique elements. - daniil mankevich
  • This I know) I asked about this particular example. What is inherited from? - Marty McFly
  • This is not inheritance, but encapsulation - daniil mankevich
  • The basis of encapsulation in Java is the class. ... For this, methods and variables inside the class can have access modifiers (public, private). Encapsulation means that object data is not directly accessible to its clients. - daniil mankevich