Java class by default, it will implicitly extend java.lang.Object [duplicate] - java

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 7 years ago.
In this tutorial (http://www.studytonight.com/java/object-and-classes) I read that a java class may optionally extend one parent class. By default, it will extend java.lang.Object.
Note: important statement that i was read that Java enums extend the java.lang.Enum class implicitly, so your enum types cannot extend another class.
according to note our normal java class should not extend other class like enum (enum types cannot extend another class).but we can able to inherit one class. is this multiple inheritance.?
in java class can derived by extends keyword. like this
class SomeClass
{ }
class MyClass extends SomeClass{}
How can all java classes by default extends java.lang.Object class without extends keyword in java?
When our class extends some base class, it becomes multiple inheritance. I searched in stackoverflow, but still I am not clear.
By default any class extends Object class. Doesn't it mean java supports multiple inheritance?
Can anybody clarify this with a simple example.

Every class, except for java.lang.Object, extends exactly one class.
If you write extends Something, then your class extends Something.
If you don't write extends Something, then your class extends java.lang.Object. (the same as if you wrote extends Object)

If you don't extend any class, you will still extend Object. If you explicitely extend some class, than you extend just this class, but the extended class will in other turn extend Object by default. This way Object is always in class hierarchy.

Related

About the Multiple Inheritance in java? Since Java Doesn't support Multiple Inheritance [duplicate]

This question already has answers here:
Java Multiple Inheritance
(17 answers)
Closed 7 years ago.
For eg:
Case 1: Does every Java class we create will extend Object class and if so then how come we an able to extend one more class to our class may be silly question just got it in mind i am posting this question
Pretty simple:
The hierarchy of java-classes is tree-like, with Object as root. If a class doesn't extend any other class it directly extends Object by default. So what about classes that extend some other class, like class A extends B. Now A extends B and Object. But this is not multiple inheritance. A extends B, thus A aswell extends any superclass of B, including Object. Just imagine the inheritance this way:
Object
/ | \
B ... SomeObject
|
A
Yes every class extends Object. However, Object is not necessarily the direct superclass. If a class Child extends Parent, then Child's direct superclass is Parent. In this case, the class hierarchy is:
Child < Parent < Object
So there is no multiple inheritance for Child. Object is inherited transitively by its direct superclass Parent.
No offense, but your english makes your question very difficult to read. I think I understood it though. Multiple inheritance is not the contrary of inheriting only from one class. You can inherit from several classes and still not have multiple inheritance.
For example, C inherit from A and B if A extends B, B extends C. However, the inheritance is a linear chain here, whereas multiple inheritance allows for trees, for example A extends B, A extends C. This is different, as you see. In java, you can inherit from as many classes as you like by transitivity but directly extend only one class.
Multiple inheritance is when you try to inherit more than a class at the same time,
I mean like
class Foo extends Faa, Fii {
}
that is not posible in Java but we can have this
class Faa{
}
class Fii extends Faa{
}
class Foo extends Fii{
}
that is not the same, in that case we have 2 parents (i mean Fii because we are extending from Fii and Faa because Fii extends from it)

when abstract class doesn't contain any abstract method [duplicate]

This question already has answers here:
Why use an abstract class without abstract methods?
(11 answers)
Closed 7 years ago.
I am still confused when abstract class doesn't contain any abstract method, what a purpose of it? why don't use regular class rather than abstract class if it doesn't contain any abstract method ? In fact, I was saw this situation is applied on java and libgdx library or perhaps for every library.
So, because this situation, I was thinking is it very important to know why use abstract class without abstract method rather than regular class.
When you make a class abstract (either with or without abstract methods), you are forcing the users of this class to create concrete sub-classes of it, since they can't instantiate it.
A user of an abstract class must create a concrete derived class.
This can be useful since it allows the author of an abstract class to introduce abstract functions at a later date. The amount of refactoring necessary at that time is then significantly reduced.

Multiple Inheritance in Java allowed with Object class? [duplicate]

This question already has answers here:
Inheritance in java and Superclasses(Object, Class)
(7 answers)
Closed 7 years ago.
In Java, they say that Multiple Inheritance is not supported. Also its a fact, that each class in Java extends class Object. So if I write :
public class ThreadInstance extends Thread {
}
How does this compile? ThreadInstance here is actually extending Thread as well as Object. Isn't it multiple Inheritance here.
By multiple inheritance you should understand inheriting multiple classes at the same time, e.g. it's impossible to create a
public class ThreadInstance extends Thread, Object {
}
because class hierarchy would look like this:
ThreadInstance
^ ^
Thread Object
When you define your ThreadInstance like you did, the ThreadInstance inherits Object too, but it first inherits thread.
ThreadInstance
^
Thread
^
Object
There is no multiple inheritance there.
Your class extends Thread but Thread already extends Object so it's still single-inheritance as it follows the same line of inheritance
Object is implicitly inherited by every class. In your ThreadInstance class, Thread is explicitly inherited.
Thus every Java class except Object must have a super-class. There's nothing all to special about implicit inheritance, it's simply there to reduce boilerplate code.
Your class that extends that other class, but it extends Object, too, so you're still in one line of inheritance, not two.
Eg.
ThreadInstance extends Thread, but Thread in turn extends Object so cant I say ThreadInstance extends Object.

Multiple Inheritence Related Confusion in Java

I have confusion in java inheritance. As I studied Java does not support Multiple Inheritance. So that it does not have diamond problem of inheritance at all.
But, every class of Java inherits Object class by default, and if we extend a class then there are two inherited classes first is Object and second is our inherited class. As I know if any class inherits two or more classes, its known as Multiple Inheritance. It shows that java supports multiple inheritance because its inheriting two classes at same time.
Then why its said that java does not support multiple inheritance and is there any possibility to have diamond problem in java?
Since all classes extend Object, the base class you're extending is by definition already extending Object. Your class doesn't need to add Object itself since it already exists in the hierarchy.
That means, Object will still only exist once in your hierarchy as the very root, so no "diamond".
See this
OBJECT CLASS
|
|
CLASS A{} //Class A extends class Object
|
|
CLASS B{} //Class B extends class A
|
|
CLASS C{}//Class C extends class B
Since every Class is inherited from Object class it is not in the case When you inherit from other class.Thus the object class will become super or the root class for all other inherited classes.
So whats the problem?
But, every class of Java inherits Object class by default, and if we extend a class then there are two inherited classes first is Object and second is our inherited class
That's partially correct.
If you extend a class explicitely, then it won't extend Object class.
if you have a class
class A
{
}
then, compiler will change it into
class A extends Object
{
}
But, if you extends a class explicitely, as following,
class A extends AnotherClass
{
}
compiler won't add anything now, hence No multiple inheritance.
You have misunderstood the concept of Class extending Object in case of Inheritance.
Only the top level class in the inheritance hierarchy, extends from Object class, and rest of the class lower in the hierarchy, extend the immediate super class. Thus they have all the methods of Object class through this hierarchy of inheritance, and there is no multiple inheritance involved.
public class A { // Extends from `Object` class
}
class B extends A { // Extends from `A`
}
class C extends B { // Extends from `B`
}
So, there will be no diamond problem in the hierarchy.
Java supports only syntactical multiple inheritance. Java does not
supports implementation of multiple inheritance.
Some people says java supports multiple inheritance through interfaces but it's not correct here the explanation:
inheritance ::
Getting the properties from one class object to another class object:
Class A{}
Class B extends A {}
Here an object of class B getting the properties(methods/functions/ & data members/class variables)
of class A.
Why java does not supports multiple inheritance using classes:
Class A{}
Class B{}
Class C extends A,B{}
X--this statement causes error because Class A getting object of object class from both sides A and B...
Every java class by default extending Object of object class and Object of object class is the root object means super class for all classes.
But here Class C having two super classes of object so giving error means java does not support multiple inheritance by using classes.
Is java supports multiple inheritance using Interfaces:
Because of this interface concept only few of us saying that java supports multiple inheritance but it is wrong.
Here the explanation::
interface A{}
interface B{}
interface C implements A , B{}
(or)
interface A{}
interface B{}
Class C implements A , B{}
Here its look like multiple inheritance but inheritance means getting the properties from one class object to another class object.
Here in interface concept we are not at all getting any properties rather we are implementing the unimplemented methods of interface in class.
So inheritance and intefaces are quite opposite.......
So finally java supports only syntax of multiple inheritance does not supports implementation of multiple inheritance.
Inheritance is like debit and interface is like credit but interface has its own importance in other concepts like server side programming.
Not exactly in Java you can only inherit from 1 distinct class at a time. That distinct class might inherit from another class however you don't have a single class inheriting from multiple distinct classes...
You can have many ancestors but only one parent. Here ancestors mean Object class where as parent mean the class which you are extending.
By specifying a class to inherit you override the default extends Object. So only when you don't specify a class to inherit does a class extend Object.
The class you extend from already extends from Object. When you extend from the new class, you in effect stop extending directly from Object, and instead inherit it via the new direct ancestor.
So it's not that you're extending from two different classes, it's that you're extending from a class that itself already extends from another class, namely the Object class.
Visually, multiple inheritance would be:
A Object
| |
C
But the situation you describe is actually:
Object
|
A
|
C

What's the difference between the implements & extends keywords in Java [duplicate]

This question already has answers here:
Implements vs extends: When to use? What's the difference?
(19 answers)
Closed 8 years ago.
What's the difference between the following keywords in Java: implements, extends?
An interface is an abstract specification of how a class should behave whilst a class is a concrete implementation of such a specification.
Therefore, when you write implements you're saying that you are fulfilling some abstract specification in the implementation you've written.
extends means that you take either an implementation (class) or specification (interface) and add to it with different or new functionality (or change the specification of its behaviour), thus modifying its behaviour and extend-ing it.
a class extends another class and implements interface. interface extends another interface.
Interface hasn't any implemented methods all defined methods are empty so if class inherits from the interface it should implement it's methods. But if Class1 inherits from Class2 then it already have some working methods (from Class2) and just extends Class2.

Categories