Why only java.lang. Object is given superclass in java? [duplicate] - java

This question already has answers here:
Why Object class is Superclass in java [closed]
(4 answers)
Closed 6 years ago.
Is there any reason sun microsystems make Object for all javaclass for superclass. I face the question my last interview. I hope, I can find answers here
Thanks

Following could be the reasons for this design decision,
By having the Object as the super class of all Java classes, without knowing the type we can pass around objects using the Object declaration.
Before generics was introduced, imagine the state of heterogeneous Java collections. A collection class like ArrayList allows to store any type of classes. It was made possible only by Object class hierarchy.
The other reason would be to bring a common blueprint for all classes and have some list of functions same among them. I am referring to methods likehashCode(), clone(), toString() and methods for threading which is defined in Object class.
Please check the below link. I hope it will answer your question.
Why object is super class in JAVA

Related

How marker interface are identified by JVM? [duplicate]

This question already has answers here:
How Marker Interface is handled by JVM
(7 answers)
Closed 7 years ago.
I have gone through few question in stack overflow but could not find a suitable answer. So raising it for more clarity.
I know a marker interface is an interface with no methods. When we implement a marker interface for example Serializable it declares that the class implementing it becomes eligible for serialization.
My question is how JVM understands that the objects of class implementing Serializable interface should be serialized. If i write an interface with no methods and hope that objects of class that implements it will be serialized i'll not work that way.
Is it possible for us to create a custom marker class.?
They aren't 'identified by the JVM' at all. They're identified by the Java code that is interested in them, for example ObjectOutputStream, via the instanceof operator.

why every class in java is a subclass of object? [duplicate]

This question already has answers here:
Why does every object in Java implicitly extend java.lang.Object class?
(11 answers)
Closed 8 years ago.
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.
why do we need a class that is the super class of every other class in java ?
Because this is how Java is being designed. Java treats everything (except pirmitives) as an object including your self-defined objects.
There is an advantage by making all newly created classes to extend from Object. It allows common methods and attributes to be automatically available upon creation of a new object.
Some of the common methods are for example: toString() and equals()..etc
It's useful to have a common behavior/interface among all types for operations like comparison among other things.
It's also useful for when you want to make an array or other collection which contains or can contain different types.
Having Object as an implicit base class of all Java classes helps you write code that does not depend on the precise type, such as a collection, a class that produces string representations, and so on. See documentation of java.lang.Object for a list of methods what every class supports "out of the box".
This is by no means a required feature of all languages: there are other languages where there is no mandatory common subclass. Doing it this way was a choice of the language designers.

Java - Extending Unknown Classes [duplicate]

This question already has answers here:
Extend a generic type in Java
(2 answers)
Closed 8 years ago.
I wanted to create a class that can extend an unknown class that is provided at runtime. I thought that I could do something like this:
public class Foo<T extends Bar> extends T {}
but that doesn't work. Is there a way to do this?
Not without some crazy runtime code generation. Java's generics are not the same sort of thing as C++'s templates: Internally, T is simply treated as an Object, and up- and down-casted as necessary for the benefit of your code. So while a C++ template is instantiated for a particular T, and can decide at compile-time whether that works, in Java the machinery underlying inheritance requires that the actual base class be decided upon in the class definition itself.

Is Method Overloading a Type of Polymorphism? [duplicate]

This question already has answers here:
Is Method Overloading considered polymorphism? [closed]
(8 answers)
Closed 9 years ago.
I was studying about static and dynamic polymorphism and got these links:
http://guruzon.com/1/oop-concepts/polymorphism/what-is-polymorphism-example-tutorial-uml-diagram-notation
http://www.coderanch.com/t/379004/java/java/static-polymorphism-dynamic-polymorphism
http://javarevisited.blogspot.in/2011/08/what-is-polymorphism-in-java-example.html
In all these links , It has been said that Overloading is an example of polymorphism , then I came across two more places where it has been said that overloading has nothing to do with polymorphism, the links are:
Is Method Overloading considered polymorphism?
Head First Java 2nd Edition Chapter 7 (Inheritence and Polymorphism) Last topic: Overloading a method.
Now I am not able to figure out whether it is a kind of polymorphism or not and if it is not the is there anything called static polymorphism in OOPs ?
Sometimes, method overloading is indeed referred to as an example of "static polymorphism". This designation is somewhat arbitrary indeed, but it is possible to make a weak defense in its favor.
The concept of static polymorphism does exist, and it is not limited to OOP. Polymorphism (an ability to present the same behavior in different forms) can be static when polymorphic behavior is achieved based on compile-time type information. The most common example of such polymorphism is templates of C++: all calls are resolved statically, yet the behavior of a template is altered based on the static type of its type parameters.
Overloading could be thought of as a primitive form of choosing the behavior based on the static type of an object, too. However, this view of the overloading does not help understanding of the concept, and also makes it harder for the newcomers to understand the "real" (i.e. dynamic) polymorphism. That is why the term "static polymorphism" is not common in books on Java.

Actual use of interface in java [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Java Interfaces?
What is the use of interface in java? Is interface helps to provide multiple inheritance actually?
Interfaces are used as a contract.
If an Object implements an interface, it is guaranteed to offer the functionality specified in the interface.
Interfaces also allow you to treat Objects (that implement the interface) polymorphicly.
No, multiple inheritance is when you extend behaviours from more than an object.
Interfaces provide a safe way to declare what an object is able to do, not how it does it. That's why you can implement multiple interfaces.
They are used in many kinds of problems, for example when you want to give to your object the same capability that is orthogonal to what these object actually are: for example the interface Comparable states that object can be compared with other objects of the same type. This doesn't relate with what kind of object it is at all.

Categories