Question

Please answer the following question in java: 1. Why do we need to override the method...

Please answer the following question in java:

1. Why do we need to override the method public boolean equals(Object ob) in any class?

2. Is public boolean equals(Object ob) is same as public boolean equals(Circle ob)?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Why do we need to override the method public boolean equals(Object ob) in any class?

equals(Object ob) method belongs to the object class. so every class will automatically inherit this equals(Object ob) method. Internally this equal method compares two objects based on their reference memory value.

But if we want to compare the object based on the value we want then we override the equals() method.

2. Is public boolean equals(Object ob) is same as public boolean equals(Circle ob)?

equals(Object ob)

the above function will compare object class object to whatever object we calling on it. But equals(Circle ob) function will compare only the same two Circle objects.

OutPut wise even if instead of equals(Circle ob) we write equals(Object ob), it won't create any problem, cause Circle class is child of Object class. But it is not recommended to do that.

Add a comment
Know the answer?
Add Answer to:
Please answer the following question in java: 1. Why do we need to override the method...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming. please give me answer and explain in details. Question 1 (c) (4 marks] For...

    Java Programming. please give me answer and explain in details. Question 1 (c) (4 marks] For each of the methods equals and hashCode in the following class, explain whether or not they are implemented correctly (with respect to their specification in the object class and any additional specification in this class). If they are not implemented correctly. show how they may be rewritten to address the problems that you have identified. *An immutable representation of a term in a polynomial....

  • in java Part 1 In this section, we relook at the main method by examining passing...

    in java Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags when running programs. On command line, for example, we may do “java MyProgram -a -v". Depending on options, the program may do things differently. For example, "a" may do all things while "-v" display messages verbosely. You can provide options in Eclipse instead of command line: "Run ... Run Configurations ... Arguments". Create a Java class (Main.java)....

  • QUESTION 21 In the Java programming language method overriding occurs when (a) and precedes the call with an @Override...

    QUESTION 21 In the Java programming language method overriding occurs when (a) and precedes the call with an @Override annotation a method in a child class calls the method of the same name in one of its ancestor classes (b) when a class defines an abstract method and precedes the declaration with an @Override annotation two or more methods of a class have the same name but different signatures and both of (c) them are marked with an @Override annotation...

  • This is a question about Java abstract classes. The correct answer is E and I do...

    This is a question about Java abstract classes. The correct answer is E and I do not understand why. Please be specific! I am a beginner in Java abstract classes and methods... 13.6 What is the output of running class Test? public class Test {   public static void main(String[] args) {     new Circle9();   } } public abstract class GeometricObject {   protected GeometricObject() {     System.out.print("A");   }   protected GeometricObject(String color, boolean filled) {     System.out.print("B");   } } public class Circle9 extends GeometricObject {...

  • In this question, your task is to override a standard public method of the Object class...

    In this question, your task is to override a standard public method of the Object class for the Card class with an instance variable, int serial, such that if you write a print command for an object of this class, it will print out the following output: The Serial# of this Card is: XXXXX In the above output, XXXXX is the value of serial variable from the given Card object. Now, complete that standard method for this task. Hint: You...

  • Need help with this Java question in 1 hour please JAVA Using the files from the...

    Need help with this Java question in 1 hour please JAVA Using the files from the in-class assignment add two additional animals of your choice. For each animal add two additional methods appropriate to your particular animal. As the majority of the code for this program exists you will not need an algorithm. Use the 3 java classes below. Thank you //Animal.java public class Animal {    public Animal() {    System.out.println("A new animal has been created!");    }   ...

  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • Could somebody please help.! Please DON't copy paste from other places and please read the question...

    Could somebody please help.! Please DON't copy paste from other places and please read the question and answer exactly what's asked. Thank you I much appreciate it. You are to write a program name MyArrayStack.java that create/build the ArrayStack data structure. The class must be written to accept any type of Objects. The following must be implemented i.e. YOU must write the code (do not import any stack from the Java Library): 1. One default constructor that will create an...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students...

    PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT