Why can we cast "servletRequest" to "HttpServletRequest"? [duplicate] - java

Upcasting is allowed in Java, however downcasting gives a compile error.
The compile error can be removed by adding a cast but would anyway break at the runtime.
In this case why Java allows downcasting if it cannot be executed at the runtime?
Is there any practical use for this concept?
public class demo {
public static void main(String a[]) {
B b = (B) new A(); // compiles with the cast,
// but runtime exception - java.lang.ClassCastException
}
}
class A {
public void draw() {
System.out.println("1");
}
public void draw1() {
System.out.println("2");
}
}
class B extends A {
public void draw() {
System.out.println("3");
}
public void draw2() {
System.out.println("4");
}
}

Downcasting is allowed when there is a possibility that it succeeds at run time:
Object o = getSomeObject(),
String s = (String) o; // this is allowed because o could reference a String
In some cases this will not succeed:
Object o = new Object();
String s = (String) o; // this will fail at runtime, because o doesn't reference a String
When a cast (such as this last one) fails at runtime a ClassCastException will be thrown.
In other cases it will work:
Object o = "a String";
String s = (String) o; // this will work, since o references a String
Note that some casts will be disallowed at compile time, because they will never succeed at all:
Integer i = getSomeInteger();
String s = (String) i; // the compiler will not allow this, since i can never reference a String.

Using your example, you could do:
public void doit(A a) {
if(a instanceof B) {
// needs to cast to B to access draw2 which isn't present in A
// note that this is probably not a good OO-design, but that would
// be out-of-scope for this discussion :)
((B)a).draw2();
}
a.draw();
}

I believe this applies to all statically typed languages:
String s = "some string";
Object o = s; // ok
String x = o; // gives compile-time error, o is not neccessarily a string
String x = (String)o; // ok compile-time, but might give a runtime exception if o is not infact a String
The typecast effectively says: assume this is a reference to the cast class and use it as such. Now, lets say o is really an Integer, assuming this is a String makes no sense and will give unexpected results, thus there needs to be a runtime check and an exception to notify the runtime environment that something is wrong.
In practical use, you can write code working on a more general class, but cast it to a subclass if you know what subclass it is and need to treat it as such. A typical example is overriding Object.equals(). Assume we have a class for Car:
#Override
boolean equals(Object o) {
if(!(o instanceof Car)) return false;
Car other = (Car)o;
// compare this to other and return
}

We can all see that the code you provided won't work at run time. That's because we know that the expression new A() can never be an object of type B.
But that's not how the compiler sees it. By the time the compiler is checking whether the cast is allowed, it just sees this:
variable_of_type_B = (B)expression_of_type_A;
And as others have demonstrated, that sort of cast is perfectly legal. The expression on the right could very well evaluate to an object of type B. The compiler sees that A and B have a subtype relation, so with the "expression" view of the code, the cast might work.
The compiler does not consider the special case when it knows exactly what object type expression_of_type_A will really have. It just sees the static type as A and considers the dynamic type could be A or any descendant of A, including B.

In this case why Java allows downcasting if it cannot be executed at the runtime?
I believe this is because there is no way for the compiler to know at compile-time if the cast will succeed or not. For your example, it's simple to see that the cast will fail, but there are other times where it is not so clear.
For instance, imagine that types B, C, and D all extend type A, and then a method public A getSomeA() returns an instance of either B, C or D depending on a randomly generated number. The compiler cannot know which exact run-time type will be returned by this method, so if you later cast the results to B, there is no way to know if the cast will succeed (or fail). Therefore the compiler has to assume casts will succeed.

# Original Poster - see inline comments.
public class demo
{
public static void main(String a[])
{
B b = (B) new A(); // compiles with the cast, but runtime exception - java.lang.ClassCastException
//- A subclass variable cannot hold a reference to a superclass variable. so, the above statement will not work.
//For downcast, what you need is a superclass ref containing a subclass object.
A superClassRef = new B();//just for the sake of illustration
B subClassRef = (B)superClassRef; // Valid downcast.
}
}
class A
{
public void draw()
{
System.out.println("1");
}
public void draw1()
{
System.out.println("2");
}
}
class B extends A
{
public void draw()
{
System.out.println("3");
}
public void draw2()
{
System.out.println("4");
}
}

Downcast works in the case when we are dealing with an upcasted object.
Upcasting:
int intValue = 10;
Object objValue = (Object) intvalue;
So now this objValue variable can always be downcasted to int because the object which was cast is an Integer,
int oldIntValue = (Integer) objValue;
// can be done
but because objValue is an Object it cannot be cast to String because int cannot be cast to String.

