Regarding Object class function access from a user defined class - java

We can use base class functions by extending from subclass.Generally we use equals() method which is defined in Object class.I read in the book that every class will extend Object class and so that we are able to use functions like equals() in our user defined class with subclass reference.
One doubt i am having is with out extending Object class (Even any other class that extends Object class) we are able to use equals method.
can any one explain how it happens?

Every class in Java extends Object by default(unexplicitly) no matter what you do.

If you do:
Class Foo { }
the compiler will treat it as if it were
Class Foo extends Object { }
Even when classes can only have at most 1 super class, if you do
Class Foo extends Bar { }
Class Bar extends Biz{ }
at certain moment up in the hierarchy one of those classes will have the class Object as it's parent, so even when only the topmost class would directly extend from Object, all of the other classes down the hierarchy would indirectly extend Object too, hence giving you access to Object class defined methods such as: clone, equals, finalize, etc...
Regards.

It's an implicit rule, JVM may implement the mechanism.

Related

Class without java.lang.Object as a superclass

We know that by default, every class inherits ultimately from the java.lang.Object class, which is why methods such as toString are readily available to every class. Therefore, a generic class is effectively as follows:
public class Foo extends java.lang.Object {
public Foo() {super(); }
}
However, is it at all possible to create a class which does NOT automatically inherit from the Object class, and thus, has no superclass? And if so, what would be the effect?
We can't write class without having java.lang.object as superclass. Compiler will automatically extend the object class.Only the object class itself and interfaces are the ones which do not extend object class.
No, that is not possible. 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.
I do not believe that you can have a class that does not inherit Object. Quoting from Object as a Superclass..
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. You need not use any of these methods,
but, if you choose to do so, you may need to override them with code
that is specific to your class.
In cases where the inheritance is not explicitly stated, it is implicitly stated. Now, inheritance will obviously not form a cycle. The Object class is the only one which inherits from nobody, it's the top of hierarchy.

Abstract class and methods | Ambiguity in opinions

