Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7085501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:25:56+00:00 2026-05-28T07:25:56+00:00

Hi have one class like this import java.util.ArrayList; public class MobilePhone { private String

  • 0

Hi have one class like this

import java.util.ArrayList;

public class MobilePhone {

    private String number;
    private ArrayList<Message> messages;



    public MobilePhone(String n) {
        this.number = n;
        this.messages = new ArrayList<Message>();
    }

    public String getNumber() {
        return number;
    }

    public void setMessages(Message messages) {
        this.messages.add(messages);
    }

    public ArrayList<Message> getMessages() {
        return messages;
    }

}

And then a Message class

public class Message {

    protected String sender;
    protected String receiver;
    protected String subject;
    protected String bodyText;
    protected int tipo;

    protected Message() {
        this.sender = this.receiver = this.subject =
        this.bodyText = "";
    }

    protected Message(String s, String r, String sbj, String b, int t ) {
        this.sender = s;
        this.receiver = r;
        this.subject = sbj;
        this.bodyText = b;
        this.tipo = t;
    }

    public String getSender() {
        return sender;
    }

    public String getSubject() {
        return subject;
    }

    public String getBodyText() {
        return bodyText;
    }

    public int getTipo() {
        return tipo;
    }


}

And one subclass

public class SMS extends Message {
    static int maxBodySize = 160;


    public void showMessage(){
        System.out.println("SMS");
        System.out.println("Subject: " + super.subject);
        System.out.println("Text: " + super.bodyText);
    }
}

On my code I have this:

    for (MobilePhone item : listaTelefones) {
         for (Message item2: item.getMessages()){
             ((SMS) item2).showMessage();
         }
    }

And it gives me this error:

Exception in thread "main" java.lang.ClassCastException: Message cannot be cast to SMS

Can’t I downcast Message to SMS so I can use the SMS showMessage() method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:25:57+00:00Added an answer on May 28, 2026 at 7:25 am

    Some of the items in the list are of class Message but not of class SMS. Therefore, you can not cast them to class SMS.

    Add something like this to ensure you are dealing with SMS:

    if (item2 instanceof SMS) {
        ((SMS) item2).showMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains a boolean field like this one: public class
import java.io.*; import java.util.*; public class Sort { public static void main(String[] args) throws
Say we have a class like this: import java.net.URL import xml._ class SearchData(xml: Node)
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
I've got a class with many string arrays. I'd like to have one generic
I'm looking at some code with this form: 1 package com.stackoverflow.java.questions; 2 import java.util.ArrayList;
This is our code: import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;
how to clear the surfaceView in android? I have a class like this -
I have a model called Contact which has_one guest like so. class Contact <

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.