🌟 We hope you're enjoying our content! 🌟

To keep bringing you quality articles, please consider whitelisting our site in your ad blocker. It’s a small step that makes a big difference!

Thank you for your support.

Top Ad unit 728 × 90

Welcome !!! To The World of Programming

OOPS FEATURES

Oops

Object Oriented Languages - Method Overriding

Object Oriented Programming(OOPs):

The two most significant pillars of Object-Oriented programming are:

1. Polymorphism:

  • Polymorphism is a key principle in Object-Oriented Programming (OOP). 

  • It adds to code reusability, extensibility, and flexibility.

  • It allows you to write code that works with a variety of objects, making programs software more adaptable and easier to maintain. 

Types of Polymorphism:

1) Compile-Time Polymorphism (Static Binding or Early Binding):

  • It occurs at compile time.
  • The decision about which method to call is made by the compiler based on the method's name and the number and types of its parameters.
  • Multiple methods with the same name but different parameter lists can exist in the same class.
  • The appropriate method is selected based on the arguments provided during the method call.


Polymorphism means to have many forms. In real world, A human works in different roles like Principal, Clerk or a HOD. In Programming world, a function or a  Method exists  with the same name but with parameters of different data type.

 class ArthmeticOperations {
     // Function or Method Overloading
     //Compile time Polymorphism
     
    int add(int a, int b) {
        return a + b;
    }
    
    double add(double a, double b) {
        return a + b;
    }
}

       Ex. void add(int a, int b) and void add(double a, double b)            

2) Run-Time Polymorphism (Dynamic Binding or Late Binding):    

  • It occurs at runtime.
  • It allows objects of different classes to invoke the same method in a class-specific way.
  • It requires a superclass-subclass relationship.  

  • A subclass provides a specific implementation of a method that is overrides the functionality defined in its superclass.

2. Inheritance:

Inheritance is one of the fundamental concepts in object-oriented programming (OOP). It allows creating a new class (subclass or derived class or Child class) that inherits properties and behaviors (attributes or fields and methods) from an existing class (superclass or base class or Bose class). It creates a relationship between classes where the subclass is a specialized version of the superclass.

Inheritance permits reusability of code. It not only allows to reuse existing code but also allows to add new features or override existing features.

In this Post, the focus is on Method overriding. It allows to override the default functionality defined in the base or the parent class.

Method Overriding

Why Overriding? 
  • To override the default functionality of the inherited method from the base or the super class.
  • To provide new definition or functionality to the inherited method in the derived or the sub class.
Remember:

Method Overriding is possible only if the developer of the base or super class has allowed it. It can be prevented in different object-oriented languages using provided  keywords.

How to override?
  • By redefining the method of the Base class in the subclass with the same signature.
  • Same signature means the return type, method name and the parameters should match in datatype and the order of parameter list.  
  • Ex. ReturnType methodName(datatype parameter1, datatype parameter2,...)

Problem: Define a class Person with fields to store the first name and the last name of the person. Add a constructor to initialize the fields. Also add a method to display the complete name of the person in the first name and the last name format.

Define a class Employee that extends the Person class. Test the Employee class.


The problem statement now will be implemented in different Object-Oriented Languages. Here we explore the concept of overriding and its implementation in different object-oriented languages.

Method Overriding



1) Method Overriding in Java:

Java is one of the mostly used language in the world. Method Overriding in java can be done as follows:
  • Java has default overriding mechanism. If the developer of the Super or Base class have not prevented it, One can override the inherited Method in the class.
  • However, if the developer of Super or Base class has defined method (member function) with the final keyword , then the inherited method cannot be overridden in the sub or the derived class.
Explore More 

2) Method Overriding in C#.NET:

C#.NET is a programming created by Microsoft. It runs on .NET framework. Method Overriding in C#.NET is implemented as follows:
  • C#.NET do not allow default overriding like Java. 
  • A method defined with the virtual Keyword in the Base or Super class can only be overridden.
  • A method is redefined or implemented with the override keyword in the Sub or Derived class.

3) Method Overriding in Visual Basic .NET:

Visual Basic .NET is the next version of Visual Basic. It is also from Microsoft. Method Overriding is implemented as follows:
  • Visual Basic.NET also do not allow default Method Overriding.
  • A method defined with Overridable keyword in the Base or the Super class can be overridden in the sub class.
  • A method is redefined or implemented with the Overrides keyword in the sub or the derived class.

4) Method Overriding in Python:

Python is a high level, a general purpose programming language. Method Overriding is implemented as follows:
  • Python allows default Method Overriding.
  • However Method Overriding can be prevented by two underscores (i.e. makes it private, hence not inheritable) before the Method name in the base or super or the parent class.

5) Method Overriding in PHP:


PHP is a powerful Server-Side Scripting language used for developing a dynamic and an interactive web applications. Object-Oriented programming can be done with PHP and Method Overriding is implemented as follows:
  • PHP allows default Method Overriding.
  • However Method Overriding can be prevented by using final keyword before the Method name.
Object Oriented Languages - Method Overriding Reviewed by Syed Hafiz Choudhary on August 30, 2023 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.
!-- JavaScript for Ad Block Detection -->