Java Override, If any one declare a method in subclass, then make sure that same method should already exist in the parent class. The main advantage of method Java Override is that the class can provide its own specific implementation to a inherited method without even modifying the parent class. To go with following are the some rules are needed.
Classes should have Is-A relationship or inheritance.
Parent class parameters and child class parameters should be same.
Both the method names should be same in the parent class and child class.
Conceptual
Figure
From the above figure one can easily understand the difference between overloading and overriding.
Example
Java Override, Following is an example to understand the concept without method overloading.
[java]
class splesson{
void run(){System.out.println("Stop thinking Start coding");}
}
class Bike extends splesson{
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
} [/java]
The problem is that specific run time implementations are not provided to run method in a sub class. When compile the code following is the output will be generated.
[java]Stop thinking Start coding[/java]
Following is an example for method Java Override concept, make sure that method name and their parameters should be same in both parent class and child class.
[java]
class splesson{
void run(){
System.out.println("Hello SPLessons");}
}
class overriding extends splesson{
void run(){
System.out.println("Stop thinking Start coding");}
}
public static void main(String args[]){
overriding obj = new overriding();
obj.run();
} [/java]
Where declared only same method in parent class and child class as follows.
[java]void run()[/java]
Here IS-A relation ship has been followed.
[java]class overriding extends splesson[/java]
Output
When compile the code following is the result will be generated.
[java]Stop thinking Start coding[/java]
Example
Following is an another example to understand the concept.
[java]
public class Overriding {
public static void main(String[] args) {
System.out.println("Program starts");
DerivedClass dRef1=new DerivedClass();
dRef1.test1();
System.out.println("Program ends");
}
}
class BaseClass
{
//Overridden method
void test1()
{
System.out.println("Running test1() method of BaseClass");
}
}
class DerivedClass extends BaseClass
{
//Overrided method
void test1()
{
System.out.println("Running test1() method of DerivedClass");
System.out.println("Method Overriding");
}
}
[/java]
Output:
[c]
Program starts
Running test1() method of DerivedClass
Method Overriding
Program ends[/c]
Data overriding
Description
Java Override, Re-declaring and initializing super class variables in the sub class known as data overriding.
When an instance of subclass is created, always the subclass definition will be loaded.
To refer super class members java provides special keyword "super" using which all the non-static members of super class are referred.
Sub class can override the super class methods as well as overloaded super class methods.
Subclass Is - A type of super class.
In otherwords sub class should have features of super class inheritance.
Example
[java]
class Vehicle{
int speed=500;
}
class Bike4 extends Vehicle{
int speed=1000;
void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}
public static void main(String args[]){
Bike4 b=new Bike4();
b.display();
}
}
[/java]
Where super key word is used to print the parent class instance variable speed as follows.
[java]System.out.println(super.speed);[/java]
Output:
When compile the code following is the output will be displayed.
[c]
1000
[/c]
ExceptionHandling with MethodOverriding
Case-1:
In the event that the superclass method does not indicate an exception, subclass overidden method can't pronounce the checked exception however it can proclaim unchecked exemption.
[java]
package com.splessons;
import java.io.*;
class Parent{
void msg(){System.out.println("parent");}
}
class ChildException extends Parent{
void msg()throws IOException{
System.out.println("TestExceptionChild");
}
public static void main(String args[]){
Parent p=new ChildException();
p.msg();
}
}
[/java]
In the above example compile time error will be raised.
Case-2:
In the event that the superclass method declares an exception, subclass overridden method can announce same, subclass special case or no exception yet can't pronounce parent exception.
[java]import java.io.*;
class Parent{
void msg(){System.out.println("parent");}
}
class TestExceptionChild extends Parent{
void msg()throws IOException{
System.out.println("TestExceptionChild");
}
public static void main(String args[]){
Parent p=new TestExceptionChild();
p.msg();
}
} [/java]
In the above example compile time error will be raised.
Summary
Key Points
Super keyword is used to call the parent class variables.
Once any one declare final keyword before class, method then it can not be changed.