Default class that is extended by all classes in java - java

Is there any Default Class that is extended by all the classes by default in Java?
Example: If I have a simple class like:
Class A {
String a;
}
Is this class extending a class by default?

java.lang.Object class is superclass of all classes.
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.
You can test it :
A a = new A();
if(a instanceof Object){
System.out.println("Object is superclass of all classes");
}

In Java, everything (apart from the plain old data types; int, boolean, double etc.) is implicitly derived from java.lang.Object.
In particular, the class contains useful functions such as lock() and notify() which are used in thread synchronisation.
For a full list, see http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html

Yes it is and it is extending Object class.
Object is root class of all java classes.

"All Classes in the Java Platform are Descendants of Object":
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

java.lang.Object is the super clas of all the classes.
all the Java provided classes or the class which you create by your self all are the sub class of Object class by default

yes all the classes by default extend Object class in java. Is that what you wanted?

Yes, it is extending java.lang.Object.

Class Object is the root of the class hierarchy. Every class has Object as a superclass.

yes "Object" class is root class for all other classes. Here is an example to prove it to find the package and class using the Object reference variable.As you can see I have not included Object class explicitly to the project but still I can assign the reference variable to the class "Object" and use it as the class "FindingClass" is inheriting the Object class,Object class reference variable can now access the "FindingClass" object.It is possible only when the current class "FindingClass" is inheriting Object class.
package Chapter9.Packages;
class FindingClass{
}
public class FindClass {
public static void main(String[] args) {
Object obj;
FindingClass fcls = new FindingClass();
obj=fcls;
System.out.println(obj.getClass());
}
}
Output:
class Chapter9.Packages.FindingClass

import java.util.*;
default class in java

Related

Default Superclass in Java

I was reading an article on Inheritance . Few facts were listed on Inheritance but one point i couldn't understand what is Default superclass and its explanation. What is Default superclass?
Default Superclass : Except Object class, 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 the Object class.
The default superclass is Object (more precisely java.lang.Object). If a class does not define a direct superclass explicitly (via extends), then Object is implicitly a superclass of that class.
Have a look at the following example which graphically shows this:
public class A {}
public class B extends A {}
public class C {}
Note that his rule does not apply to Object itself since this would produce a cyclic inheritance. In other words, java.lang.Object is the root of the class hierarchy.
Except Object class, which has no Super Class, 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 the Object class
Points to understand first:
There is a class Object in java which is already available with the JRE libraries.
When you define a class without an extends keyword, your class will by default extend the Object class in Java.
Even if you extend another class in your new class, the parent or its parent transitively inherits the Object class.
Simple way to understand - after you define a class with / without a parent class, create an object for it. If you are using an IDE, you can see that there are some method suggestions which is not implemented in your class (or parent ). Methods like clone() equals() hashCode() wait() etc. Where did these methods/ behaviors come from to your object ? - Yes it came from the ultimate parent Object
The default inheritance is implicit and handled by the Java itself. Hope this makes your understanding better.
object class is base class for every class you create.
when you create an object of your class then constructor of object class is called.
The Object class in Java is the default superclass. Object class is inherited into a newly created class by default if it does not explicitly inherited from any other class. So every class that you create in Java programming is inherently a child class of the Object.
All classes in Java extend Object class by default, and they are subclass of object class, the only exception of this convention is the Object class itself, object class does not extend any class by default, this is the whole idea.

Does Object class also extends some other class?

Every Class in java extends Object class. Does Object class also extends some other class?
Does Object class also extends some other class?
No. It is the first class in the hierarchy.
Quoting 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.
no it is the first defined object in the hierarchy, you can see the hierarchy on the Object.java api.
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

class a keyword or a Class?

When we create a class Class_Name, we code:
class Class_Name
{
}
Is the word class a keyword or it is a Class?
The word class is a keyword. It can be used to denote a class or to get a class object such as String.class.
This may be somewhat confusing as there is a class called Class. It's an object that represents a class itself.
For example,
Class<?> strClazz=String.class;
has the Class as a type and class as a keyword.
On an unrelated note, the Java documentation calls Class objects clazz to avoid conflict with keywords. Also, Class is technically a generic in that Foo.class will return a Class<Foo>, allowing for the lack of covariance to restrict class objects in parameters.
Yes class is a keyword to tell the java compiler that you are writing this piece of code as a class. YourClass.methodname is used to call methods of a class in a static way.
When we should call a function using its Class name in Java?
You should do this when you want to call a static method of a class.

Is the object of an abstract class an anonymous inner class? [duplicate]

