Concise Guide to Object-Oriented Programming by Kingsley Sage

Concise Guide to Object-Oriented Programming by Kingsley Sage

Author:Kingsley Sage
Language: eng
Format: epub
ISBN: 9783030133047
Publisher: Springer International Publishing


We can now create some classes in Java to represent the super class Item and the three sub classes Vase , Statue and Painting . The keyword extends is used to indicate in Java that a class is a sub class of another:

If we create this as a project in BlueJ, we see the super and sub class relationship clearly on the central panel:

The Vase class has a total of four attributes. Two of these are unique to it, and two are “inherited ” from its superclass. Similarly, the Painting class has six attributes, four unique to it, and two inherited from its superclass

So a super, base or parent class contains a basic set of attributes that are intended to form a part of the implementation of sub classes that are built from it. The sub classes have their own attributes that make them unique, and they inherit attributes from their superclass. Similarly, a sub class inherits methods from its super class. If we add a method to the super class Item , we find that instances of Vase , Statue and Painting will inherit it, and be able to call those inherited methods as if they had been defined in their own class definitions. In our example, a showValue() method has been added to the Item super class. A new AntiqueShop class has been created, and that creates v1 , an object of type Vase , and v1 then calls the new showValue() method:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.