*
The moose likes Beginning Java and the fly likes Needs answers of some important questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Needs answers of some important questions" Watch "Needs answers of some important questions" New topic
Author

Needs answers of some important questions

sharma sachin
Greenhorn

Joined: Nov 13, 2009
Posts: 24
Q1: We can Create our own thread by extending Thread class and by implementing Runnable Interface
which one is to use when?
give some examples

Q2: Which is more advantageous Abstract Class or Interface?
Q3: Can we execute servlet or JSP without using container? What is the difference between Web Server and Container
Q4: If the constructor is declared private what will happen?
Q5:how we can change the context-innitialization parameters before stopping the server?
Q6:
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

What are your thoughts on these homework questions?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
sharma sachin
Greenhorn

Joined: Nov 13, 2009
Posts: 24
According to me:
Answer 1: No Idea
Answer 2: Interface is more advantageous than abstract class because Inteface provide strong polymorphism.
Answer 3: we can not execute either servlet or jsp without container because it is the container which call the lifecycle methods of servlets and jsp; the difference between web server and container is web-server contains container the request from the client first of all goes to web server then server passes the request to the container.
Anser 4: if the constructor is declared as private then we can not call that constructor in the subclass constructor.
Answer 5 and Answer 6: No idea

please do reply where i am wrong in answerd questions
also do reply the questions in which i m having no idea
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

Why would an interface provide stronger polymorphism than an abstract class? What is "strong polymorphism"?


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

what is mean by context-innitialization parameters before stopping the server?


Do or die...?? No, Do before you die... || (SCJP .6)
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49



insert this method inside the class

but as I know hash code also must be overide to take effect this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
deca leni wrote:
insert this method inside the class

but as I know hash code also must be overide to take effect this.
That is a very poor equals method. Please explain why it will give inconsistent and possibly incorrect results.
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

public boolean equals(Object o) method is used to check if the tow object are meaningfully equal .so if we check more instant variable equality then we get the exact result.
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

Campbell Ritchie wrote:
deca leni wrote:
insert this method inside the class

but as I know hash code also must be overide to take effect this.
That is a very poor equals method. Please explain why it will give inconsistent and possibly incorrect results.


I gave this code according to match your previous code.so it is not code that can give good result .I have shown only way of overriding it.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

Campbell Ritchie wrote:That is a very poor equals method. Please explain why it will give inconsistent and possibly incorrect results.

Well, for a start it won't compile. What is o?
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

give me full code you try to compile
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
deca leni wrote:public boolean equals(Object o) method is used to check if the tow object are meaningfully equal .so if we check more instant variable equality then we get the exact result.
Really? If you tried your equals method like this, you would doubtless get the wrong result. Even after you get it to compile, by putting "o" in the appropriate location. I think you have unbalanced () too.
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

I wanted tell why we use equals() method not how it use...
if really want to take effect of this we must override hashcode() method too





deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

try this code

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
. . . and try it with this one change, in line 21: . . . and you can see your code produce misleading results.

The idea of the equals() method is to allow you to define circumstances under which two objects are regarded as equal. You should by now know that using the == operator on reference types will produce inconsistent and misleading results. It is even worse with Strings because they are interned. Try serialising two A objects and comparing them for equality before and after serialisation. You will then see how your equals() method becomes inconsistent.

And it is poor style to write if (...) return true; else return false;
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

mmm Serialization

deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

this is the code and try this

deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

deca leni wrote:what do you mean by serializing. you mean implementing the Serializable interfaces.but as I know, Serialization involves saving the current state of an object to a stream, and restoring an equivalent object from that stream.for an object to be serialized, it must be an instance of a class that implements either the Serializable or Externalizable interface.here we are not going write this object to a file.so we don't want to implements Serializable interface as I think.


so if it is not what you mean.
then tell me about what you mean by serializing and how it involves with equals() write the correct Code to get the good result
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

String class
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
deca leni wrote:String class
Why are you quoting Java 1.4.2 documentation?

And what has serialisation to do with it? Well, try this modification . . . and as you know, serialisation and deserialisation produces an object identical to the original. So you can tell what the result of the second printf statement will be . . .

By the way: only use spaces, not tabs for indentation. And your if(...)return true; else return false; is very poor style. It should read
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
. . . and thank you whoever moved this thread; it never was a JDBC question in the first place.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
Yes, using the equals method of String will sort out the problem of the == operator. But you can still have problems.People think writing an equals method is easy, but it isn't. And what about this class?Try that and see what happens.


And we haven't been anywhere near the hashCode method yet.
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

Campbell Ritchie wrote:
. . .

and as you know, serialisation and deserialisation produces an object identical to the original. So you can tell what the result of the second printf statement will be . . .


hey your serialisation and deserialisation is not perfect as I know.

try this and see what has happen



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay... and with that "new and improved" code try this:


Deca, I am not sure what you were trying to prove by posting all of this code, or what question it was supposed to answer, but posting bad code which repeatedly has to be debugged and fixed doesn't help.
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

