OVERRIDING
OVERRIDING
But, rate of
interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7% a
nd 9% rate of interest.
import java.util.Scanner;
class Bank{
public int interest;
public int getInterest(int interest){
return 0;
}
}
class SBI extends Bank{
public int getInterest(int interest){
this.interest=interest;
return interest;
}
}
class RBI extends Bank{
public int getInterest(int interest){
this.interest=interest;
return interest;
}
}
class PNB extends Bank{
public int getInterest(int interest){
this.interest=interest;
return interest;
}
}
public class Problem1{
public static void main(String args[]){
SBI s=new SBI();
RBI a=new RBI();
PNB i=new PNB();
Scanner sc=new Scanner(System.in);
System.out.print("Enter SBI return rate:");
int sbi=sc.nextInt();
System.out.print("Enter RBI return rate:");
int rbi = sc.nextInt();
System.out.print("Enter PNB return rate:");
int pnb = sc.nextInt();
int m=s.getInterest(sbi);
int n=i.getInterest(rbi);
int o=a.getInterest(pnb);
System.out.println("Return of SBI Bank:"+m+" %");
System.out.println("Return of RBI Bank:"+n+" %");
System.out.println("Return of PNB Bank:"+o+" %");
}
}
2.Create a class 'Degree' having a method 'getDegree' that prints "I got a degree". It has two
subclasses namely 'Undergraduate' and 'Postgraduate' each having a method with the same
name that prints "I am an Undergraduate" and "I am a Postgraduate" respec vely. Call the
method by crea ng an object of each of the three classes.
class Degree{
public void getDegree(){
System.out.println("I got a degree");
}
}
class Undergraduate extends Degree{
public void getDegree(){
System.out.println("I am an UnderGraduate");
}
}
class Postgraduate extends Degree{
public void getDegree(){
System.out.println("I am an Post Graduate");
}
}
public class Problem2{
public static void main(String args[]){
Degree d1=new Degree();
Undergraduate u1=new Undergraduate();
Postgraduate p1=new Postgraduate();
d1.getDegree();
u1.getDegree();
p1.getDegree();
}
}
3. A class has an integer data member 'i' and a method named 'printNum' to print thevalue of 'i'.
Its subclass also has an integer data member 'j' and a method named 'printNum' to print the
value of 'j'. Make an object of the subclass and use it to assign a value to 'i' and to 'j'. Now call the
method 'printNum' by this object.
import java.util.Scanner;
class Value{
public int i;
public void PrintNum(int i){
this.i=i;
System.out.println("The value of i: "+i);
}
}
class subClass extends Value{
public void PrintNum(int j){
this.i=j;
System.out.println("The value of j: "+j);
}
}
public class Problem3{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter value of i: ");
int a=sc.nextInt();
System.out.print("Enter value of j: ");
int b=sc.nextInt();
Value v1=new Value();
subClass s1=new subClass();
v1.PrintNum(a);
s1.PrintNum(b);
}
}
4. A boy has his money deposited $1000, $1500 and $2000 in banks-Bank A, Bank B and Bank C
respec vely. We have to print the money deposited by him in a par cular bank. Create a class
'Bank' with a method 'getBalance' which returns 0. Make its three subclasses named 'BankA',
'BankB' and 'BankC' with a method with the same name 'getBalance' which returns the amount
deposited in that par cular bank. Call the method 'getBalance' by the object of each of the three
banks.
import java.util.Scanner;
class Bank{
public double balance;
public double getBalance(){
return 0.0;
}
}
class BankA extends Bank{
public double getBalance(double balance){
this.balance=balance;
return balance;
}
}
class BankB extends Bank{
public double getBalance(double balance){
this.balance=balance;
return balance;
}
}
class BankC extends Bank{
public double getBalance(double balance){
this.balance=balance;
return balance;
}
}
public class Problem4{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter Balance of Bank A , Bank B and Bank C: ");
double a=sc.nextDouble();
double b=sc.nextDouble();
double c=sc.nextDouble();
BankA b1=new BankA();
BankB b2=new BankB();
BankC b3=new BankC();
System.out.println("Balnce in Bank A: "+b1.getBalance(a));
System.out.println("Balnce in Bank B: "+b2.getBalance(b));
System.out.println("Balnce in Bank C: "+b3.getBalance(c));
}
}