Method overriding is the process of redefining a parent class’s method in a subclass.
In other words, if a subclass provides a specific implementation of a method that had already been defined in one of its parent classes, it is known as method overriding.
In method overriding:
The method in the parent class is called the overridden method.
The methods in the child classes are called the overriding methods.
The highlighted portions in the code snippet below show where method overriding is happening.
Let’s have a look!
This lesson welcomes you to the world of object-oriented programming.
We'll cover the following
Procedural programming
Object-oriented programming
Anatomy of objects and classes
User-defined data types
Procedural programming
If you are here, you are probably already familiar with the basics of programming and have used methods in your programs at some point.
Procedural programming is one programming paradigm among many others.
In procedural programming, a program is divided into smaller parts called methods. These methods are the basic entities used to construct a program. One of the main advantages of procedural programming is code reusability. However, the implementation of a complex real-world scenario becomes a difficult and unwieldy task.
Object-oriented programming
Object-oriented programming, also referred to as OOP, is a programming paradigm that includes, or relies, on the concept of classes and objects.
The basic entities in object-oriented programming are classes and objects.
Programming isn’t much use if you can’t model real-world scenarios using code, right? This is where object-oriented programming comes.
The basic idea of OOP is to divide a sophisticated program into a number of objects that talk to each other.
Objects in a program frequently represent real-world objects.
Assume there is a parent class named Shape from which the child classes Rectangle, Circle, Polygon, and Diamond are derived.
Suppose your application will need methods to calculate the area of each specific shape. The area for each shape is calculated differently, which is why you can’t have a single implementation. You could throw in separate methods in each class (for instance, getSquareArea(), getCircleArea() etc.). But this makes it harder to remember each method’s name.