inheritancedemo
inheritancedemo
/** * parent class */ package com.training; /** * @author arun * */ public class Addition { int a; int b; void demo(){ System.out.println("This program is the example of inheritance in java"); } void displayno(){ System.out.println("a and b :" +a +" " +b); } void addition(){ System.out.println("addition of a and b is: " +(a+b) ); } } /** * child class */ package com.training; /** * @author arun * */ public class Addition1 extends Addition{ int c; void thirdno(){ System.out.println("c :" +c); } void multiplication(){ System.out.println("multiplication of a,b and c is: " +(a*b*c)); } class SimpleInheritance{ } public static void main(String[] args) { Addition a1 = new Addition1(); a1.a = 2; a1.b = 4; a1.demo(); System.out.println("-----------------------------------------------"); System.out.println("contents of parent class Addition : "); a1.displayno(); a1.addition(); Addition1 a2 = new Addition1(); // public members of super class can be accessed by sub class a2.a = 4; a2.b = 5; a2.c = 3; System.out.println("contents of child class Addition1 :"); a2.thirdno(); a2.multiplication(); } } /* output This program is the example of inheritance in java ----------------------------------------- contents of parent class Addition : a and b :2 4 addition of a and b is: 6 contents of child class Addition1 : c :3 multiplication of a,b and c is: 60 */
Please send comments to
vgdarur.javafive@blogger.com
Copyright © 2008 - iForeRunner.com
http://www.iforerunner.com/