Core Java - SPLessons
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. [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]

shape 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

shape Description

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

shape 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

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.