|
| 1 | +class Calculator{ |
| 2 | + |
| 3 | + |
| 4 | + public int add(int num1, int num2){ |
| 5 | + return num1+num2; |
| 6 | + } |
| 7 | + |
| 8 | + public int substract(int num1, int num2){ |
| 9 | + return num1-num2; |
| 10 | + } |
| 11 | + |
| 12 | +} |
| 13 | + |
| 14 | +class AdvCalc extends Calculator{ |
| 15 | + public int mult(int n, int m){ |
| 16 | + return n*m; |
| 17 | + } |
| 18 | + |
| 19 | + public int divide(int num, int deno){ |
| 20 | + return num/deno; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class ScientificCalc extends AdvCalc{ |
| 25 | + public int powerOfN(int a, int n){ |
| 26 | + if(n == 0)return 1; |
| 27 | + |
| 28 | + return a *powerOfN(a, n-1); |
| 29 | + } |
| 30 | + |
| 31 | + public int fibo(int n){ |
| 32 | + if(n <= 1) return n; |
| 33 | + |
| 34 | + return fibo(n-1) + fibo(n-2); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +public class Inheritance{ |
| 39 | + |
| 40 | + public static void main(String a[]){ |
| 41 | + Calculator cal = new Calculator(); |
| 42 | + int sum = cal.add(4,5); |
| 43 | + |
| 44 | + System.out.println("SUM IS : " +sum); |
| 45 | + |
| 46 | + int sub = cal.substract(6,3); |
| 47 | + |
| 48 | + System.out.println("Substraction IS : " + sub); |
| 49 | + |
| 50 | + AdvCalc calcu = new AdvCalc(); |
| 51 | + |
| 52 | + int add = calcu.add(4, 11); |
| 53 | + |
| 54 | + int subs = calcu.substract(30, 14); |
| 55 | + |
| 56 | + int mul = calcu.mult(4, 5); |
| 57 | + |
| 58 | + int div = calcu.divide(20, mul); |
| 59 | + |
| 60 | + System.out.println("Addition with Adv calc : " + add); |
| 61 | + System.out.println("Substraction with Adv calc : " + subs); |
| 62 | + System.out.println("Multiplication with Adv calc : " + mul); |
| 63 | + System.out.println("Division with Adv calc : " + div); |
| 64 | + |
| 65 | + |
| 66 | + ScientificCalc sci = new ScientificCalc(); |
| 67 | + int power = sci.powerOfN(2, 3); |
| 68 | + System.out.println(" a to the power of n : " + power); |
| 69 | + |
| 70 | + int fib = sci.fibo(4); |
| 71 | + System.out.println("Hey the fibonnaci no. : "+ fib); |
| 72 | + } |
| 73 | +} |
0 commit comments