This question already has answers here:
Can we instantiate an abstract class?
(16 answers)
Closed 9 years ago.
When I create the object of an Abstract class, I have to do like this just like an interface.
AbstractClass abstractClass = new AbstractClass() {
#Override
public void abstractMethod() {
}
};
Does this mean that the object of AbstractClass is an anonymous inner class object?
AbstractClass abstractClass = new AbstractClass() {
#Override
public void abstractMethod() {
}
};
This block of code means that you are creating an anonymous class which extends AbstractClass. You can also use the same notation for an interface also.
SomeInterface someImplementingClass = new SomeInterface(){/*some code here*/};
This means you are creating a class that implements SomeInterface.
Note that there are certain limitation when you are creating an anonymous class. As and anonymous class is already extending the parent type, you cannot make it extend another class as in java you can extend only on class.
This code will help understand concept of overriding methods in anonymous classes
class Anonymous {
public void someMethod(){
System.out.println("This is from Anonymous");
}
}
class TestAnonymous{
// this is the reference of superclass
Anonymous a = new Anonymous(){ // anonymous class definition starts here
public void someMethod(){
System.out.println("This is in the subclass of Anonymous");
}
public void anotherMethod(){
System.out.println("This is in the another method from subclass that is not in suprerclass");
}
}; // and class ends here
public static void main(String [] args){
TestAnonymous ta = new TestAnonymous();
ta.a.someMethod();
// ta.a.anotherMethod(); commented because this does not compile
// for the obvious reason that we are using the superclass reference and it
// cannot access the method in the subclass that is not in superclass
}
}
This outputs
This is in the subclass of Anonymous
Remember that anotherMethod is implemented in implemented in the subclass that is created as anonymous class. and a is the reference variable of type Anonymous i.e. superclass of the anonymous class. So the statement ta.a.anotherMethod(); gives compiler error as anotherMethod() is not available in Anonymous.
An object is not a class object (in this context). It is derived from a class. In Java there is a difference between classes and objects, as compared to e.g. prototype based languages (e.g. JavaScript) where this difference does not exist.
In your example you create an anonymous class, create an object of that anonymous class and assign it to a variable; all in one step.
Anonymous classes are always inner classes: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9.5
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3
One fundamental properties of attract classes is the fact that there could be no direct instance of this type. Only classes that implement the complete interface of an class can be instantiated.
In order to create an object you need a non abstract class first, by extending the abstract class.
Abstract classes does not have any instances (objects of it's type). I recommend Mavia to take a look at following link for clearness:
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
You cannot create an object of an abstract class. They are non-instantiable. What you are doing when you do this is creating a sort of dynamic subclass object, and instantiating that (at the same time). More or less, yes, you can create it the same way as an interface. See this answer for more information.
In fact here you create both: an anonymous inner class, that extends AbstractClass as well as an instance of this anonyomous class, the object. You do not and cannot create an instance of AbstractClass.
Also you declare a variable named abstractClass that has the Type AbstractClass. Inside this variable you store a newly created instance of your newly defined subclass of AbstractClass.
EDIT: You can of course not reuse the anonymous inner class, since it is anonymous, and the only place where an instance of it can be created or rather is created is right here.
Here might be a loop or function in which case you would be able to create many instances of this anonymous inner class. But it would still be only this piece of code where instances are created.

Inheritance of Object Class in Java

While i was reading java book, i came across "Every class extends class Object"...but if want a class B to extend class A.....but Class B will be now having multiple inheritance,one from Object class and one from Class A.How the conflict is resolved.
Can anyone explain?
Its multi-level inheritance, not multiple:
class A extends Object
class B extends A
First of all, Object class is the super/base/parent class of every class including user-defined classes.
So even if we don't mention it explicitly, the user-defined classes extends Object class by default.
Morevoer, Object class implements a set of methods and variables which are common to all the objects being created in the application. This is the main reason why we have Object class as a base class for all the other classes.
For eg:
hashCode() - this method creates a unique identity for each of the object being created in JVM.
ClassB extends from ClassA which also extends from Object. Therefore ClassB extends Object indirectly, through classA
"Every class extends class Object" just means if you don't specify the parent class, it takes on Object as the parent class
The book was trying to explain that every class is either a direct or indirect subclass of Object. Among other things, this means that every class inherits the public methods of Object: toString(), hashcode(), wait(), etc. It also means that whatever class variable a happens to be, you can always assign a to a variable of class Object.
There is no such thing as multiple inheritance in Java. The closest Java comes to that is interfaces, which is a whole subject in itself.
there is no conflict.. take a look at this structure
animal
bird
sparrow
parrot
dog
poodle
cat
the parrot class gets all attributes/methods of its super-class bird and from its super-class animal. this is called multiple inheritance.
You get traits from your parents right? You also get traits from their parents also.

Categories