Downcasting is very useful in the following code snippet I use this all the time. Thus proving that downcasting is useful.
private static String printAll(LinkedList c)
{
Object arr[]=c.toArray();
String list_string="";
for(int i=0;i<c.size();i++)
{
String mn=(String)arr[i];
list_string+=(mn);
}
return list_string;
}
I store String in the Linked List.
When I retrieve the elements of Linked List, Objects are returned. To access the elements as Strings(or any other Class Objects), downcasting helps me.
Java allows us to compile downcast code trusting us that we are doing the wrong thing.
Still if humans make a mistake, it is caught at runtime.

Consider the below example
public class ClastingDemo {
/**
* #param args
*/
public static void main(String[] args) {
AOne obj = new Bone();
((Bone) obj).method2();
}
}
class AOne {
public void method1() {
System.out.println("this is superclass");
}
}
class Bone extends AOne {
public void method2() {
System.out.println("this is subclass");
}
}
here we create the object of subclass Bone and assigned it to super class AOne reference and now superclass reference does not know
about the method method2 in the subclass i.e Bone during compile time.therefore we need to downcast this reference of superclass to subclass reference so as the resultant reference can know about the presence of methods in the subclass i.e Bone

To do downcasting in Java, and avoid run-time exceptions, take a reference of the following code:
if (animal instanceof Dog) {
Dog dogObject = (Dog) animal;
}
Here, Animal is the parent class and Dog is the child class.
instanceof is a keyword that is used for checking if a reference variable is containing a given type of object reference or not.

Downcasting transformation of objects is not possible.
Only
DownCasting1 _downCasting1 = (DownCasting1)((DownCasting2)downCasting1);
is posible
class DownCasting0 {
public int qwe() {
System.out.println("DownCasting0");
return -0;
}
}
class DownCasting1 extends DownCasting0 {
public int qwe1() {
System.out.println("DownCasting1");
return -1;
}
}
class DownCasting2 extends DownCasting1 {
public int qwe2() {
System.out.println("DownCasting2");
return -2;
}
}
public class DownCasting {
public static void main(String[] args) {
try {
DownCasting0 downCasting0 = new DownCasting0();
DownCasting1 downCasting1 = new DownCasting1();
DownCasting2 downCasting2 = new DownCasting2();
DownCasting0 a1 = (DownCasting0) downCasting2;
a1.qwe(); //good
System.out.println(downCasting0 instanceof DownCasting2); //false
System.out.println(downCasting1 instanceof DownCasting2); //false
System.out.println(downCasting0 instanceof DownCasting1); //false
DownCasting2 _downCasting1= (DownCasting2)downCasting1; //good
DownCasting1 __downCasting1 = (DownCasting1)_downCasting1; //good
DownCasting2 a3 = (DownCasting2) downCasting0; // java.lang.ClassCastException
if(downCasting0 instanceof DownCasting2){ //false
DownCasting2 a2 = (DownCasting2) downCasting0;
a2.qwe(); //error
}
byte b1 = 127;
short b2 =32_767;
int b3 = 2_147_483_647;
// long _b4 = 9_223_372_036_854_775_807; //int large number max 2_147_483_647
long b4 = 9_223_372_036_854_775_807L;
// float _b5 = 3.4e+038; //double default
float b5 = 3.4e+038F; //Sufficient for storing 6 to 7 decimal digits
double b6 = 1.7e+038;
double b7 = 1.7e+038D; //Sufficient for storing 15 decimal digits
long c1 = b3;
int c2 = (int)b4;
//int 4 bytes Stores whole numbers from -2_147_483_648 to 2_147_483_647
//float 4 bytes Stores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits
float c3 = b3; //logic error
double c4 = b4; //logic error
} catch (Throwable e) {
e.printStackTrace();
}
}
}

I will tell you why this happened. First of all you have to understand how JVM supports when we assign parent class into the child class using downcasting, because of reference . For example consider in the following code.
A is the super type any class that extends from it and can store the reference B class.
A a =new B();
When you assign a reference variable into the child class jvm will understand that since A can store the reference of B class that is why you can do it.
B b=(B)b;
The reason which is called compile time error and why you couldn't directly assign Parent class into the Child class because there is not any extends relationship. Note that casting only occurring with the key which is called extends, that is why you receive the compile time error.
Another reason which is called ClassCastException by the runtime because of jvm it directly accept the rule which is okay I accept that it is true but jvm after that will understand that by the runtime it is not store any referance of Child class when code was writing by the programmer who write coding in the syntax .

Related

If I cast an array of T to an array of Q (being Q derived from T) does it cast each element in turn?

