answersLogoWhite

0

What is the Role of casting in java program?

Updated: 8/18/2019
User Avatar

Wiki User

9y ago

Best Answer

Casting an object to a child object is to make accessible fields, methods, etc that are found only in a child. For example.

ObjectA has field apples, and

ObjectB (which extends ObjectA) has field bananas, and

ObjectC (which also extends ObjectA) has field strawberries.

We created an instance of ObjectB which is of type ObjectA , Represented like this:

"ObjectA my_instance = new ObjectB();" //Since ObjectB extends ObjectA, we can make my_instance of type ObjectA

We can access apples:

"my_instance.apples"

Since my_instance is of the type ObjectA, any field from ObjectA is accessible such as apples, HOWEVER ObjectA does not contain the field bananas, so how do we access bananas? (And we can do it since we created an INSTANCE of ObjectB not ObjectA).

We do it by casting like this:

"((ObjectB) my_instance).bananas" <<We can now access apples AND bananas

This is what casting is used for.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the Role of casting in java program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.


What is java program on computers?

#!/usr/bin/perl print 'java program';


Why are Java files important in programming?

Java files, containing source code, play a crucial role in development, compilation, packaging, maintenance, and version control.


What powder game doesnt need java?

Any program written for the Java technology needs Java. Any program NOT written for Java DOESN'T need it.


Java Track?

Java Track is the java training program offered by SynergisticIT


Where can one get the program Java?

The program Java can be downloaded from the official Java website on the internet. The program is free for personal use, so it can be downloaded without any cost by anyone.


What is a Java applet?

PCH answer small Embedded Java Program.


How do you change a float into a double using java?

with help of type casting it is possible


Do I need Java?

No, if a program needs Java it'll specify that it does in the installer package for the program or on the website for the program or a section usually called "Requirements" for a specific program.


What is a java programming?

A java program is a program that is coded and run in the programming language called java. Java is similar to c++ in structure, and is more common in web apps. C++ is the equivalent for more heavy duty programs such as most software used to compose a java program.


Where java program started?

If it is already compiled, you can start your Java program from the command line. Just type java myclass replacing "myclass" with the program you want to start - a program with the ".class" extension, for example, myclass.class.