?
Solved

Returning an object in a Vector.

Posted on 2002-05-23
3
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
yoren earned 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

719 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