Похожие чаты

Regarding upcasting and downcasting in java im trying to come

up with some examples & causes of different kinds of errors regarding casting and polymorphism

given B extends A

((B) new A())
will cause a runtime error, right?

and say C doesn't extend anything
(C(new A()))
B ba = new A()
will cause a compile time error


so
are there any more cases like this im not thinking about that would result in compile/runtime errors?

also if u can provide an intuition behind those that'd be appreciated thanks

7 ответов

21 просмотр

A casted to B is just regular downcasting. It won't cause an error

dankey- Автор вопроса

isn't there a scenerio in which this'll cause an error?

dankey
isn't there a scenerio in which this'll cause an e...

If B didn't extend A Java would've thrown a ClassCastException which is a RuntimeException

dankey- Автор вопроса
Nader Jafari
If B didn't extend A Java would've thrown a ClassC...

i was sure im missing something. sure there aren't any small annoyances regarding casting in between classes with is a relation between them?

dankey
i was sure im missing something. sure there aren'...

I don't think so. Primitives are all numeral types so it's just a matter of widening and narrowing. As for references, classes with parent-(grand)child relations can be casted, you lose fields and methods upcasting but since children can't make inherited fields and methods less visible it just can't go wrong. The only slightly problematic one is downcasting which you can prevent at compile time using instanceof

dankey- Автор вопроса
Nader Jafari
I don't think so. Primitives are all numeral types...

umm can you give an example for such problematic downcasting?

Похожие вопросы

Обсуждают сегодня

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
any reference of this implementation?
BitBuddha
29
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Недавно Google Project Zero нашёл багу в SQLite с помощью LLM, о чём достаточно было шумно в определённых интернетах, которые сопровождались рассказами, что скоро всех "ибешни...
Alex Sherbakov
5
So much speculation in the last week. So much volatility in price. This is because Hedera has a GC that isn't using the network it's governing. Why aren't people asking why a...
Summit Seeker R
9
Карта сайта