Determining class with a Java instanceOf example - 032

preview_player
Показать описание
What if we need to know the specific class for an instance in our list? In this lesson we're going to take a quick look at how we can determine the class of an instance.

Concepts: Java, Programming, instanceof, class, instance

Social Links: Don't hesitate to contact me if you have any further questions.

Related Videos:

Free Java Course Online

If You Watch 1 Playlist About Java Classes Watch this One!

Media credits: All images are owned by DJ Spiess unless listed below

Рекомендации по теме
Комментарии
Автор

Would you be able to explain how this snippet works? I don't quite understand how this works and I have learned a lot from your teaching style.

public class TestClass {

public static void main(String[] args) {
B b = new C();
A a = b;
if(a instanceof A) System.out.println("A");
if(a instanceof B) System.out.println("B");
if(a instanceof C) System.out.println("C");
if(a instanceof D) System.out.println("D");
}
}
class A {}
class B extends A {}
class C extends B {}
class D extends C {}

robwm
visit shbcf.ru