Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Inheritance, abstract classes
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Andrew Meikle
Inheritance, abstract classes
0 Answer(s)      3 years and 9 months ago
Posted in : Java Beginners


Hi. I wish to thank you for answering my last question, but I had figured it out, but the GUI was very helpful. I could use someones help again with abstracts & a demo account. I know you will be thinking how could I write the code but not get the simplest part.I'm getting a little lost with child classes, but here goes and don't know what to call. First, the code works, I just can't finish the demo application.For class, I had to create a class named Account for a bank. Included an integer field for the account number and a double field for the account balance.Also included a constructor that requires an account number and sets the balance to 0.0. I included a set method for the balance. Also included two abstract get methods-one for each field. I created two child classes of Account: Checking and Savings. Within the Checking class, the get method displays the string"Checking Account Information", the account number and the balance. Within the Savings class, added a field to hold the interest rate, and require the Savings get method to display the string "Savings Account Information", the account number, the balance, and the interest rate. I have to write an application that displays both Checkin and Savings objects. Save as Account.java, Checking.java, Savings.java. In addition, I had to write an application named AccountArray in which you enter data for a mix of 10 Checking and Savings accounts. Use a loop to display data, save as AccountArray.java. My code is as follows:

public Account(int an)

{

accountNumber = an;

accountBalance = 0.0;

}

public void setBalance(double bal)

{

accountBalance = bal;

}

public abstract int getNumber();

public abstract double getBalance();

public abstract void getInfo();

}
public class Checking extends Account

{

public Checking(int a)

{

super(a);

}

public void getInfo()

{

System.out.println("Checking Account Information " +

getNumber() + " $" + getBalance());

}

public int getNumber()

{

return accountNumber;

}

public double getBalance()

{

return accountBalance;

}

}
public class Savings extends Account

{

double rate;

public Savings(int a, double r){

super(a);

rate = r;

}

public void getInfo()

{

System.out.println("Savings Account Information " +

getNumber() + " $" +getBalance() + " rate " + rate);

}

public int getNumber()

{

return accountNumber;

}

public double getBalance()

{

return accountBalance;

}

}

This is the DemoAccounts application I am having problems with. I can only get the Savings to print:

public class DemoAccounts

{

public static void main(String[] args)

{

Savings mySvAcct = new Savings(2345, 0.03);

 mySvAcct.getInfo();

}

}

This is the AccountArray:

public class AccountArray

{

public static void main(String[] args)

{

Account someAccts[] = new Account[10];

int i;

someAccts[0] = new Checking(101);

someAccts[1] = new Savings(201, 2.1);

someAccts[2] = new Checking(102);

someAccts[3] = new Savings(202, 2.2);

someAccts[4] = new Checking(103);

someAccts[5] = new Savings(203, 2.3);

someAccts[6] = new Checking(104);

someAccts[7] = new Savings(204, 2.4);

someAccts[8] = new Checking(105);

someAccts[9] = new Savings(205, 2.5);

for(i = 0; i <10; ++i)

someAccts[i].getInfo();

}

}

Advertisement
View Answers

