Part 3: Overriding vs. Overload in JAVA with example.

JAVA

Key Point on Overriding vs Overloading:

Overriding is a run-time concept while overloading is a compile-time concept. In any object-oriented programming language(OOP), The ability of a subclass to override a method. This process allows a class to inherit from a super class whose behavior is frequently similar in nature and then to modify behavior as needed. Use the “@Override” annotation to instructs the compiler that  intend to override a method in the superclass. If  the compiler detects that the method does not exist in one of the super classes, then it will generate an error.

Besides in Java OOP, two or more methods may have the same name if they differ in parameters such as different number of parameters, different types of parameters, or both. In These methods are called overloaded methods and this feature is called method overloading. These Overloading methods have the same name but accept different arguments. Method Overloading in Java with examples briefly discuss in below. Method Overloading  feature  allows a class to have more than one method having the same name.

Step by step overriding vs. overload in JAVA  features with real example is given below.

Overriding Features:

  • Same method name.
  • Same parameter list.

Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.

In below example Learn is parent class and Teach is child class and share() method is specific implementation of  method between these class.

class Learn{
 public void share(){
     System.out.println("class learn method share");
 }
}
class Teach extends Learn{
    
     public void care(){
     System.out.println("class Teach method care");
 }
    public void share(){
     System.out.println("class Teach method share");
 }
}

Here Learn and Teach both class uses same method same name,same perameter share().

Overloading Features:

  • Same method name.
  • Different parameter list.
class Learn{
 public void share(int n){
     System.out.println("class learn method share");
 }
   public void share(int n1,int n2){
     System.out.println("class Teach method share");
 }
}

During compilation time Compiler know they are different because they have different method signature.Two method signature are like:

share(int)

share(int,int)

if both method have

  • same method name.
  • same number of arguments.
  • and same type of arguments.

like below then compiler show compile error.

share(int n)

share(int n)

Draftsbook is a learning base blog. The blog share Article series based on Subject. For example:

  1. JAVA : JAVA  is a object-oriented programming(OOP) language. This language classes and objects are the two main aspects of OOP. The blog content discuss below principles of OOP:
    1. Encapsulation
    2. Inheritance
    3. Abstraction
    4. Polymorphism
  2. Data Structure: Data structure refers a several way of organizing data in a computer system.
  3. Android : Android software development  apps can be written using Kotlin, Java, and C++ languages” using the Android software development kit (SDK). This blog contains  android feature Navigation drawer implementation and its usage are mention.
  4. IELTS : IELTS  Four module with strategies are written hereby

5.Problem Solving : Problem solution on various online platform tips and solution are given here following platform.

0 Comments

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...