![]() What is shown to us are the speed at which the car is being driven and the litres of petrol available. ![]() For instance, while driving a car, we are not concerned about internal functions or mechanisms. The snapshot of output is shown below-Ībstraction is the process of hiding certain data from the users and showing only the required information to them. The same method Voice when used in Bird would output “Turr Turr” and when used with Duck will output “Quack Quack”. In this example, using the same method we can perform multiple tasks. With this, the same method can perform differently when invoked by the parent class and by the child class.Īn example of the polymorphism is shown below: In Method Overriding, the method of the parent class can be overridden by the child class. The methods will have the same name but different parameters can be used as input. In Method Overloading, a single method can be used in numerous ways and perform different functions. The polymorphism we usually come across two terms namely- Method overloading and Method overriding. From this, we can infer that a single variable can have multiple implementations depending upon its usage. Lastly, the same ground can be used for parking vehicles. A real-life example of polymorphism would be- consider an open ground, now this ground can be used for playing sports.īesides, it could also be used to organize weddings and concerts. Polymorphism allows the programmer to perform different tasks using the same variable or function. The output of the above code is:Īs the name suggests- Polymorphism is the ability of a variable or a function to exist in multiple forms. We have created an object of Team that can access the properties of the parent class. We have created a subclass called Team that inherits the parent class- Sponsor. In the above example, the Sponsor is the parent class with the owner being its attribute. To inherit the properties of the parent class into the child class, a keyword called extends is used. The classes that inherit the properties of the parent class are called the child class or subclass. The parent class is that class whose properties need to be inherited by other classes. The properties refer to the attributes and methods of the parent class. The term inheritance refers to inheriting the properties of one class to another. Now let’s proceed and talk about the crux of object-oriented programming. ![]() In the absence of a child class, the code cannot be accessed.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |