java - How can the Object class be a super class of subclasses? - Stack Overflow
The class from which the subclass is derived is called a superclass (also a base class or a parent All Classes in the Java Platform are Descendants of Object. Learn about object inheritance and how it can be used to create a relationship between objects that share common characteristics. Note: Superclass & Subclass comes under Inheritance and concrete class is altogether a different topic. Definitions: A class that is derived from.
Classes near the bottom of the hierarchy provide more specialized behavior. An Example of Inheritance Here is the sample code for a possible implementation of a Bicycle class that was presented in the Classes and Objects lesson: Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods.
However, you didn't have to do all the work. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug. What You Can Do in a Subclass A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in.
If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members: The inherited fields can be used directly, just like any other fields.
Java - Inheritance
You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it not recommended. You can declare new fields in the subclass that are not in the superclass. The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it. You can declare new methods in the subclass that are not in the superclass. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.Java Tutorial 10: SubClasses and SuperClasses in Java
The following sections in this lesson will expand on these topics. Private Members in a Superclass A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
Casting Objects We have seen that an object is of the data type of the class from which it was instantiated. MountainBike is descended from Bicycle and Object.
Therefore, a MountainBike is a Bicycle and is also an Object, and it can be used wherever Bicycle or Object objects are called for. Moreover, a Person object is more likely to be passed as that is what is the one required by the method.
But when we deal with overridden methods, the method call result resolution appears unusual.
Before we look into it, include the following code in a main method and execute the program: The marks are printed along with the name and age. This contradicts what has been already said.
The printDetails method call above has to call the super class version as p is a Person object and Student methods cannot be invoked on p. So, p should also not be having any idea of the overriding method. But during run time, the version in Student was called. This is because of dynamic binding also known as run time binding or run time polymorphism. When a class file is being compiled, the methods that can be called on a particular object referred to by a variable are decided by the data type of the variable.
In the above example, the printDetails method has been invoked on the Student object which is referred to by a Person variable p. Hence, the call would be resolved to the printDetails method defined in the Person class during compile time. This is known as static binding which occurs during compile time.
However, the actual version of the method to be called is decided during run time.
- Java™ How To Program (Early Objects), Tenth Edition by Harvey Deitel, Paul Deitel