In this example I had
created 3 level of Inheritance, Class A as grandparent, Class B as parent and
Class C as child.
All these classes have
methods and I had mentioned in comments what kind of overriding is possible and
what gives error.
Example:
Grand Parent class A:
package blogspot.evaluatethecode.overriding;
import java.io.FileNotFoundException;
import java.io.IOException;
public class A {
public void test() {
System.out.println("Super class A");
}
public int test(int a) {
System.out.println("Super class A int test(int)");
return a;
}
public Double test(int a,int b) {
System.out.println("Super class A Double test(int,int)");
return (double) (a/b);
}
public Number test(double a) {
return a;
}
public String test(String a) {
return a;
}
public Object test(String a, String b) {
return a+" "+b;
}
public void test(char a) throws Exception{
System.out.println("Exception test(char)");
}
public void retest(char a) {
System.out.println("Super class without Exception test(char)");
}
public void testFile() throws FileNotFoundException {
System.out.println("Super class file not found exception");
}
public void testIO() throws IOException {
System.out.println("Super class IO exception");
}
public void testIO2() throws IOException {
System.out.println("Super class IO2 exception");
}
public void testIO3() throws IOException {
System.out.println("Super class Checked exception");
}
public void testIO4() throws NullPointerException {
System.out.println("Super class throws non checked exception");
}
protected void protectedTest() {
System.out.println("Super class protected");
}
public void publicTest() {
System.out.println("Super class public");
}
private void privateTest() {
System.out.println("Super class private");
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
public class A {
public void test() {
System.out.println("Super class A");
}
public int test(int a) {
System.out.println("Super class A int test(int)");
return a;
}
public Double test(int a,int b) {
System.out.println("Super class A Double test(int,int)");
return (double) (a/b);
}
public Number test(double a) {
return a;
}
public String test(String a) {
return a;
}
public Object test(String a, String b) {
return a+" "+b;
}
public void test(char a) throws Exception{
System.out.println("Exception test(char)");
}
public void retest(char a) {
System.out.println("Super class without Exception test(char)");
}
public void testFile() throws FileNotFoundException {
System.out.println("Super class file not found exception");
}
public void testIO() throws IOException {
System.out.println("Super class IO exception");
}
public void testIO2() throws IOException {
System.out.println("Super class IO2 exception");
}
public void testIO3() throws IOException {
System.out.println("Super class Checked exception");
}
public void testIO4() throws NullPointerException {
System.out.println("Super class throws non checked exception");
}
protected void protectedTest() {
System.out.println("Super class protected");
}
public void publicTest() {
System.out.println("Super class public");
}
private void privateTest() {
System.out.println("Super class private");
}
}
Parent class B:
package blogspot.evaluatethecode.overriding;
import java.io.FileNotFoundException;
import java.io.IOException;
public class B extends A {
public void test() {
System.out.println("Sub class B");
}
/*public Integer test(int a) {
System.out.println("Super class A int test(int)");
return a;
}*/
// This gives compilation error as return type incompatible with super class
/*public double test(int a,int b) {
System.out.println("Super class A Double test(int,int)");
return (double) (a/b);
}*/
// This also gives compilation error as return type incompatible with Super class
/*public double test(double a) {
return 1.0;
}*/
// This also gives compilation error as return type incompatible with Super class
public Double test(double a) {
return 1.0;
}
/*public Object test(String a) {
return a;
}*/
// This also gives compilation error as return type incompatible with Super class
public String test(String a, String b) {
return b+" "+a;
}
public void test(char a) {
System.out.println("Subclass without Exception test(char)");
}
/*public void retest(char a) throws Exception{
System.out.println("Sub class with Exception test(char)");
}*/
//Exception is not compatible with throws clause of Super class
/*public void testFile() throws IOException {
System.out.println("Super class file not found exception");
}*/
//Exception is not compatible with throws clause of Super class
public void testIO() throws FileNotFoundException {
System.out.println("Sub class File Not Found Exception");
}
public void testIO2() {
System.out.println("Sub class IO2 without exception");
}
public void testIO3() throws NullPointerException {
System.out.println("Sub class not checked exception");
}
/*public void testIO4() throws IOException {
System.out.println("Sub class throws checked exception");
}*/
//Exception is not compatible with throws clause of Super class
public void protectedTest() {
System.out.println("Super class protected sub class public");
}
/*protected void publicTest() {
System.out.println("Super class public");
}*/
// This gives compilation error - cannot reduce visibility of inherited method
private void privateTest() {
System.out.println("Super class private sub class private, it is not overrriden but both of them considered as separate method");
}
public static void main(String[] args) {
A a = new A(); // Runtime polymorphism/ Dynamic method dispatch
a.test();
a= new B(); // Runtime polymorphism/ Dynamic method dispatch
a.test();
System.out.println(a.test(2.2));
System.out.println(a.test("Vikas", "Thakur"));
/*System.out.println(a.test('a'));*/
//This gives compilation error PrintStream is not applicable for the arguments (void)
try {
a.test('a');
} catch (Exception e) {
e.printStackTrace();
}
try {
a.testIO();
a.testIO2();
a.testIO3();
} catch (IOException e) {
e.printStackTrace();
}
//If we call this subclass method without try catch it gives compilation error
a.protectedTest();
/*a.privateTest();*/
//This gives compilation error while calling - This method from super class is not visible
new B().privateTest();
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
public class B extends A {
public void test() {
System.out.println("Sub class B");
}
/*public Integer test(int a) {
System.out.println("Super class A int test(int)");
return a;
}*/
// This gives compilation error as return type incompatible with super class
/*public double test(int a,int b) {
System.out.println("Super class A Double test(int,int)");
return (double) (a/b);
}*/
// This also gives compilation error as return type incompatible with Super class
/*public double test(double a) {
return 1.0;
}*/
// This also gives compilation error as return type incompatible with Super class
public Double test(double a) {
return 1.0;
}
/*public Object test(String a) {
return a;
}*/
// This also gives compilation error as return type incompatible with Super class
public String test(String a, String b) {
return b+" "+a;
}
public void test(char a) {
System.out.println("Subclass without Exception test(char)");
}
/*public void retest(char a) throws Exception{
System.out.println("Sub class with Exception test(char)");
}*/
//Exception is not compatible with throws clause of Super class
/*public void testFile() throws IOException {
System.out.println("Super class file not found exception");
}*/
//Exception is not compatible with throws clause of Super class
public void testIO() throws FileNotFoundException {
System.out.println("Sub class File Not Found Exception");
}
public void testIO2() {
System.out.println("Sub class IO2 without exception");
}
public void testIO3() throws NullPointerException {
System.out.println("Sub class not checked exception");
}
/*public void testIO4() throws IOException {
System.out.println("Sub class throws checked exception");
}*/
//Exception is not compatible with throws clause of Super class
public void protectedTest() {
System.out.println("Super class protected sub class public");
}
/*protected void publicTest() {
System.out.println("Super class public");
}*/
// This gives compilation error - cannot reduce visibility of inherited method
private void privateTest() {
System.out.println("Super class private sub class private, it is not overrriden but both of them considered as separate method");
}
public static void main(String[] args) {
A a = new A(); // Runtime polymorphism/ Dynamic method dispatch
a.test();
a= new B(); // Runtime polymorphism/ Dynamic method dispatch
a.test();
System.out.println(a.test(2.2));
System.out.println(a.test("Vikas", "Thakur"));
/*System.out.println(a.test('a'));*/
//This gives compilation error PrintStream is not applicable for the arguments (void)
try {
a.test('a');
} catch (Exception e) {
e.printStackTrace();
}
try {
a.testIO();
a.testIO2();
a.testIO3();
} catch (IOException e) {
e.printStackTrace();
}
//If we call this subclass method without try catch it gives compilation error
a.protectedTest();
/*a.privateTest();*/
//This gives compilation error while calling - This method from super class is not visible
new B().privateTest();
}
}
Child class C:
package blogspot.evaluatethecode.overriding;
public class C extends B{
public void test() {
System.out.println("Third level hierarchy inheritance Sub class A method overriding");
}
public static void main(String[] args) {
A a = new C();
a.test();
A a2 = new A();
a2.test();
}
}
public class C extends B{
public void test() {
System.out.println("Third level hierarchy inheritance Sub class A method overriding");
}
public static void main(String[] args) {
A a = new C();
a.test();
A a2 = new A();
a2.test();
}
}
Output of Class B:
Super class A
Sub class B
1.0
Thakur Vikas
Subclass without Exception test(char)
Sub class File Not Found Exception
Sub class IO2 without exception
Sub class not checked exception
Super class protected sub class public
Super class private sub class private, it is not overrriden but both of them considered as separate method
Sub class B
1.0
Thakur Vikas
Subclass without Exception test(char)
Sub class File Not Found Exception
Sub class IO2 without exception
Sub class not checked exception
Super class protected sub class public
Super class private sub class private, it is not overrriden but both of them considered as separate method
Output of Class C:
Third level hierarchy
inheritance Sub class A method overriding
Super class A
Super class A
No comments:
Post a Comment