Demonstrate how the Java operator instanceof  tests to verify if an object is an instance of another class or not.  You will create 3 classes, the main, a simple class and another that is a child of the simple.  Type up the code and submit the results.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter10: Introduction To Inheritance
Section: Chapter Questions
Problem 8RQ
icon
Related questions
Question

Demonstrate how the Java operator instanceof  tests to verify if an object is an instance of another class or not.  You will create 3 classes, the main, a simple class and another that is a child of the simple.  Type up the code and submit the results.

MAIN CLASS
public class InstanceOfExample {
public static void main(String[] args) {
SIMPLE CLASS
public class SimpleClass {
}
SimpleClass sc = new SimpleClass();
AnotherSimpleClass asc = new AnotherSimpleClass();
boolean b1 = asc instanceof SimpleClass;
boolean b2 = sc instanceof AnotherSimpleClass;
System.out.println("Is AnotherSimpleClass an instance of SimpleClass:
System.out.println("Is SimpleClass an instance of AnotherSimpleClass:
private int myNumber;
private String myName;
public double change;
public void setVars(int num, String name)
{
}
public int getNumber ()
{
return myNumber;
}
}
myNumber= num;
myName = name;
{
public String getName()
return myName;
ANOTHER SIMPLE DERIVED CLASS
public class AnotherSimpleClass extends SimpleClass{
}
public String pirntOut() {
return "I am a derived class!";
b1);
+ b2);
Transcribed Image Text:MAIN CLASS public class InstanceOfExample { public static void main(String[] args) { SIMPLE CLASS public class SimpleClass { } SimpleClass sc = new SimpleClass(); AnotherSimpleClass asc = new AnotherSimpleClass(); boolean b1 = asc instanceof SimpleClass; boolean b2 = sc instanceof AnotherSimpleClass; System.out.println("Is AnotherSimpleClass an instance of SimpleClass: System.out.println("Is SimpleClass an instance of AnotherSimpleClass: private int myNumber; private String myName; public double change; public void setVars(int num, String name) { } public int getNumber () { return myNumber; } } myNumber= num; myName = name; { public String getName() return myName; ANOTHER SIMPLE DERIVED CLASS public class AnotherSimpleClass extends SimpleClass{ } public String pirntOut() { return "I am a derived class!"; b1); + b2);
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage