![]() |
![]() |
Java Interface | |
Java Class vs Interface | |
Java Multiple Inheritance | |
Java Nested Interface |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Method Overriding means declaration of same method in the subclass which is already present in the Base class in the inheritance.
In Method Overriding child class can give it's own implementation of the method of Parent class.
When an overridden method is called then, the method defined by the subclass is executed and method defined by the superclass is hidden.
Superclass methods are inherited by it's subclass and its used by the objects of subclass also.
class class1 { type method1() { ... ... } } class class2 extends class1 { type method1() { ... ... } }
In above syntax the method1 is redefined in the subclass
class Phone { String os,ram,company; String model,camera; int battery; Phone() //constructor for initialization { model = "Galaxy S9"; camera = "12 Mega Pixel"; os = "Android 8.0"; battery = 3000; } void display() // method defined { System.out.println("Phone Detailes are \n"); System.out.println("Model Name : "+model); System.out.println("Camera : "+camera); System.out.println("Batter power : "+battery+" mAh"); } } class Computer extends Phone { Computer() { os = "Windows 10"; ram = "8 GB"; company = "Asus"; } void display() // method defined again { super.display(); // for executing superclass display method System.out.println("Computer Detailes are \n"); System.out.println("Company : "+company); System.out.println("Operating System : "+os); System.out.println("RAM Capacity : "+ram); } } class Hardware { public static void main(String args[]) { Computer myComputer = new Computer(); myComputer.display(); // executes subclass display method only } }
Overloading and Overriding is a kind of polymorphism. Which means "having one name" and "many forms".
Following difference clearly specifies the difference between method overloading and method overridden concept
Parameter | Method Overloading | Method Overriding |
---|---|---|
Definition | In method Overloading, methods of the same class shares the same name but must have different number of parameters or parameters in different order. | In method Overriding, superclass and subclass have the same method name,same type and same number of parameters. |
Meaning | It means more than one method having same name but different type of parameters | It means base class method is redefined in the derived class having same type,method name and parameters list. |
Polymorphism | Method Overloading is a Compile time polymorphism | Method Overriding is a Run Time polymorphism |
Inheritance | It may or may not needed the inheritance to implement Method Overloading. | It always requires inheritance to implement Method Overriding. |
No of classes | In this doesn't require more than one class for overloading. | In this it requires at least two classes to implement overriding |
Example |
class first { int add(int a, int b) { return a+b; } float add(float a, float b) { return a+b; } } |
class first { int add(int a, int b) { return a+b; } } class second extends first { int add(int a, int b) { return a+b; } } |