News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Overriding Ceocept in Java

Started by thiruvasagamani, Jul 31, 2008, 02:23 PM

Previous topic - Next topic

thiruvasagamani

Overriding in java

when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.

sample program on overriding:

class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}

void show() {
System.out.println("i and j: " + i + " " + j);
}
}

class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}

void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B class
}
}


What if I want to call show method in super class.....


sample program on overriding:

class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}

void show() {
System.out.println("i and j: " + i + " " + j);
}
}

class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
Thiruvasakamani Karnan