answersLogoWhite

0


Best Answer

It depends on the programming language you are referring to. In the Java Programming Language, Casting a superclass reference to a subclass is dangerous. It is dangerous because the code will compile fine, leading you to believe that the code will always work correctly. However, sometimes when you cast a superclass to a subclass, the program will throw a java.lang.ClassCastException when performing the cast.

Consider an example in Java programming language:

class Person{

String getName(){ return "Fred"; }

}

class Employee extends Person {

double getSalary(){ return 57; }

}

public class X {

public static void main(String args[])

{

Person p = new Person();

Employee e = (Employee) p; //This causes a java.lang.ClassCastException

System.out.println(e.getSalary());

}

}

When this program runs, It crams a Person p object into an Employee e object which causes a "java.lang.ClassCastException: Person cannot be cast to Employee"

Employee e = (Employee) p; //error happens here

Note that an Employee has a method called getSalary, but Person does not. Not all People have Salaries. The Person object is Instantiated and put into 'p'. The problem is that this particular person p is not an employee. If that person p WAS an employee the code would run correctly and print 57.

Casting a superclass reference to a subclass is dangerous because sometimes the cast will work perfectly, and sometimes it will crash the program. The cast itself is the dangerous part. When doing casts of this sort, the programmer needs to make 100% sure that this Person p was instantiated with a new Employee(). Then the Person object will have the method getSalary().

This code is dangerous as well, but it does not throw any exceptions, it makes a Person which is an Employee, and then casts that Person/Employee into an Employee type.

class Person{

String getName(){ return "Fred"; }

}

class Employee extends Person {

double getSalary(){ return 57; }

}

public class X {

public static void main(String args[]){

Employee x = new Employee();

Person w = (Employee) x;

Employee y = (Employee)w;

System.out.println(y.getSalary());

}

}

Here, employee x is created and then cast into a Person object. Person w is an Employee. Now when we cast the Person w into Employee y, everything is fine, and the program prints "57.0". It's dangerous because this person might not have been an Employee.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

AnswerBot

1mo ago

Casting a superclass reference to a subclass reference can be dangerous because the subclass may have additional methods or attributes that are not present in the superclass. If you then try to access these subclass-specific features using the superclass reference, it can cause runtime errors like ClassCastException or NullPointerException. It's generally safer to use polymorphism and dynamic binding to work with subclass objects using superclass references.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why casting a super class reference to subclass reference is potentially dangerous?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the g638 reference on the bottom side of a block with casting?

From this website, the G638 might reference a GM truck block. But this is not my area of expertise.


Why is there type casting in java?

Type casting exists to allow the use of base class or base interface reference but yet if you know that the object in hand is aspecific derived class, you can typecast and access methods available only in the derived class.


What is casting and its type?

Simply stated, casting allows you to change the type (class) of an object. Technically, the object itself does not change type, but your reference is changed. For example, lets suppose you have the class "Animal" with subclasses "Cat" and "Dog". If you have a reference (variable) declared to be type "Animal" and you store and object of type "Dog" in it, you will not be able to call any methods from the "Dog" class which the "Animal" class does not have. You would first need to cast your reference to "Dog" so that you can utilize those methods.


How casting is done?

Metal casting can be done thro sand casting ,investment casting , pressure die casting methods


What are types of casting?

Two popular types of casting include: metal casting and concrete casting. However, there's also resin casting as well as iron casting.


Why converting a base class pointer to a derived class pointer is consider dangerous by the compiler?

There are two methods of casting one type to another: static casting and dynamic casting (both of which apply to pointers and references to objects). Statically casting a derived class to a base class is typesafe as the base class is guaranteed to exist if the derived class exists. However, static casting from a base class to a derived class is always considered dangerous as the conversion is not typesafe. Dynamic casting exists to cater for this scenario, however it is only possible when the base class is polymorphic (thus ensuring the required runtime information is available). If the conversion is not possible, the return value is NULL. However, it is considered bad programming practice to dynamically cast from a base class to a derived class. If the base class is polymorphic (which it must be), there is NEVER any need to dynamically cast between types. Virtual methods ensure correct behaviour. Whenever you are forced to dynamically cast from a base class to a derived class, consider redesigning the base class interface instead, as it is a clear sign of bad design. It is not dangerous, however; only static casting from a base class to a derived class is considered dangerous.


Which is not the typical casting process?

Die casting.


How is metal casting done?

Casting is the process of pouring liquid metal into a mold. The metal cools and takes the shape of the mold.


What happens if we use an integer pointer as a member of structure instead of structure pointer?

By declaring an integer pointer you are declaring that any non-zero reference stored in the pointer is guaranteed to be an integer reference. In order to guarantee the reference is actually a structure, the pointer must be declared as such, because casting an integer to a structure can never be regarded as being type-safe.


How are car wheels made?

Casting Casting


Is the crane hook casting or forging?

casting


What is gravity die casting?

It is process of making Alu. zinc component die. and material flows in die by Gravity force only no external pressure applied as Pressure Die Casting. Gravity casting is the liquid metal in the Earth's gravity into the casting process, also known as gravity pouring casting. The generalized gravity casting include sand casting, metal casting, investment casting, lost foam casting, clay mold casting; narrow-defined gravity casting refers to the metal casting. uses the force of gravity, to fill a permanent mold, or die, with molten material. It does not use the high-pressure method to form the casting. It will ensure that there will not be any clusters formed during this gravity casting process. The method has advantages like cost effective, good quality, and process control over other casting techniques.