Super Class Constructor Java


This Simple Java Program is for calling the Base or Super Class Constructor in the Derived Class in Java. This is a part of Mumbai University MCA Colleges Java Practicals. 

Super Keyword is used to denote the base class for the inherited class or the child class. Super is used to call the base class constructor or the members variables of the base class. Super must be called in first line in the child or derived or inherited class constructor.


In the below program, The Class Interest is inherited by 2 derived class named Simple Interest and Compound Interest. They both call the base class constructor (Interest class constructor) using the java super keyword.


class Interest
{
double p,amt,n;
Interest(double a,double y)
{
p=a;
n=y;
}
void display()
{
System.out.println("\nAMOUNT = "+p+"\nNOS OF YRS ="+n);
}
}

class SimpleInterest extends Interest
{
SimpleInterest(double a,double y)
{
super(a,y);
}
void calc()
{
amt= (p*n*0.0925);
System.out.println("SIMPLE INTEREST="+amt);
}
}

class CompoundInterest extends Interest
{
CompoundInterest(double a,double y)
{
super(a,y);
}
void calc()
{
amt= p*Math.pow((1+(8.5/100)),n);
System.out.println("COMPOUND INTEREST="+amt);
}
}

class Bank
{
public static void main(String str[])
{
SimpleInterest s1=new SimpleInterest(25000,5);
CompoundInterest c1=new CompoundInterest(25000,5);
s1.display();
s1.calc();
c1.display();
c1.calc();
}
}

Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

No comments:

Post a Comment