EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
9th Edition
ISBN: 9781337671385
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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);
expand button
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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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.
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning