It will be overwritten and properly implemented in the sub classes instead. On occasions, as this is the case in our example, the drive() method of the Car class is only defined but not implemented at all (an empty function, without code) in the Car class. This is what over-ride polymorphism means: when a method from a subclass has to be overwritten to override the method inherited form the parent class. The code used in both methods will be different and overwrite the code of the parent class. However this method will have to be implemented differently as you do not dive an electric car the same way you drive a petrol car. In this class, both the PetrolCar and the ElectricCar classes inherit the drive() method from the parent Car class. Let’s consider the following class diagram, where both the ElectricCar class and the PetrolCar class inherits from the Car class. There are two forms of polymorphism, over-riding and over-loading. In OOP these multiple forms refer to multiple forms of the same method, where the exact same method name can be used in different classes, or the same method name can be used in the same class with slightly different paramaters. In this case it will inherit the properties and methods of all the parent classes.įor example we could have a class representing hybrid cars, that would inherit the properties and method of both electric cars and petrol cars. It is possible for a class to inherit from two or more parent classes. The child class is also called sub-class or derived class. The parent class is also called master class or base class. The inherited properties and methods of a sub class do not need to be duplicated in a class diagram, as it is understood that they are inherited from the parent class: Methods: viewFullDescription(), addToShoppingBasket(), removeFromShoppingBasket(), previewContent().Attributes: name, description, priceg, author, genre, numberOfPages.Methods: viewFullDescription(), addToShoppingBasket(), removeFromShoppingBasket(), viewTrailer().Attributes: name, description, price, certificate, duration, actors.Methods: viewFullDescription(), addToShoppingBasket(), removeFromShoppingBasket(), play(), download().Attributes: name, description, price, artist, duration.The MP3, DVD and Book sub-classes will inherit the properties and methods of the parent class (Item) as represented in green below. Attributes: author, genre, numberOfPages.Attributes: certificate, duration, actors.Sub-classes can then be defined with additional attributes and methods which are more specific. Methods: viewFullDescription(), addToShoppingBasket(), removeFromShoppingBasket().This class would contain all the attributes and methods that each item will need: Item ClassThe code of such an online shop could be based around the use of a class called Item. OOP Programming is based around the following key concepts: It facilitates the re-usability of code.It facilitates the decomposition of a large system into smaller modules and help structure your code more effectively.cars can accelerate or slow down, turn, crash, etc.) For instance in a car racing game, all the cars, the buildings even the road signs would be objects with their own attributes (Physical dimensions, positions, colours, speed, etc…) and behaviours (e.g. ![]() ![]() Similarly to real life, an OOP computer model would be structured around different objects that have their own properties and behaviours and that interact with each other.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |