Reading:  

Quick introduction to Object oriented concepts in Java - Part 2 of series


Method overriding in Java

Method Overriding in Java is based on OOPS polymorphism concept which having two methods with same method signature and name and its implementation and actual methods is called during runtime depending on object type at runtime.

 

Method overriding allows flexible and extensible code in Java because it allows new functionality with minimal code change.

Rules for method overriding

  • The argument list should be exactly same as that of overridden method.
  • The return type should be same or a subtype of return type declared in original overridden method of super class.
  • Instance methods can be overridden only if inherited by subclass.
  • A final method cannot be overridden.
  • A static method cannotbe overridden but can be re-declared.
  • A subclass in different package will only override non-final methods declared public or protected.
  • Constructors cannot be overridden.

Example

// Method overriding. 
class X {
	inta, b;
	X(inti, intj) {
		a = i;
		b = j;
	}
	// display aanda
	void showab() {
		System.out.println("a and b: " + a + " " + b);
	}
}

class Y extends X {
	intc;
	B(inti, intj, intz) {
		super(x, y);
		z = c;
	}
	// display z – this overrides show() in A 
	void showab() {
		System.out.println("z: " + z);
	}
}

class Override {
	public static void main(String args[]) {
		YsubObcls = new Y(1, 2, 3);
		subObcls.showab();
	}
}


The output: z: 3

When showab() is invoked by object of type Y, the version of showab( ) defined within Y is used.The method showab() inside Y override the version declared in X. To access the Superclass method we need to use Super keyword.

class Y extends X {
	int z;
	Y(inti, int j, int z) {
		super(a, b);
		z = c;
	}
	void showab() {
		super.showab(); // this calls X's show() 
		System.out.println("z: " + z);
	}
}

The Output:

a and b: 1 2
z: 3

 

Here method super.showab() calls superclass version of method showab( ). Method overriding occurs only when names and type signatures of two methods are same.

Use of Java Method Overriding

  • Method overriding is runtime polymorphism
  • Method overriding provides specific implementation for a methodwhich is already provided by its super class.

 

Description

This tutorial will introduce you to Object oriented concepts in Java. Basic definitions and learning with code examples.

This tutorial has subdivided into 7 parts as listed below

  1. Understanding Inheritance    
  2. Understanding Overrides
  3. Understanding Polymorphism
  4. Understanding Abstraction
  5. Understanding Encapsulation
  6. Understanding Interfaces
  7. Understanding Packages

Let us know if you found any error in this tutorial. Your feedback is welcome as always.



Audience

Students seeking a quick overview on Java Programming language and its Object oriented features.

Learning Objectives

Learn Object Oriented concepts in Java language.

Author: Subject Coach
Added on: 10th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!