java


Java inheritance: Why this program gives 0.0 in output


I have written this code. The output should calculate the interest of the Bank but it gives 0.0 as output. I have created a class named as Bank and extended it in ICICI class.
import java.util.Scanner;
public class Bank
{
static double rate;
// n = number of years
public double calculateInterest( double PrincipalAmount, double n)
{
double interest;
interest = (PrincipalAmount * n*rate) /100; // interest formula
return interest;
}
public static void main(String[] args)
{
Scanner s1 = new Scanner(System.in);
System.out.print("Enter PrincipalAmount :" );
double PrincipalAmount = s1.nextDouble();
Scanner s2 = new Scanner(System.in);
System.out.print("Enter Number of Years :" );
double n = s2.nextDouble();
ICICI ic;
ic = new ICICI();// new object created of ICICI Class
ic.rate = rate; // object call by reference
System.out.print("Interest of ICICI is " + ic.calculateInterest( PrincipalAmount,n));
}
}
public class ICICI extends Bank
{
double rate = 4;
}
you are doing following:
ic.rate = rate;
and rate is not initialized....
so ic.rate = 0.0;
you should by the way take a look at this:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
What does the 'static' keyword do in a class?
and this
http://www.oracle.com/technetwork/java/codeconventions-135099.html
calculateInterest method is using static rate variable not instance rate , inheritance does not apply on variable , it does not override variable. so default value of static rate will be 0.0 and hence calculateInterest will give 0.0 (because it is double)answer.
You have mistakenly altered the assignment statement:
ic.rate = rate;
Instead, it should be :
rate= ic.rate;
Thanks!

Related Links

Java: Confining Robot simlated keypresses to the application itself
Switch between multiple JFrames
my psvm class doesnt run the other classes and methods
Error in Connection to SQL Server Database
How to use images in SWT Text field?
Why does camel neglect proxy details set as part of the Header information
How to update a List of entities in Hibernate
Make static for non-static method
Concurrent collection size calculation
How to read path to file with button in JSP/Servlet? [duplicate]
Unable to get annotation in a server environment
How do I implement binary search on an array of objects of a user-defined class [duplicate]
InputMismatchError when scanning from file
How to get a specific key or value from a ConcurrentHashMap
Swing randomly throws NullPointer # different times each execution
Strange behavior from a while loop

Categories

HOME
mediawiki
kde
bpmn
cvs
paw-app
retrofit
cross-browser
flyway4
node-notifier
pheatmap
adfs
communication
nano-server
numeral.js
handsontable
php-7.1
quickfix
reactcsstransitiongroup
oxyplot
scichart
swingx
lcd
vlsi
tooltipster
greendao
webkitspeechrecognition
junit5
gtrendsr
usbserial
commit
vsts-build-task
gammu
force-layout
stacked
git-merge
restlet
uft-api
graphenedb
particles.js
websphere-mq-fte
wptoolkit
qwt
user-accounts
termination
fancybox-2
rdfs
integrity
statsd
drupal-6
carrot
qtwebview
pillow
iso8601
vmware-tools
pagedlist
skype4py
hill-climbing
qcustomplot
ipconfig
two-factor-authentication
jfugue
hsv
ios8-today-widget
qdialog
retina
content-length
mono-embedding
facebook-graph-api-v2.4
modalpopup
ng-animate
rdtsc
wordpress-theme-customize
app42
uv-mapping
geos
cdc
html4
limejs
rdoc
twrequest
ticoredatasync
postgresql-performance
spring-portlet-mvc
cufon
actionview
floating
remember-me
.nettiers
dmx512
infrastructure
windows-live-messenger
misv

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile