methodoverdidingdemo
methodoverriding
/** * parent class */ package com.training; /** * @author arun * */ public class Arun { int i; int j; Arun(int a,int b){ i = a; j = b; } void show(){ System.out.println("i and j : " +i +" " +j); } void sum(){ System.out.println("sum of i and j is: " +(i+j)); } void multiplication(){ System.out.println("multiplication of i and j is: " +(i*j)); } } /** * */ package com.training; /** * @author arun * */ public class Kiran extends Arun { int k; Kiran(int a,int b,int c){ super(a,b); k = c; } // overrides show() in superclass void show(){ System.out.println("k: " +k); } // overrides sum()in superclass void sum(){ System.out.println("sum of i j and k is: " +(i+j+k)); } public static void main(String[] args) { Kiran k1 = new Kiran(1,2,3); Arun a1 = new Arun(2,10); k1.show(); a1.sum(); k1.sum(); k1.multiplication(); a1.multiplication(); } } /* output k: 3 sum of i and j is: 12 sum of i j and k is: 6 multiplication of i and j is: 2 multiplication of i and j is: 20 */
Please send comments to
vgdarur.javafive@blogger.com
Copyright © 2008 - iForeRunner.com
http://www.iforerunner.com/