Does interface extend Object class? [duplicate] - java

This question already has answers here:
Do interfaces inherit from Object class in java
(9 answers)
Closed 9 years ago.
Means, by default in Java every class extends Object class.
So interface also extends Object class or not?

"by default in Java every class extends Object class. So interface also extends Object class or not?"
No...
1. interface can only extend another interface. in java every class extends object class (Not every interface).
If an interface has no parent, then it will IMPLICITLY have methods of the object class.

Reference types all inherit from java.lang.Object. Classes, enums, arrays, and interfaces are all reference types
(from http://docs.oracle.com/javase/tutorial/reflect/class/index.html).
So I think the answer is yes, it does.

Can you make instances of interfaces? : No
We need concrete classes which implement the interface which also implicitly extend Object class.
Also can you call methods defined in Object class in interfaces? : No(Not directly atleast. Maybe using it as a polymorphic reference.)
Interfaces and Objects are two different concepts. You cannot mix them. If anything that is common between them is that they are in general used a polymorphic references.
As for how does the code compile when we use interface as polymorphic reference
and call Object methods on it?
An interface has one implicit method declared for each public method in Object. For more details you can refer to this question.

Related

How to get a reference to the subclass inside an abstract java class [duplicate]

This question already has answers here:
Get Concrete Class name from Abstract Class
(2 answers)
Closed 5 years ago.
All abstract classes in Java are obviously extended by a concrete class and cannot be instantiated on their own, how can I get a reference to the class that is extending my abstract class? My use case involves reflection and in my parent abstract class I want to introspect the methods on the concrete subclass and I can't figure out how to get a reference to the subclass without adding an abstract method forcing the subclass to return its Class. Any ideas?!
You can use this as a reference to the current object. By calling this.getClass() inside your abstract class, you will get the Class instance for the subclass during runtime.
Additionally, your question was already answered here:
Get Concrete Class name from Abstract Class
Please, correct me, if I understood you wrong.
Your abstract class doesn't "exist" at runtime, only concrete implementations can be instantiated. Any method that is defined in the abstract superclass and is not overridden in the concrete class can reference "itself" through the this reference.

why it is not posssible to create object for interface and abstract class in java [duplicate]

This question already has answers here:
Instantiate an Interface [duplicate]
(4 answers)
Closed 7 years ago.
I know that we can't create object for interface and abstract class. my doubt is why? why can't we create an object for interface and abstract class?
for example
class Demo{
}
Demo demo = new Demo();//it is possible
but
interface Demo{
}
Demo demo = new Demo();// it is not possible why?
An object in java contains data (class members) and set of operations (methods). These operations are performed on the data stored by the object.
Now both interface and abstract class don't provide concrete implementation of operations, which could be performed on the object data. Basically interface and abstract classes in the java are used to define/declare the contract for an object. This contract of operations is later fulfilled by the concrete/complete classes.
So these incomplete objects will be of no use in the application. So java does not allow creating instance of interface or abstract classes.
Because both interface and abstract class could have abstract methods which doesn't have implementation that's why is unclear what new Demo().myMethod() would do.

Java Object class and multiple inheritance [duplicate]

This question already has answers here:
If one class is derived from another that is derived from Object, is that "multiple inheritence"
(7 answers)
Closed 7 years ago.
It may be very very basic question but didn't find any answer so far, so asking here. By default in java, every class extends Object class as far as i know. But again how we are able to extend any other class as multiple inheritance is not possible in java. Thanks in advance.
A class can't have more than one direct super-class, but it can have multiple ancestors.
For example, ArrayList extends AbstractList which extends AbstractCollection which extends Object. Even though ArrayList has 3 ancestors, it only has one direct super-class - AbstractList.
Object is parent class of all classes by default but as soon as you inherit your class from any other class now your class is not direct descendant of Object class any more and hierarchy goes up to the Object class.
Just to add to what Beri said, you can have a situation like this:
Class class extends Object implements Interface_1,Interface_2,Interface_3{
}
If you are trying to add more to the program, you can always add more classes and import their methods.
It should also be noted that the subclass does not inherit the private variables and methods from the superclass.

Regarding extending of classes [duplicate]

This question already has answers here:
Java doesn't support multiple inheritance but implicitly every class in java extends Object and allows one more [duplicate]
(8 answers)
Closed 10 years ago.
I have a query in java, that since Object class is the parent class for all, I want to know that is Object class is being implicitly extended by every class in java.
For example: if I make an object class B then I extend class A that is ok since in java, class can extend only a single class at most, but since my class B has implicitly extended the Object class then how can it extend class A? Please advise.
Only when a parent is not specified does it implicitly inherit off of Object, otherwise it only inherits off of the specified parent.
Side note:
This is allowed:
C
|
A
|
B
In the above case, replace C with Object to be in line with what you're asking.
This is not allowed:
C A
\ /
B
The inheritance can be as deep as you want it to be, but one object cannot have multiple parents.
Think of it as a family tree where individuals only have one parent. Each individual only has one parent, but all that parent's ancestors are also ancestors of the individual. That's what's meant when it is said that all classes extend Object: it is the most remote ancestor of all, though only the direct ancestor (parent) of those classes that are declared with extends Object or with no extends declaration at all.
I think you know the concept but you must be a bit confused. Think of a tree. Object class is the root of that tree. It has leaves and other nodes in between. No matter how your class hierarchy is designed, it will eventually an Object because its root is that. So you can extend any class (not abstract or Interface), without thinking of Object class. Because Object extention will be done automatically for you.
Here, I will suggest, the keyword is EXPLICIT. You can EXPLICITLY extend only one class. But if you don't do that Java will implicitly extend your class with Object class. If you have done that Object must be somewhere up in the hierarchy of the inherited class.

Does every java class inherit from Object class implicitly [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Classes that don’t inherit Object class
I am wondering if each class in Java is inherited from Object implicitly. I am reading a book which said no, I got quite confused.
That book is lying. Take a look at any class from the Java API and you'll notice the very first parent class is, in fact, Object.
BTW : what is that book anyway?
** Edit **
Let's clarify one thing first :
Q: Does every java class inherit from Object class implicitly?
A: If you consider that the class Object cannot have a superclass "because it is the primordial class and has no direct superclass", then no, therefore your book was right.
However, if you consider that "the class Object is a superclass of all other classes", and is excluded from the question (as a class cannot inherit itself), then by definition yes.
From my understanding of the language and it's specifications, is that "all class and array types inherit the methods of class Object" should be taken litteral. Meaning that every object created possess the methods defined in the class Object. For this reason, and since the class Object has no superclass (because there is no superclass from the specification!), and also since all class inherits those methods (the methods are available in any object created by the JVM), then if a class does not explicitly extends Object (because it's not the class Object), it does so implicitly by itself or from a superclass by the virtual machine.
Quoting the documentation:
public class Object
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.
As you can see... Class inherit from Object:
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html
public final class Class<T>
extends Object
implements Serializable, GenericDeclaration, Type, AnnotatedElement
throw this book to Trash!!
The book can be correct in one case. The Object class doesn't extends Object. But remember every other does.
See documentation.
Class Object

Categories

Resources