• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in few questions

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am new to this site.
I have few doubts, i would be glad if you could help me in this.
1) x.equals(y) to be true, y instancof x should be true.
Is this statement true.
2) Can static methods be overridden to be static.
3) Which objects create a file if it does not exist oyher than FileOutputStream and RandomAccessFile.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. No. equals() takes a parameter which is a reference to
an object, while the right-hand operand for instanceof
must be type. So, if y is an object, it can't be used
as a right operand for instanceof.
2. No. Only methods of an object can be overriden. Static
methods are class methods.
3. I don't know of any others. I will like to find out too.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha,
1. Yes. The equals implements a check against class state. If both classes aren't equal then their states sure can't be!
2. Static methods can not be overridden. Technically only non-static methods can be overridden. If we try to hide the static method in a subclass the compiler will make sure the subclass method will also be declared static to avoid ambiguity.
3. What about FileWriter?
Regards,
Manfred.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) You cannot assert this condition true in all cases for several reasons:
a) Object.equals() can be overriden. By default, it tests reference assignment, but any subclass of Object is free to redefine equals() as it sees fit.
b) (subclass instanceof superclass) is true, but (superclass instanceof subclass) is false. Yet both could have an equality test that returns true.

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3) File.createNewFile();
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred,
i agree with your answer.
But i am yet not clear with my first question.
Could u give an example.
Regarding my 2nd question ,could u tell me whether this is overiding.
public class StaticOverridingTest {
public static void main(String s[]) {
Parent p =new Parent()
p.dostuff();
Child c=new Child()
c.dostuff();
p=c;
p.dostuff();
}
}
class Parent {
static int x = 100;
public static void doStuff() {
System.out.println("In Parent..doStuff");
System.out.println(x);
}
}
class Child extends Parent {
static int x = 200;
public static void doStuff() {
System.out.println("In Child..doStuff");
System.out.println(x);
}
}
File writer will also create a file if it does not exist.
I hope i am right.

 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding your second, static overriding question.
This is not overriding. this is hiding of the base class static mehod. This is what JLS 8.4.6.2 says abt it -
"If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method. "
Your code
p=c;
p.doStuff(); // parent doStuff
calls doStuff() of parent method and not child method, so overriding does not apply here, as it would apply for non-static methods. See also -
8.4.6.1 Overriding (by Instance Methods)
8.4.6.2 Hiding (by Class Methods)
HTH,
- Manish
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic