Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object class as super class in Java

Tags:

java

class

Why is object class the super class in Java?

like image 389
abson Avatar asked Mar 22 '10 08:03

abson


People also ask

Is Object class A superclass in Java?

Object class is the root or superclass of the class hierarchy, which is present in java.

Can we create Object for super class in Java?

Only a subclass object is created that has superclass variables. This situation is different from a normal assumption that a constructor call means an object of the class is created, so we can't blindly say that whenever a class constructor is executed, the object of that class is created or not. Example: Java.

What are methods of Object super class in Java?

There are five of these methods: public final void notify() public final void notifyAll() public final void wait()

Is Super an Object in Java?

The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.


2 Answers

That is a good question. Java chose to make a single class be the ultimate parent class for everything so that there is an easy way to pass around any arbitrary object, without needing to know its type (i.e. you can use the declared type of Object to refer to every single item in the type system, even primitives using their wrapper classes). However, there are OOP languages such as C++ where there is no universal base class as in Java. Another benefit to having a universal base class is that logic dealing with the superclass does not have to be special cased for top-level classes (with the exception of the universal base class, Object, itself).

like image 106
Michael Aaron Safyan Avatar answered Sep 28 '22 09:09

Michael Aaron Safyan


It's what we call the axiom of object-oriented programming in Java. Every single abstraction in your code is an object. It contains a few things that are applicable to every peace of information you use in your code:

  • equals and hashCode methods to establish an equality theory within the given abstraction (see corresponding javadoc);
  • toString to represent an object in human-readable (probably, only programmer-readable) format (because most of us still use displays and keyboards);
  • getClass to provide reflection capabilities on the given abstraction;
  • some methods to organize object-oriented runtime (garbage collection, synchronization, etc.).

If you learn Java, it is best for you to study the "Inheritance" section of whatever book you use and then try to answer this question yourself.

like image 22
BorisOkunskiy Avatar answered Sep 28 '22 09:09

BorisOkunskiy