Creating an instance of an object using different constructors

Recently, my teacher talked about using different constructors to create objects. But I'm really confused by this. For this, I want to understand why I get the following compilation error.

class SuperClass { void superClass(){ System.out.println("superClass"); } } class SubClass extends SuperClass { void subClass(){ System.out.println("subClass"); } } class Call { public static void main(String args[]){ SuperClass s = new SubClass(); s.superClass(); } } 

When I compile and run the following code, I get output

 superClass 

But when I try to call subClass() through the s object, I get the following error.

 damn.java:17: cannot find symbol symbol : method subClass() location: class SuperClass s.subClass(); ^ 1 error 

OK, therefore, in accordance with this, I can assume that even I create an object using another constructor, only the specified type of the object is loaded into RAM.

But, when I use override here,

 class SuperClass { void superClass(){ System.out.println("superClass"); } } class SubClass extends SuperClass { void superClass(){ System.out.println("subClass"); } } class Call { public static void main(String args[]){ SuperClass s = new SubClass(); s.superClass(); } } 

I get the method in the called subclass. Because of what I'm really confused about this. Anyone can explain what happens here when I use another constructor to instantiate an object.

+5
source share
9 answers

At run time, the JVM knows that your variable s is a "SubClass" and therefore can call the correct (overwritten) method.

The problem you are facing at compile time. The compiler tries to check your program to make sure that you haven't made any mistakes. He has no idea about the types of variables, except for the type you are talking about.

 // the Java compiler remembers that there is a variable called 's' and that // it has the type 'SuperClass'. Note that the compiler does not check the // actual type of the instance. It just checks to see if the assignment is // is valid. Since the right side is of type 'SubClass' and the left side // has the type 'SuperClass' which is a parent of 'SubClass' this assignment // is valid for the compiler. But it ONLY remembers that 's' is of type // 'SuperClass' since that is what you told it about 's'. SuperClass s = new SubClass(); // Here the compiler sees 's' and looks up its type. The type of 's' is // 'SuperClass' as remembered earlier. javac will no go and look up the // definition of 'SuperClass' and finds out that 'SuperClass' does not // have a method with the name 'subClass' so you get a compiler error. s.subClass(); 

The reason the compiler does this is because you said that the compiler is of type SuperClass. Thus, any assignment for anything that extends SuperClass is a valid assignment for 's'. Theoretically, you can assign SuperClass 's'. If the compiler does not perform this check, you can write code that calls methods on objects that may not have these methods, which will lead to a runtime error. Having an error like randomly occurring at runtime is much worse than a compiler just checking all assignments and calls, since you can fix them immediately, and runtime errors are sometimes hard to find and fix.

As others have pointed out, you can tell the compiler that you are actually a β€œSubClass” later by saying it:

 ((SubClass) s).subClass(); 

With this, you basically tell the compiler: "I know that s is actually a" SubClass ", so please consider it as one for this part." If you are somehow mistaken about this, and although your "really" SuperClass is at run time, you will get a "ClassCastException", which is a run-time error, because the JVM does not know what to do at the moment.

+4
source

You declared your instance as an instance of SuperClass, so it will behave as such.

If you want to call subClass, there are two options:

  • ((SubClass)s).subClass();
  • Declare it as SubClass: SubClass s = new SubClass();

In any case, you need to tell the JVM that you want it to work with the behavior of SubClass, not SuperClass.

+3
source

You create a SuperClass object:

 SuperClass s = new SubClass(); 

This is also an instance of SubClass , but to use all the functions of SubClass that do not have SuperClass , you must drop them or create a SubClass :

 SubClass s = new SubClass(); 

To better understand this, think about the person and architect who builds things:

 Person a = new Architect(); a.build(); // ERROR! not every person knows how to build! Architect a = new Architect(); a.build(); // SUCCESS! 
+3
source

First of all, you are not asking about different constructors of the same class. You ask about different constructors of different classes - SubClass and SuperClass .

Secondly, what you do in the second snippet is called overriding, not overloading.

In Java, there is a difference between the type of compilation time of a variable and the type of runtime of this variable.

Here:

 SuperClass s = new SubClass(); 

The compilation time type is SuperClass, but the execution type is SubClass. You are allowed to call methods declared in the compile time type. However, if the runtime type overrides the method that exists in the compile-time class, the run-time class method will be called at run time.

+3
source

First of all, the methods you wrote are not constructors because they have a return value type and are not called a class (capital letter). The constructor must have the same name as the class and return type.

In addition to your questions. This code works:

  SuperClass s = new SubClass(); s.superClass(); 

Since the variable s is defined as SuperClass, and the s.superClass () method is defined in the SuperClass class (it doesn't matter that it is actually an instance of SubClass, because SubClass is also a SuperClass.

  SuperClass s = new SubClass(); s.subClass(); 

For the same reason, this does not work here. The variable is of type SuperClass, and SuperClass has no subClass method. If you want to use the variable as an instance of SubClass, then you should do:

  SubClasss = new SubClass(); s.subClass(); 
+1
source

This is because you create your s as a SubClass object when you like it: s = new SubClass()

Also in your first case, you need to pass your s object to SubClass , because the compiler did not know that s is of type SubClass , something like: ((SubClass)s).subClass()

0
source

If the link is of type SuperClass , Java compilation does not β€œknow” that the SubClass runtime value stored in it does not allow you to call SubClass methods. You can, however, explicitly β€œtell” the compiler that it is a SubClass by casting in order to access these methods. Please note that such a casting in no way changes the actual object - it just provides more information to the compiler regarding what can and cannot call:

 SuperClass s = new SubClass(); ((SubClass) s).subClass(); 
0
source

Your problem (I think) in this line ...

 SuperClass s = new SubClass(); 

The object you create is of type Subclass, BUT the variable s that you insert is of type SuperClass, so it does not see the subClass () method.

In your second example, the superClass () method in SubClass overrides (thinks it is replacing) the SuperClass method with the same name.

Becaues SuperClass has a method of this name, you can simply call this method.

Since the variable s contains an instance of SubClass, it calls this version of the method.

Hope this helps. It seems confusing now, but there will come a time when you will be amazed how hard it has ever been for you - just stick to it :)

0
source

When you instantiate an object using the new keyword, regardless of your reference, the object is created from the class that is used in the new keyword.

In this case: When you did this SuperClass s = new SubClass(); , this means that s is able to hold the SuperClass object, however, when you say new SubClass() , it actually created an object of the SubClass class.

And now you call the SuperClass class method on SuperClass s = new SubClass(); which leads to this error because your actual object is not of type SuperClass and therefore you cannot call its method (s).

0
source

All Articles