I have the next snippet of code:
Certificate[] a = myFavouriteKeystore.getCertificateChain();
but I need to pass it to a method with the following signature:
void setCertificateChain(X509Certificate[] chain);
Can I do the following?:
setCertificateChain((X509certificate[]) a);
IDE(eclipse) accepts it and I assume that compiler does also, but my guess is that I'll get a ClassCastException even in the case that all the array elements are of class X509Certificate or a subclass of it.
You will get ClassCastException at runtime, yes.
The cast is like lying to the compiler, saying that you really know what you are doing and even if the compiler can't prove that the cast will work - you are instructing it to trust you.
The compiler listens to you (not in all cases, i.e. you can't tell it to cast a String to an Integer for example, since String is final and can't have sub-classes), but at the same time will inject into the byte code checkcast instructions.
Tested, and it fails with a ClassCastException error:
package test;
public class TestClass {
public static class A { }
public static class B extends A { }
public static void main(String [] args) {
A[] a = new A[100];
for (int i = 0; i < a.length; i++) {
a[i] = new B();
}
B[] b = (B[]) a; /* Error: ClassCastException, even if all elements are of type B */
}
}
Thanks to #Eugene that so quick answered the question.
NOTE
This agrees with the policy of casting generic containers. For a container derived of a super class only the cast applies if the parameter types match. E.g: Set<A> can be casted to SortedSet<B> only if A and B are the same type.

Casting objects in a way that we cast Primitive Data Types

I have three classes: A, AA and Top. A and AA extend Top.
Why is it that this won't compile:
A a = new A();
AA aa = (AA)a;
but this will:
float f = 4.3f;
int i = (int) f;
Class A and class AA are on the same hierarchy, but they are side by side, so they can not be cast as one another.
Lets say class A was defined as so:
public class A extends Top{
public A() {
}
public void foo(int i) {
System.out.println(i);
}
}
And that class AA was defined as so:
public class AA extends Top {
public AA() {
}
public void bar(String s) {
}
}
Now lets theoretically imagine what would actually happen if you tried to cast an A to an AA and it worked:
A a = new A();
(AA) aa = (AA) a;
Since aa has a static type of AA, Java would let you write code like this:
aa.bar("hi!");
Because the AA class has a bar method, BUT aa has a dynamic type of A, which means that the actual object that the variable aa refers to does not have a method called bar("hi!").
(The static type of an object tells you what methods you can call on it and the dynamic type tells you what methods it actually has.)
So Java would tell aa to do bar("hi!") but aa wouldn't know what to do, because aa does not have the bar method defined.
You may create cast method in the class you want to cast. Here I have cast type into type A by using a.cast(B b) method.
public class A {
public int i;
public A cast(B b){
A a = new A();
a.i=b.i;
return a ;
}
}
public class B {
public int i=10;
}
public class Tester {
public static void main(String[] args) {
B b = new B();
A a= new A();
a=a.cast(b);
System.out.println(a.i);
}
}
Java is a strong-type programming language, which means to define every variable, you need to specify its type, and for each variable, it can only hold a value belongs to the same type. For primitive types Java also defines the rules how different types can be casted to each other, there's Widening Primitive Conversion(no info loss) and Narrowing Primitive Conversion(info may loss), you can find more details at the official Java docs. So the reason you can do int i = (int) f; is that the conversion rule is defined in Java Spec and Java compiler allows you to do so. You can't do int a = (int) true if you tried, because there's no such a rule to convert boolean type to int.
Type casting rules for reference types are also simple, the compiler only allow you to do type casting A a = (A) b when it thinks the type of b maybe A or some sub-type of A in the type hierarchy, take a look at the following code:
Object b = c;
A a = (A) b;
compiler only knows that b is type Object but has no idea of its specific type, because Object is the root of Java type hierarchy, so the real type of b maybe any type, like type A, so the compiler will allow you to do so. If b actually is not a A, the error can only be found at runtime, when a ClassCastException will be thrown.
On the other hand, the compiler will prevent you to do A a = (A) b when it clearly knows that b is not type A:
class A {}
class B {}
If you have the above definition of type A and B, then compiler has enough information that an instance B is absolutely not type A, so it will give you a compile error of Inconvertible types when you try to do A a = (A) new B(). And this is one of big benefits to use a strong-type programming language: to guarantee type safety (at compile time for Java).

I have some issue about casting in java