Related Tutorials/Questions & Answers:
Inheritance, abstract classes
Inheritance, abstract classes  Hi. I wish to thank you for answering... a little lost with child classes, but here goes and don't know what to call... method for the balance. Also included two abstract get methods-one for each
What is Abstract classes in Java?
What is Abstract classes in Java?  What is Abstrack class in Java...,   Hi, In Java programming language we are used the Java Abstract class to declare some common characteristics of subclasses.This Abstract class
Advertisements
Abstract class,Abstract methods and classes
Abstract methods and classes     ... is used with methods and classesAbstract Method An abstract method one...(); Abstract Class In java programming language, abstract classes
Interfaces and Abstract Classes - Development process
Interface and Abstract Classes?  Hi Friend, Interface: Java does.... It relate classes from different types of hierarchy. Abstract Class: Any class... be embedded into these classes. Abstract class prevents from instantiating a class
Abstract Classes - Java Interview Questions
Abstract Classes  Why we cann't instantiate a Abstract Classes? Even if an Abstract Class does not have any abstract methods, but declaring the class as abstract prevents it from creating an instance. Why? Thanks in Advance
Java Inheritance and abstract - Java Interview Questions
Java Inheritance and abstract  1) Why we need to write the separate Interface in which we are not doing any operation except declaring empty abstract... the abstract methods. If the subclass need to use this methods then he can write
Abstract methods and classes
Abstract methods and classes     ... is used with methods and classesAbstract Method An abstract method one...(); Abstract Class In java programming language, abstract classes
inheritance
inheritance  how does one implement inheritance in java
inheritance
inheritance  hi.. pleaseeeeee i need a quick help(answer) in creating aprogrm in java by using inheritance for car in such away that .... Car is parent class and both of Private and Public Car extends it then both of Bus
Java Abstract Class Example
the behaviour of classes from the outside environment. Abstract class can be created... and some are unique to specific classes. An abstract class should...Java Abstract Class Example In this section we will read about the Abstract
inheritance
inheritance  write a programwhich has three classes,namely,FirmClass,Animal and Dog,to illustrate how these class Dog inherits from Animal .The class Animal should have a method called move() which prints out the string"Animal
Inheritance in Java
Inheritance, one of the important concepts of Object Oriented Programming.... Benefit of using inheritance: A code can be used again and again Inheritance in Java enhances the properties of the class, which means
abstract class
abstract methods. Abstract classes cannot be instantiated, but they can be extended...abstract class   Explain the concept of abstract class and it?s use with a sample program.   Java Abstract Class An abstract class
Abstract class and abstract method
Some key points about abstract methods are as following An abstract modifier identifies abstract classes and methods An abstract methods cannot be private because it defined in the other class. An abstract methods cannot
Interface Vs Abstract Class
) and non-public members abstract classes may or may not be a little bit... Interface Vs Abstract Class       There are three main differences between an interface and an abstract
multiple inheritance - Java Beginners
)why java classes does not support Multiple Inheritance? JAVA CLASSES ONLY... Inheritance. Ans...>java classes doesn't support multiple inheritance,the practical...multiple inheritance  Why java does not support Multiple Inheritance
Inheritance,Inheritance in Java,Java Inheritance
Inheritance       To know the concept of inheritance clearly you must have the idea of class and its... this, super etc. As the name suggests, inheritance means to take something
multiple inheritance
multiple inheritance  why java doesn't support multiple inheritance? we can achieve this by using interface but not with classes? what is the main reason that java community implemented like
Abstract Factory Pattern
Abstract Factory Pattern       II Abstract Factory Pattern :  This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns
Java Abstract Class
. It may or may not have abstract methods. Abstract classes cannot be instantiated.... Which helps us to organize our classes based on common methods. An abstract class... is unique to specific classes. So you implement the superclass as an abstract
Inheritance
Inheritance       To know the concept of inheritance clearly you must have the idea of class and its... this, super etc. As the name suggests, inheritance means to take something
Abstract and Interface
abstract classes, java interfaces are slow as it requires extra indirection...Abstract and Interface  What is interface? When time is most suitable for using interface? Why we use interface instead of abstract? What
Multiple Inheritance
inheritance but by default Object class is super class for all the user defined classes and we can extend at most one class so each class can extend more than one class so java any how do multiple inheritance? Can you please explain
abstract class
abstract class  Can there be an abstract class with no abstract methods
abstract method
abstract method  Can we have abstract class with no abstract methods
abstract method
abstract method  is final method is in abstract class
Abstract class
Abstract class  what is an Abstract class
Abstract class
Abstract class  Can an abstract class be final
Uses of abstract class & interface - Java Beginners
classes from different types of hierarchy. Abstract Class: Any class... into these classes. Abstract class prevents from instantiating a class...Uses of abstract class & interface  Dear sir, I'm new
Difference between abstract class and an interface
an abstract class is class which cannot extends multiple classes because java...Difference between abstract class and an interface The difference between abstract class and interface is one of the most popular question in the interview
PHP Inheritance Class
values in classes, we must use the inheritance when the structure...Inheritance in PHP: Inheritance is a property of Object Oriented Programming.... With the help of this property classes and objects  get more flexibility, scalability
 

 

 

DMCA.com