According to the Java API for the Math class, Math extends the Object class:
public final class Math extends Object
However, Math does not inherit Object's methods, and you also cannot construct a Math object (EDIT: this statement is false and partly false; see below).
My question is then why is this done? If it is an object, then it should have the bare-bones methods from Object. If it is not an object, then I feel like it should not be extending Object.
And, assuming there is a good reason for extending Object, how is it done? In other words, how are the inherited methods and constructor suppressed?
Math is the foremost example in my mind of this kind of static definition class (I guess you'd call it that?), but this question may also apply to classes that have a similar purpose.
EDIT: So I'm aware that all classes implicitly extend Object. What's bothering me is that to me there's a logical disconnect between the notion of an "object" in theory-- something with a state and associated functionality-- and what's being done here.
Object is the super class of everything. All classes extend Object
Math does inherit Object's methods. Every class in Java ultimately has to inherit from Object. But since you can't construct Math objects, it doesn't matter. The methods are inherited, but without being able to construct an instance you can't ever use them from Math.
Every class implicitly extends Object unless they extend another class (Java doesn't allow multiple inheritance).
Still! by extending another class you're, in the very end, extending Object, because the last class that doesn't extend another particular class extends Object.
The concept behind this "default inheritance" can be extracted directly from Object's JavaDoc:
Class Object is the root of the class hierarchy. Every class has
Object as a superclass. All objects, including arrays, implement the
methods of this class.
EDIT: Math does inherit Object's methods. It does not override them tough and the calls are derived to the implementations of the superclass.
You should pay attention to an important aspect of the Math class. The constants and methods it defines are mainly static. Mathematical functions do not depend of a particular instance of the class because those calculations are independent of the context.
You have no particular reason to create an instance of the Math class and that's why its constructor is not visible. You cannot do Math m = new Math() and that's why you don't see methods such as equals or hashcode. In particular since the class is final and can't be extended, you cannot create a sublclass that defines them. IMHO it would be pointless.
Object is the super class of every java class. An you are mistaken that Math does inherit object's methods.
If you want more see the source.
http://www.docjar.com/html/api/java/lang/Math.java.html
Related
As I continue to expand my knowledge of OOP programming, it's becoming a bit more difficult to tie all the concepts back together. Separately, I know what an object, class, interface, and inheritance is. However, I fail to see "the big picture."
An object embodies a specific concept (class) and stores specific data to that class. It's an instance of a class, to be more specific.
A class defines the common set of attributes and behaviors among all objects of its class ("the template")
Interfaces are essentially a to-do list of method headers. It expects its children to implement these abstract methods. It does this in an attempt to create a standard way of doing something in unrelated classes (ex: compareTo() in Comparable)
Inheritance allows subclasses to extend the design space for objects by adding more functionality and behaviors while still retaining that of the superclass.
All these concepts affect the way objects interact with classes, but I fail to see how inheritance and interfaces have anything to do with objects. This is a rather conceptual question, but what exactly is this "big picture?" How do all these concepts affect the relationship between classes and objects?
Inheritance is the OO concept which has many types like Single, Hybrid etc. Java supports Multiple inheritance in the way of Interface. Interface allows you to define the behaviour or functionality in abstract level. It leaves the children to implement those behaviour or functionality. For eg(given in official docs): You can define the behaviour of a Bicycle like gearUp, gearDown, pedal, break, etc., and let the brands to implement their style and functionality. Below is the java doc link.
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
Inheritance in simple terms is to acquire the properties / behaviours of an other class. In order to achieve Inheritance we have to use a keyword extends in JAVA.
Class B extends Class A means Class B will act just like class A and all the methods and Variables in class A can be referred using class B also. ie. Whatever class A possesses it is shared by class B too.
Whereas Interface is a Type Definition Block (User-Defined). An abstract (incomplete) class can be called Interface. An Interface allows only Incomplete methods. In order to Inherit an Interface we have a keyword implements in JAVA. If a Class implements an Interface it is bound to contract.
That is: Either the class implementing the Interface should be declared abstract OR the class should over-ride the method in Interface and Complete it.( ie. provide method body).
Example:
Interface Car
{ public void display(); //Incomplete Method, Method has no Implementation.
}
class B implements Car
{ public void display()
{ System.out.println(“HI”);
} }
Here display method is over-rided and completed in class B. Hence no need to declare class B as abstract.
NOTE: Multiple Inheritance using class is not possible in JAVA. ie. A child class can extend or have only one parent class.
Multiple Inheritance is possible in JAVA using Interfaces. ie. An interface can extend more than one interface / implement more than one interface.
Interface Objects cannot be created, because it is incomplete.
Interface methods should be made non-static, because it is incomplete and should be over-rided in order to make it complete. If interface methods are static, it cannot be over-rided, because only one copy of static members are present and that is loaded during class loading.
We know that by default, every class inherits ultimately from the java.lang.Object class, which is why methods such as toString are readily available to every class. Therefore, a generic class is effectively as follows:
public class Foo extends java.lang.Object {
public Foo() {super(); }
}
However, is it at all possible to create a class which does NOT automatically inherit from the Object class, and thus, has no superclass? And if so, what would be the effect?
We can't write class without having java.lang.object as superclass. Compiler will automatically extend the object class.Only the object class itself and interfaces are the ones which do not extend object class.
No, that is not possible. From the documentation:
Class Object is the root of the class hierarchy. Every class has
Object as a superclass. All objects, including arrays, implement the
methods of this class.
I do not believe that you can have a class that does not inherit Object. Quoting from Object as a Superclass..
The Object class, in the java.lang package, sits at the top of the
class hierarchy tree. Every class is a descendant, direct or indirect,
of the Object class. Every class you use or write inherits the
instance methods of Object. You need not use any of these methods,
but, if you choose to do so, you may need to override them with code
that is specific to your class.
In cases where the inheritance is not explicitly stated, it is implicitly stated. Now, inheritance will obviously not form a cycle. The Object class is the only one which inherits from nobody, it's the top of hierarchy.
So tha main problem is to understand the class hierarchy of Java.
We need to give an answer to this question:
"Explain whether in Java there is one single class hierarchy (with a single class at the top) or there are many class hierarchies (each with its own top-most class). Explain what consequences this has."
I have no idea about how to answer this question..
From the Object JavaDoc:
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
Edit: Also take a look at the spec.
All objects in java inherit from Object
Its the Object class.
The class Object is a superclass (§8.1.4) of all other classes.
Also check this Oracle docs:
The Object class, in the java.lang package, sits at the top of the
class hierarchy tree. Every class is a descendant, direct or indirect,
of the Object class. Every class you use or write inherits the
instance methods of Object.
The Object class is mother of all java classes.
You can verify the same programmatically.
You will get null in the code below for Super Class of Object
Object.class.getSuperclass(); // returns null
The above test can be done for any other class similarly.
It is the Object Class. Every class is directly or indirectly descendant of Object class which is in java.lang package. Unlike any other parent/child class it doesn't require extends key word to inherit. It's implicit by nature where as you can write explicitly. Java compiler doesn't deny it.
You can use/call methods of object class from any other class and at the same time you can overwrite them to implement in your way.
Are Constructors not members of a class? If they are, why cannot they be inherited?
JLS 7.0 says that constructors are not members and hence cannot be inherited. Is that true alone for Java or is it a general OOP paradigm?
Constructors are not inherited for a good design reason. If they are inherited, then you'd have problems trying to hide them in the inherited class.
Consider this example:
class Plane{
public Plane(int passengerCapacity, int cargoCapacity){...}
}
class F18 extends Plane{
public F18(){
super(0,0);
....
}
}
Now, does the following constructor make sense?
Plane p = new F18(0,0);
Not really, because the child class is a special case of the parent class. In this particular example, it does not make sense to provide the Plane constructors in the F18 class; the child class wishes to control how the parent class is constructed.
If constructors are inherited and someone wish to hide the parent constructors, they may attempt to do something like this:
class F18{
Plane innerPlane;
public F18(){
innerPlane = new Plane(0,0);
}
}
Not really a solution because in every method and property, one would have to write return innerPlane.SomeMethod();, which pretty much defeats the purpose of inheritance.
This principals apply to other OOP languages (e.g. C#) as well.
But what if I really want to inherit constructors in many child class...
Then your architecture is wrong. You don't actually need inheritance to solve your problem. You can abstract the parts that are different in another base class / interface. e.g.
class Plane{
IPilot Pilot;
public Plane(int passengerCapacity, int cargoCapacity, IPilot pilot){...}
}
Constructors are ways to construct an object. Do you really need many child class which can be constructed in the same way (and using the same logic, since they're inherited), or a class which has the flexibility to swap out some of its components?
The concept of inheriting a constructor does not make sense in most paradigms; instead the constructors are called in reverse order of inheritance, so for example if you have:
Pidgeon -> Bird -> Animal
Where -> indicates 'inherits from', then to construct a Pidgeon it first calls the Animal constructor, then the Bird constructor and, finally, the Pidgeon constructor. If it didn't do it this way then you would have to either initialise all members of the class you're inheriting from or manually call the constructor of the base class (as you may have to do anyway if you have specific parameters to pass).
Or, to put in another, whereas normally a function in the derived class replaces the function in the base class, constructors extend the function in the base class.
Despite you may hear, this is not general OOP paradigm. I think, there are old OOP languages, where constructor was just normal method. But currently many popular languages treat constructors in different manner, as other methods. This is imperfect solution with leads to some workarounds, like recommendation in Beans specification to have only none-argument constructors.
why we should not use static and abstract for a single method?
the static keyword is defined so that a method can be called by a class name rather then an object. that means the method has to have some sort of definition. but abstract means you do not have any details about what the method does, it is as it says **Abstract**. When you inherit or extend a class you can then define the method.
Think of an interface.
If you are asking about having a static method inside of an abstract class, that is a different story. An abstract class is essentially as mentioned an interface and contains just a template of say functions that you must later on implement by inheriting / extending the class. Once you extend that class the static method does not come along with it (that is by default unless the access modifier is public / protected).
A static method is not inherited. Therefore, making it abstract is a nonsense.
The abstract keyword means that child classes must override the method - this is (one of the ways) Java supports polymorphism. If you want to make it so that subclasses cannot override the method you mark it final. So it would be impossible to have an "abstract final" method since they are the exact opposite of each other.
the static keyword implies final as well - all static method are also final. Thus it is impossible to have a method that is both static and abstract since you would be able to make a method that is abstract and final.
The reason for static being final is that it is bound to the class instead of the instance. That means that the compiler looks it up at compile time rather than runtime to determine which method to call. The reason what it is like that? Arbitrary decision that the designers of Java made - they could have allowed static method to be overridden but decided not to. I don't have any particular insight as to why the chose one over the other unfortunately.
As others have said, static+abstract is nonsense in Java. But there have been (rare) occasions where I've wished I could do just that.
The result I was looking for was basically to say that... "all concrete classes that extent this abstract class (or implement this interface) must provide a static method with this signature." This capability would allow these classes to provide meta-information about themselves.
Normally I have ended up with an instance method in these cases. If you stipulate that concrete implementations must support the default (no-arg) constructior, you can do...
MyInterface obj = MyClassThatImplementsMyInterface.newInstance();
obj.invokeTheMethodIWishWasBothStaticAndAbstract();