public class A {
private String superStr;
public String getSuperStr() {
return superStr;
}
public void setSuperStr(String superStr) {
this.superStr = superStr;
}
}
public class B extends A {
private String subStr;
public String getSubStr() {
return subStr;
}
public void setSubStr(String subStr) {
this.subStr = subStr;
}
}
And I expect result likes below
public static void main(String[] args) {
A a = fuc();
B b = new B();
b = (B) a;
b.setSubStr("subStr");
System.out.println(a.getSuperStr() + b.getSubStr());
}
private static A fuc() {
A a = new A();
a.setSuperStr("super");
return a;
}
but java.lang.ClassCastException is ocuured.
How can I cast this?
I want use subclass variable and super class variable.
thank you
How can I cast this?
You can't. You can only cast when the object in question has an "is a" relationship with the type. In your case, you have an A object (the one from fn), which is not a B. (All B objects are As, because B extends A, but not all A objects are Bs.)
Consider: Let's call B Bird and A Animal: A Bird is an Animal, but not all Animals are Birds. So we can treat all Birds as Animals, but we cannot treat all Animals as Birds. When you're using a variable of a given type to refer to an object, you're treating the object as being of that type (e.g., B b = (B)a tries to treat the Animal a as a Bird).
Side note: There's no point to the indicated part of the code:
B b = new B();
// ^^^^^^^^^^
b = (B) a;
Since the very next line assigns to b (well, it would if it would compile), there's no purpose served by doing new B() and assigning that to b just beforehand. (Unless the B constructor has side-effects, which is generally a Bad Idea™.)
Casting a particular object to another types does not magically convert it into an instance of that class (or at least not in Java); Therefore, the object referenced by variable a does not e.g. have the field subStr to use despite that the object referenced by b after executing B b = new B(); does.
The others have already explained why you can't do that. I'm here to give you a simple alternative. Your B class could have a constructor that had an A as argument and you would simply wrap that A so you could "transform" it to a B. Using that your code would look way more clean, it would actually work and you were following a good design pattern. For more information check the Decorator Pattern

The type of the object created with a help of Object o = "";

Say, there is a following example:
class Super {
public int i = 3;
public void m(Object o) {
System.out.println("Object " + i);
}
public void m(String o) {
System.out.println("String " + i);
}
}
public class Sub extends Super {
public Sub() {
i = 5;
}
public static void main(String[] args) {
Super s = new Sub();
Object o = "";
s.m(o);
s.m("");
}
}
The result of this code is:
Object 5
String 5
But I thought it would be:
String 5
String 5
Don't quotes set String as this object's type? There are definitely some cases of casting to String with a help of quotes, so I'm a little confused about this basic example. Thanks in advance.
The type of the method is determined in compile time, and not in run time. The dynamic dispatch exists only for the "parameter" this - there is no dynamic dispatch for parameters in static typing languages such as java.
The compiler "choses" which method should be invoked, and since o is of type Object - it choses m(Object) - it has no way to know that the dynamic type of o is actually a String.
If you are interested - a common way to overcome this issue in some cases is using the visitor design pattern.
In your specific case, in order to "force" the activation of m(String) you should use m(o.toString())
In sb.m(o), you're calling m() with an Object reference, so Java chooses that overload. Java will not choose a more specific overload than the reference type you pass it. It will go up the inheritance chain though. Say you didn't have m(String o), calling sb.m("Hello") would still be legal, but it would call the object version.
If you were to do sb.m((String) o), you would get your expected behavior.
You declared the object as Object so its type is Object. Types in Java are strong and static so when you declare an object as type Type that is what its type will be for life.
If you want it to be a string you'll have to use a toString method or a cast (String)o
You are downcasting the String to an Object. What you are doing is similar to this.
public class Sub extends Super {
public Sub() {
i = 5;
}
public static void main(String[] args) {
Super s = new Sub();
Object o = "";
System.out.println("Object Type = " + o.getClass().getName());
s.m(o);
s.m((Object)"");
}
}

How to determine an object's class?

