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
Solved

Returning an object in a Vector.

Posted on 2002-05-23
3
203 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why my table column Id is not passed to java object? 4 44
How do I remove an object from a 3 40
servlet and mdb, jms error 1 33
learn programming 8 41
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

860 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