Solved

Returning an object in a Vector.

Posted on 2002-05-23
3
177 Views
Last Modified: 2010-03-31
I have the following definition of a class:


class Bank {
     private Vector accounts;

     Bank() { // Constructor
          accounts = new Vector();
     }

     public void addAccount(BankAccount newAccount) {
          accounts.add(newAccount);
     }

     public BankAccount getAccount(int index) {

     return (accounts.get(index));

         
     }

A class named "BankAccount" has already been defined, containing a few Strings and Doubles. However, I want Bank.getAccount to return a BankAccount class when it is called, as I have attempted to do so above. However, the statement "return (accounts.get(index));" states that there is an "Incompatible type" error.

I know that the get() method returns an Object, but isn't the getAccount() method already defined as returning an Object? What am I doing wrong?

If you need more information, please let me know.
0
Comment
Question by:esotericlete
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 50 total points
ID: 7030833
You've declared your getAccount() method to return a BankAccount. However, Vector.get() returns an Object. You have to *cast* the result to a BankAccount:

public BankAccount getAccount(int index) {
  return (BankAccount) accounts.get(index);
}
0
 

Author Comment

by:esotericlete
ID: 7030838
Is this the case for every single method that returns an Object, but does not explicitly state what type of Object it returns?
0
 
LVL 7

Expert Comment

by:yoren
ID: 7030846
Yes. The key here is that you're trying to return an Object in place of a BankAccount. Every BankAccount is an Object, but not every Object is a BankAccount. That's why you have to cast in this case.

If it were the reverse (returning a BankAccount instead of an Object), then it would be valid, since every BankAccount is  an Object:

public Object someMethod() {
  BankAccount acct = new BankAccount();
  return acct;
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now