If class B and class C extend class A and I have an object of type B or C, how can I determine of which type it is an instance?
if (obj instanceof C) {
//your code
}
Use Object.getClass. It returns the runtime type of the object. Here's how to call it using your example:
class A {}
class B extends A {}
class C extends A {}
class Main {
public static void main(String args[]) {
C c = new C();
Class clazz = c.getClass();
System.out.println(clazz);
}
}
Output:
class C
You can also compare two Class instances to see if two objects are the same type.
class A {}
class B extends A {}
class C extends A {}
class Main {
public static void main(String args[]) {
B b = new B();
Class c1 = b.getClass();
C c = new C();
Class c2 = c.getClass();
System.out.println(c1 == c2);
}
}
Multiple right answers were presented, but there are still more methods: Class.isAssignableFrom() and simply attempting to cast the object (which might throw a ClassCastException).
Possible ways summarized
Let's summarize the possible ways to test if an object obj is an instance of type C:
// Method #1
if (obj instanceof C)
;
// Method #2
if (C.class.isInstance(obj))
;
// Method #3
if (C.class.isAssignableFrom(obj.getClass()))
;
// Method #4
try {
C c = (C) obj;
// No exception: obj is of type C or IT MIGHT BE NULL!
} catch (ClassCastException e) {
}
// Method #5
try {
C c = C.class.cast(obj);
// No exception: obj is of type C or IT MIGHT BE NULL!
} catch (ClassCastException e) {
}
Differences in null handling
There is a difference in null handling though:
In the first 2 methods expressions evaluate to false if obj is null (null is not instance of anything).
The 3rd method would throw a NullPointerException obviously.
The 4th and 5th methods on the contrary accept null because null can be cast to any type!
To remember: null is not an instance of any type but it can be cast to any type.
Notes
Class.getName() should not be used to perform an "is-instance-of" test becase if the object is not of type C but a subclass of it, it may have a completely different name and package (therefore class names will obviously not match) but it is still of type C.
For the same inheritance reason Class.isAssignableFrom() is not symmetric:
obj.getClass().isAssignableFrom(C.class) would return false if the type of obj is a subclass of C.
You can use:
Object instance = new SomeClass();
instance.getClass().getName(); //will return the name (as String) (== "SomeClass")
instance.getClass(); //will return the SomeClass' Class object
HTH. But I think most of the time it is no good practice to use that for control flow or something similar...
Any use of any of the methods suggested is considered a code smell which is based in a bad OO design.
If your design is good, you should not find yourself needing to use getClass() or instanceof.
Any of the suggested methods will do, but just something to keep in mind, design-wise.
We can use reflection in this case
objectName.getClass().getName();
Example:-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getClass().getName();
}
In this case you will get name of the class which object pass to HttpServletRequest interface refference variable.
There is also an .isInstance method on the "Class" class. if you get an object's class via myBanana.getClass() you can see if your object myApple is an instance of the same class as myBanana via
myBanana.getClass().isInstance(myApple)
checking with isinstance() would not be enough if you want to know in run time.
use:
if(someObject.getClass().equals(C.class){
// do something
}
I Used Java 8 generics to get what is the object instance at runtime rather than having to use switch case
public <T> void print(T data) {
System.out.println(data.getClass().getName()+" => The data is " + data);
}
pass any type of data and the method will print the type of data you passed while calling it. eg
String str = "Hello World";
int number = 10;
double decimal = 10.0;
float f = 10F;
long l = 10L;
List list = new ArrayList();
print(str);
print(number);
print(decimal);
print(f);
print(l);
print(list);
Following is the output
java.lang.String => The data is Hello World
java.lang.Integer => The data is 10
java.lang.Double => The data is 10.0
java.lang.Float => The data is 10.0
java.lang.Long => The data is 10
java.util.ArrayList => The data is []
You can use getSimpleName().
Let's say we have a object: Dog d = new Dog(),
The we can use below statement to get the class name: Dog. E.g.:
d.getClass().getSimpleName(); // return String 'Dog'.
PS: d.getClass() will give you the full name of your object.
use instanceof operator to find weather a object is of particular class or not.
booleanValue = (object instanceof class)
JDK 14 extends the instanceof operator: you can specify a binding
variable; if the result of the instanceof operator is true, then the
object being tested is assigned to the binding variable.
please visit official Java documentation for more reference.
Sample program to illustrate usage of instanceof operator :
import java.util.*;
class Foo{
#Override
public String toString(){
return "Bar";
}
}
class Bar{
public Object reference;
#Override
public String toString(){
return "Foo";
}
}
public class InstanceofUsage{
public static void main(final String ... $){
final List<Object> list = new ArrayList<>();
var out = System.out;
list.add(new String("Foo Loves Bar"));
list.add(33.3);
list.add(404_404);
list.add(new Foo());
list.add(new Bar());
for(final var o : list){
if(o instanceof Bar b && b.reference == null){
out.println("Bar : Null");
}
if(o instanceof String s){
out.println("String : "+s);
}
if(o instanceof Foo f){
out.println("Foo : "+f);
}
}
}
}
output:
$ javac InstanceofUsage.java && java InstanceofUsage
String : Foo Loves Bar
Foo : Bar
Bar : Null
your_instance.getClass().getSimpleName() will gives type name for example: String, Integer, Double, Boolean...
I use the blow function in my GeneralUtils class, check it may be useful
public String getFieldType(Object o) {
if (o == null) {
return "Unable to identify the class name";
}
return o.getClass().getName();
}

Categories