Core Java - SPLessons

Java Override

Chapter 16

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Override

Java Override

shape Description

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.


shape Conceptual
Figure

From the above figure one can easily understand the difference between overloading and overriding.

shape Example

Java Override, Following is an example to understand the concept without method overloading.

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();  
  }  
}  

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.

Stop thinking Start coding

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.

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();  
}  

Where declared only same method in parent class and child class as follows.

void run()

Here IS-A relation ship has been followed.

class overriding extends splesson

Output
When compile the code following is the result will be generated.

Stop thinking Start coding


shape Example

Following is an another example to understand the concept.

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");
		}
}

Output:

Program starts
Running test1() method of DerivedClass
Method Overriding
Program ends

Data overriding

shape Description

Java Override, Re-declaring and initializing super class variables in the sub class known as data overriding.

shape Example

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();  
     
}  
}  

Where super key word is used to print the parent class instance variable speed as follows.

System.out.println(super.speed);

Output:
When compile the code following is the output will be displayed.

1000

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.

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();  
  }  
}  

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.

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();  
  }  
}  

In the above example compile time error will be raised.

Summary

shape 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.