Why object class is superclass in java
Answer: Math class and subclass 8. Which is the output of the following code fragment? Answer: Math. After execution of line 2, sbuf still references the same instance. Answer: StringBuffer reference 8. What is the output of the following code string literal comparision? Answer: string literal comparision 8. What is the output of the following code string literal and reference? Answer: string literal and reference 8. Which of the following are legal Java statements? Answer: Integer wrapper 8.
Answer: generic class 8. Which of the following are legal enhanced while loop? Answer: enhanced while loop 8. What is the legal clone methods when subclassing Object? Answer: clone method 8. Which of the following classes implement java. Answer: java. For the moment, you need to know that clone must be declared as.
If the object on which clone was invoked does implement the Cloneable interface, Object 's implementation of the clone method creates an object of the same class as the original object and initializes the new object's member variables to have the same values as the original object's corresponding member variables.
The simplest way to make your class cloneable is to add implements Cloneable to your class's declaration. For some classes, the default behavior of Object 's clone method works just fine. If, however, an object contains a reference to an external object, say ObjExternal , you may need to override clone to get correct behavior.
Otherwise, a change in ObjExternal made by one object will be visible in its clone also. Then the original object references ObjExternal and the clone references a clone of ObjExternal , so that the object and its clone are truly independent. The equals method compares two objects for equality and returns true if they are equal. For primitive data types, this gives the correct result. For objects, however, it does not. To test whether two objects are equal in the sense of equivalency containing the same information , you must override the equals method.
Here is an example of a Book class that overrides equals :. This program displays objects are equal even though firstBook and secondBook reference two distinct objects. They are considered equal because the objects compared contain the same ISBN number.
You should always override the equals method if the identity operator is not appropriate for your class. The Object class provides a callback method, finalize , that may be invoked on an object when it becomes garbage. Please reply if u need further clarifications. Object class is the super class of all java class and present in java.
Prabhat 01 Apr At compile-time java compiler javac checks for super class of compiling class. If class have a super class then super class have extended Object class ii. If class have not super class the compiler adds Object class as super class of compiling class Means a class should have a super class Object class otherwise its multilevel inheritence's top class will be surely Object class.
Each time we create a main method, java compiler implicitly extends it with Object Class. This was done to make things easier for a programmer or the person who is writing the code. If it was not made like that, it would have been little complex even to use the System.
So in general to make some things easier, Oject class was made the super class of all the classes in java. Tahseen 30 Mar as we know that Java is object oriented language purely. Neha 29 Mar bcz declaring d object as a super class.. Other Related Discussions. Why we cannot override static method? What default access modifier do? Prime Number Algorithm. The advantages of JavaScript far exceed the disadvantages, The advantages o.
How did common sense influenc. The youths with similar talents will answer your query. Ask a question and start a new topic! I want to ask a question about core java? Ask a Question.
0コメント