Home Java Java - Inheritance Example Your Ad Here
 

Search on this site

Related Items

Advertisement Partners

Job Search

Click here to search all jobs Powered by Monster.

Advertisements

Study in Russia
Java - Inheritance Example PDF Print E-mail
User Rating: / 1
PoorBest 
Written by Admin   
Thursday, 19 February 2009 08:56

Inheritance Example

package com.interview.practice.util;

class SuperClass {

public static int add(int a, int b){
System.out.println( "in superclass");
return a+b;
}

public static int add1(int a, int b){
System.out.println( "in superclass");
return a+b;
}

public String add(String a, String b){

System.out.println( "in superclass");

return a+b;
}

}

public class Inheritance extends SuperClass {

public String name="kislay";
public static int i=6;
public static int j;

public String add(String a, String b){

System.out.println( "in subclass");

return a+b;
}

public static int add(int a, int b){

System.out.println( "in subclass");
return a+b;
}

public static void main(String args[]){

Inheritance in=new Inheritance();
// Inheritance in1=new SuperClass(); // type mismatch : cant convert from SuperClass to inheritnce;
// Inheritance in2=(Inheritance)new SuperClass(); //java.lang.ClassCastException thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
SuperClass b=new Inheritance();
SuperClass b1=new SuperClass();

System.out.println(""+add(2,3)); // subclass method wil be called
System.out.println(in.add("kis", "lay")); // subclass method wil be called
b.add(2,3); // superclass method wil be called
b1.add(4,5); // superclass method wil be called

b.add("kis", "lay"); // subclass method wil be called
b1.add("kis", "lay"); // superclass method wil be called

}
}

Output

in subclass
5
in subclass
kislay
in superclass
in superclass
in subclass
in superclass


Comments
Search RSS
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

 

Donate & Support Us

Enter Amount:

Login Form



Joomla Templates by Joomlashack