I just came across two distinct opinions regarding abstract class :
1) One says , abstract method cannot be used in concrete(general) class ; while the abstract classes can have both abstract/non-abstract methods
2) While , a tutorial that has been highly watched on youtube says , " Any class that has an abstract method , will let its class be automatically defined as abstract "
2nd point is totally in contrast to the 1st point ; While implementing it , I did succeed only in the 1st concept and not 2nd though . But , still I want to have a detailed clarity in this regard , if anyone can help me with patience .
As described in the official Java tutorial, "If a class includes abstract methods, then the class itself must be declared abstract". It does not automatically become abstract; it needs to be marked as abstract explicitly.
Any class that contains abstract methods cannot be instantiated because it contains methods that are undefined. Any time an object of a class is created, it must contain all contents of the class and they must all be defined.
So you need to declare the class as abstract which means that the class cannot be instantiated. But just because a class is abstract, doesn't mean it must implement only abstract methods. For example, you can have a static method in an abstract class because calling a static method belongs to the class rather than an object or instance of the class.
An abstract class can have methods abstracts and non abstract. For exemple,
public abstract class Employee {
...
//this method would be implemented in those classes that extends from Employee.
public abstract void calculateSalary();
public Employee addEmployee() {
//Method body
}
But if you declare an abstract method in a non abstract class this has to be converted itself into am abstract one.
On the other hand abstract methods will be implemented in the inherited classes of an abstract parent class. So you would be doing this implementation in a generic class.
I hope this would helps.

Every class is implicitly a subclass of Object Java

I'm reading Java Inheritance Docs and got this paragraph, but I don't what is mean of this. Can any body explain?
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.
Edited
Another point related to this question:
And due to Object is superclass, NetBeans show me extra method list when I try to call any Member of class with object reference? All those methods are declared in Object class?
It means that,in java the Object class is the parent class of all the classes by default. In other words, it is the topmost class or the base class of java. All other classes inherit the properties of Object class.
Meaning of implicitly - suggested though not directly expressed.
If samething applied in Java classes. Consider there is a class called Test.
If you write
public class Test {....}
That is equivalent to
public class Test extends Object {....}
Though you didn't write it, it's equivalent to it. You need not to write extends Object manually. Internally JVM treats that your class is extends Object. Since the part after extends is your class, here Object is your super class.
But,
When you write
public class Test extends BigTest{....}
Things changed now. You are telling BigTest is my parent class. That means you are implicitly writing yourself that BigTest is my parent. Interesting part here is though BigTest is your direct Parent class, internally Object also your Parent.
So now you have 2 parent classes. One is BigTest which you mentioned and other is Object. If you didn't mention anything, only Object is your Parent.
Edit :
This is the reason that you will see extra list of method when you will try to call any member of class with object-reference. Those methods are declared in Object class.
why java do this? any feature of this?
Yes. There are benefits of it. Main reason is to reduce the code duplication.
Continue reading here .... Why Object as super class in Java ?
It means that the following two classes are the same:
class MyClass {
}
class MyClass extends Object {
}
If a class definition doesn't specify extends, the class implicitly extends Object.
The Object class comes with the JRE, you use any class without it. Java does this so you can have a reference to any object e.g.
Object o = x;
This works because no matter what x is, it is also an Object.
Note: even int[] class is a sub-class of Object.

Why mark class abstract when there is no abstract method

I am little confused about abstract class in java. I know that whenever there is an abstract method in the class compiler force developer to mark class abstract. But even we don't have any abstract method in the class we still mark the whole class as abstract. I am not getting the point why we can do this. what is the purpose to allow developer to mark class abstract when there is no abstract method. One can say that reason is that we don't want to create instance of that class. If that the reason then marking constructor of the class private is more suitable rather than marking class abstract.
There is a very useful reason for having an abstract class without abstract methods: Providing default implementations for overridable methods.
There are several perfect examples in the JDK itself. Look - for example - at a WindowAdapter. It implements the WindowListener interface (among others), but provides empty not-doing-anything method implementations. In most cases you want to register a window listener that only overrides one or two of the interface methods. Then your own class simply extends WindowAdapter instead of implementing WindowListener.
Note, that with Java 8 default methods in interfaces this reason does not hold anymore, and in fact abstract classes without abstract methods do not make sense anymore.
I think it's to allow subclasses to be created but not the main class.
I guess your class has method stubs in it, otherwise tyere would be no reason not to instantiate it. It is generally better to use abstract methods for this.
For restricting the class to be instantiated.. Example HttpServlet class.. it is defined abstract but has no abstract methods.. We can use these methods in the subclasses but creating the class httpservlet itself is useless.. thats the reason i think..
HTH!
As stated, this can be to prevent instantiation. I strongly prefer private or protected constructors over this as I feel they communicate the intent more clearly.
Also, in a class hierarchy, if class A is abstract and contains an abstract method, that method does not need to be defined in a class B which extends class A. In this case, class B is marked as abstract and has no abstract members.
To prevent instantiation of a class and use it as a base class. For example, HttpServlet class, an example of template method design pattern where each method already has a behaviour defined. The child class is free to override one or more of them instead of all of them.
One can say that reason is that we don't want to create instance of
that class. If that the reason then marking constructor of the class
private is more suitable rather than marking class abstract.
No it is not at all suitable
This below example will clear your doubts , If you use private constructor , Not only your Object creation is blocked but also you can not even create a subclass of the Parent class
class ParentClass{
private ParentClass(){
}
}
class Subclass extends ParentClass{
static{
System.out.println("Hello");
}
}
You will get compile time error saying
error: ParentClass() has private access in ParentClass
But Marking a class as abstract will block Object creation but will not block Inheritence in java
Update
As you asked in comments that you can make it protected but then your class can be easily instantiated , because protected member can be accessed from the same class as well as in SubClass in same package as well as in a sub class in another package .

Extend a returned Instance

I'm a Java newbie and ran into a bit of a problem. I want a class to become another class. It's hard to explain it the abstract way, so I'll give you an example.
public class WorldGuard extends WorldGuardPlugin {
public WorldGuard(Plugin parent) {
WorldGuardPlugin plugin = parent.getServer().getPluginManager().getPlugin("WorldGuard")
// make plugin the actual class
}
}
WorldGuard should act like some kind of a wrapper here. When constructed it gets one parameter parent on which base it finds an instance of WorldGuardPlugin. The class should now become that instance.
It's simple in JavaScript, I just return the instance, but I can't do this in Java.
I'm not quite sure what you're trying to do. A class cannot "become another class". But perhaps your problem is just that you're trying to use a constructor when you should be using a plain function. Maybe what you want to do is this:
public class WorldGuard extends WorldGuardPlugin
{
public static WorldGuard getFromPlugin(Plugin parent)
{
return (WorldGuard) parent.getServer().getPluginManager().getPlugin("WorldGuard");
}
}
That would get the object via parent and return it as a WorldGuard object.
Class cannot become other class.
I think you can choose among the following possibilities.
User Factory that creates instance of your classes. The factory will choose concrete class according to any logic you want and create instance of "right" class. If you want all classes the factory operates with can implement specific interface, so caller will not even know instance of which class is created.
Use wrapper pattern. In this case your actual WordGuard class will wrap actual instance of other class and delegate all calls there.
Use dynamic proxy or byte code engineering solution. But it is much more complicated and is not the best solution in most cases.
You should call the copy constructor of WorldGuardPlugin. What I mean is that WorldGuardPlugin should have a constructor that can create a copy of a given instance of the class like:
WorldGuardPlugin pg = new WorldGuardPlugin(anInstance);
If this is the case then you are in luck. You can simply do:
public class WorldGuard extends WorldGuardPlugin {
public WorldGuard(Plugin parent) {
super( parent.getServer().getPluginManager().getPlugin("WorldGuard"));
}
}
This will make "WorldGuard act like some kind of a wrapper here". You can still call the methods defined in WorldGuardPlugin on an instance of WorldGuard while being able to add methods to WorldGuard itself.
You can't change the type of object being constructed in a constructor. A constructor, by definition, constructs that class (e.g. A's constructor creates a type A).
In your example, WorldGuard extends WorldGuardPlugin, which means that WorldGuard is a type of WorldGuardPlugin. Maybe there is a way to initialize the WorldGuardPluin class (using a call to super in the constructor) with the properties that you want.

Categories

Resources