Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Needs answers of some important questions

 
sharma sachin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11854
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are your thoughts on these homework questions?
 
sharma sachin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would an interface provide stronger polymorphism than an abstract class? What is "strong polymorphism"?
 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is mean by context-innitialization parameters before stopping the server?
 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


insert this method inside the class

but as I know hash code also must be overide to take effect this.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
give me full code you try to compile
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this code

 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mmm Serialization

 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the code and try this

 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String class
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and thank you whoever moved this thread; it never was a JDBC question in the first place.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no need try it will produce NullPointerException becouse of class A equals()..
if use String if primative compile error..
 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 17
    Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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


     
    Campbell Ritchie
    Sheriff
    Posts: 48404
    56
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48404
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 17
    Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48404
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 17
    Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48404
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No problem
     
    Megha kharde
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Campbell for providing such a detailed explaination.
    Realy helpfull for beginers like me.

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic