Solved

Returning an object in a Vector.

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

726 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