This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

instanceOf

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given a base class and a derived class defined as follows
public abstract class BaseTest extends Object implements Runnable
public class AdvancedTest extends BaseTest
what will happen when we try to compile the following method?
1. public boolean checkTest( Object obj ){
2. if( obj instanceof BaseTest ) return true;
3. System.out.println( "Not a BaseTest" );
4. if( obj instanceof Runnable ) return true;
5. System.out.println( "Not Runnable" );
6. return false;
7. }
How can i get this code working and what is its answer? I tried coding it in the following manner but i can't get it working. Pl help
If there are any good examples of instanceOf and Gc please send them to may email id too. I am weak in those areas. Pl help.
coding:
abstract class BaseTest extends Object implements Runnable
{
public abstract boolean checkTest(Object obj);
};
public class AdvancedTest extends BaseTest
{
public static void main(String[] args)
{
public boolean checkTest(Object obj)
{
if (obj instanceof BaseTest)
{
return true;
}System.out.println("NOt a base test!");
if (obj instanceof Runnable)
{
return true;
}System.out.println("Not runnable.");
return false;
}
AdvancedTest a = new AdvancedTest();
String s = new String("Welcome");
boolean m=a.checkTest(s);

}
}
Thanks in advance
Padmini
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things:
Why have you made checkTest() a inner method of main? This will not work. You should take it out of there, and only call it from main.
/Mike
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Padmini,
You also not overriding run() method of Runnable. YOu will get compiler error.
I have changed your code like this.

Vanitha.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic