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 8367629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:12:29+00:00 2026-06-09T13:12:29+00:00

my code is:- class Building{} class Barn extends Building{ public static void main(String[]args){ Building

  • 0

my code is:-

class Building{}

class Barn extends Building{
    public static void main(String[]args){
        Building build1 = new Building();
        Barn bar1 = new Barn();
        Barn bar2 = (Barn)build1;
        Object obj1 = (Object)build1;
        String str1 = (String)build1;    //also an error over here
        Building build2 = (Building)bar1;
    }
}

I am new to Java. so please can anyone explain me how the instantiation is being carried out here, like how the access is been given to each of the objects.

Also there is an error in the code please help me to rectify it too.

  • 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-06-09T13:12:31+00:00Added an answer on June 9, 2026 at 1:12 pm

    Given this Barn bar2 = (Barn)build1;

    The part after the equal((Barn)build1) means that you are typecasting build1 from type Building to the type Barn and passing it’s value to bar2.

    You could do that because any Barn is a Building, as you extended in class definition (Barn extends Building). You can also typecast any class type to Object class, because in Java, all classes extends from Object, aka everything is a Object.

    You are having that error when trying to cast it as String because Building has no conection with String class. Got it?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class Test { public static void main(String[] args) {
Consider following code public class City { public string Name { get { return
Some code for context: class WordTable { public: WordTable(); ~WordTable(); List* GetListByAlphaKey(char key); void
My code class Union { //Search Function static boolean search(int A[], int i) {
I have the code: class Point3D{ protected: float x; float y; float z; public:
I have this code: class LFSeq: # lazy infinite sequence with new elements from
I have the following code :- class A : public B { public: _container
I have a data structure that represents C# code like this: class Namespace: string
code: class Node: def __init__(self, key, children=[]): self.key = key self.children = children def
Code: class JobsManager(models.Manager): def get_active_by_category(self, cat, limit): import datetime from django.db.models import Q return

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.