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

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.

Related

What is Abstraction in Java i need to understand it properly? [duplicate]

This question already has answers here:
Understanding the purpose of Abstract Classes in Java
(8 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I understand basics of abstraction but still I have some uncertainties.
We cannot create obj of abstract class.
Abstract methods are declared in abstract class but defined in child class.
And we call abstract methods using obj of child class.
I don't get why we using abstract classes if we can do all the things through objects of the child class. We defined abstract methods in child class so it also means we declared it in child class too and we calling that method so it working like normal classes and child.
What is practical purpose of abstract classes?
from what I can see from your question you look at abstract classes as interfaces, but you could and should use them differently.
In an abstract class there could be not abstract methods, meaning a method where all of the child classes use the super class method, this way you can avoid duplicate code.
You can take a look here for an explenation : https://softwareengineering.stackexchange.com/questions/106601/in-simple-words-what-are-are-the-purposes-of-abstract-classes-and-or-interfaces

Multiple Inheritance in Java even if class inherits Object [duplicate]

This question already has answers here:
Java : If A extends B and B extends Object, is that multiple inheritance
(11 answers)
Closed 3 years ago.
All classes in Java are inherited from Object class by default.
Then how the class inherited can inherit other classes?
Java do not support Multiple inheritance, right?
Classes inherit from at least and at most one class,
Either from Object implicitly (without writing extends), or from other class explicitly (as extends YourParentClass)
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object
Because each class inherit from Object or from class inherit from Object, each class still inherit from Object, for example toString() method
If your custom class inherit from different class, still at the end of the hierarchy, the parent class will be Object, notice that classes hierarchy isn't multiple inheritance:
The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes
At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.
As you already correctly stated, all classes in Java are inherited from Object class by default. That means that any class that does NOT have an extends clause defined, implicitly has written extends Object. Example
class Animal // implicitly written extends Object
{}
On the other hand, if a class has an extends clause, then it does not directly inherit from Object. But its parent class inherits from object. As inheritance is transitive, therefore the class also inherits the functions of Object. Example
class Dog extends Animal {}
class Animal // implicitly written extends Object
{}
This means Dog is an Animal and Animal is an Object. Therefore Dog is an Object. You could write Object o = (Object) new Dog(); and that would totally be fine.
This can go arbitrarily deep. For example you could now have a class Poodle which inherits Dog which inherits Animal which inherits Object. Therefore a Poodle is an Object.
By using inheritance , a child class will inherit object class through parent class.
What you are talking about is known as - Multilevel inheritance.
Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.
i.e. - A is Object class, B is the base class that you want to inherit and C is your child class.
btw Java 8 has introduced default method to handle basic multiple inheritance problem ( though I don't agree that it solves the actual problem) - check default methods in Java 8

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.

Java class by default, it will implicitly extend java.lang.Object [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 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.

Categories