wow I like this forum I joined with this few days ago.. I rely like this.I wish I met this site befor.
there are lot more thing things talk about equal() method..
I hope you will help me.
thanks
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

no need try it will produce NullPointerException becouse of class A equals()..
if use String if primative compile error..
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

hey why do you say this code good practice


and few more thing want to discuss
1.not being able to define equals as an equivalence relation.
2.defining equals in terms of mutable fields.
3. override equal() and canEqual()
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
You will have to look through the standard sites about equals(). I can't remember where they are, but I post about them so often I simply find a previous post, like this one where I quoted them That link even finds the chapter 3 of Effective Java™. Go through those three links, Bloch, Odersky and Langer. That will teach you lots about equals().
Not sure, but I think canEqual() is a non-standard API method, which one of those sites describes.
Also have a look at these two methods expected to be available in Java7, which Rob Spoor drew my attention to.
Why getting rid of the if(...) return true; else return false; is good style? Look here. I don't follow all those conventions, but that one is good.

How would I write that equals() method? Like thisNote
  • 1: Overridden hashCode() method. If you read those three links you will learn about hash codes, too.
  • 2: The bit about getClass...hashCode is not strictly necessary.
  • 3: I have used the strictest available test for whether the two objects are from the same class.
  • 4: The ob == this bit short-circuits the whole thing if you pass the same object to itself.
  • 5: This method accepts a null argument, and will not throw an Exception.
  • 6: It will also not throw an Exception if s is null.
  • 7: In order not to suffer an Exception, the different parts of the boolean expression must be in a particular order, and the | and & operators must not be used.
  • 8: There are other ways to write an equivalent equals() method.
  • 9: If you have tests in your constructors and setXXX methods which prevent null values for the reference-type fields, then the == null checking becomes unnecessary.
  • Dic scott
    Greenhorn

    Joined: Jun 23, 2011
    Posts: 17

    Ans1: Java doesn't allow multiple inheritance DIRECTLY . So if your class is NOT going to extend ANY OTHER class, then use Inheritance ( Keyword : extends) else go for interfaces ( Keyword : implements ) because unlike inheritance you can implement how many ever interfaces you need.

    Ans2: Depends on which sense you mean "ADVANTAGEOUS" . But the rule is, if you implement an interface, you have to implement ALL the methods provided by it. If using Abstract class - provide definition for only those your class needs.

    Ans4: The class can't be instantiated. - other words , NO OBJECT CAN BE CREATED FOR THE CLASS. This is the basic idea of singelton pattern. Only one object will be created and everytime you need an object of the class, you use an accessor method (some get() method) to return you the instance.

    Have a look at my Blog



    Success is not the key to happiness. Happiness is the key to success. If you love what you are doing, you will be successful
    -Herman Cain
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37879
        
      22
    Welcome to the Ranch Dic Pari.

    What do you mean about multiple inheritance DIRECTLY? Java™ doesn't allow multiple inheritance directly, indirectly, vertically, obliquely, tangentially, ethereally, horizontally or anythingelsely.

    I am afraid I disagree with you about No 4. You can instantiate a class with a private constructor. And it doesn't have to be a singleton. There is something else you can't do with such a class, however.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37879
        
      22
    Dic pari wrote: . . .

    Ans2: . . . If using Abstract class - provide definition for only those your class needs.

    . . .
    Not convinced. All the methods must be implemented in a concrete class. In an abstract class, you would provide implementations of those methods which are unlikely to change in subclasses. Those which are likely to be different in subclasses should be left abstract. And we have an FAQ or two about abstract classes.
    Dic scott
    Greenhorn

    Joined: Jun 23, 2011
    Posts: 17

    Campbell Ritchie wrote:
    Dic pari wrote:Ans2: . . . If using Abstract class - provide definition for only those your class needs.

    . . .
    Not convinced. All the methods must be implemented in a concrete class. In an abstract class, you would provide implementations of those methods which are unlikely to change in subclasses. Those which are likely to be different in subclasses should be left abstract. And we have an FAQ or two about abstract classes.
    . . . Agreed... I meant to say, if the subclass is going to provide an implementation of the method, leave it abstract in the Parent class (Abstract class).
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37879
        
      22
    Agreed

    Please use the quote tags only to show what somebody else has said. Otherwise it is hard to see which text is new.
    Dic scott
    Greenhorn

    Joined: Jun 23, 2011
    Posts: 17

    Oops! Am sorry! Pretty new to the Ranch(joined yesterday and already am loving it! ) Will see to that its not repeated.

    and.. sharma sachin, hope you got answers for all your questions. Cheers!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37879
        
      22
    No problem
    Megha kharde
    Greenhorn

    Joined: Nov 24, 2010
    Posts: 19
    Thank you Campbell for providing such a detailed explaination.
    Realy helpfull for beginers like me.

     
     
    subject: Needs answers of some important questions
     
    Similar Threads
    valiveru mock exam errata, please explain correct answers...
    java
    Few Tricky Ques.
    java doubts
    Thread names??