Object instantiation using different Constructors

Shamal Sandeep :

Recently my teacher was talking about using different constructors to instantiate objects. But I'm really confused about that. For that I wanna understand why the I get the following compile 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 the output

superClass

But when I try to call subClass() via 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 so, according to this, I can assume that the even I instantiate object with a different constructor, only the Object type specified is loaded to the RAM.

But, when I use the override here like this,

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 sub class called. Which makes me really confused about this. Anyone can please explain me what happens here when I use a different constructor to instantiate an Object.

mhlz :

At runtime the JVM knows that your s variable is a "SubClass" and thus can call the right (overwritten) method.

The problem you're having is at compile time though. The compiler tries to verify your program to make sure that you didn't make any mistakes. It has no idea about the types of variables except the type you're telling it 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 that the compiler does this is, that you told the compiler 's' is of SuperClass type. So, any assignment of anything that extends SuperClass is a valid assignment for 's'. Theoretically you could assign a SuperClass to 's'. If the compiler would not perform this check you would be able to write code that calls methods on objects that might not have those methods, which would lead to a runtime error. Having an error like that randomly come up during runtime is much worse than the compiler just checking all the assignments and calls since you can fix those immediately and the runtime errors are sometimes hard to find and fix.

As others have pointed out you can tell the compiler about your 's' actually being a 'SubClass' later on by casting it:

((SubClass) s).subClass();

With this you basically tell the compiler: "I know that s is actually a 'SubClass', so please treat it like one for this part". If you're somehow wrong about that though and your 's' is actually a 'SuperClass' during runtime you will get a 'ClassCastException' which is a runtime error, since the JVM does not know what to do at this point.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Explicit Instantiation of Templated Constructors

From Dev

Object instantiation fails when using overloaded constructor

From Dev

create instances of the class by using different constructors

From Java

Java Simple Factory with constructors using different parameters

From Dev

Using this when declaring constructors with different parameters

From Dev

Using Delphi object constructors on preallocated memory

From Dev

Creating constructors using "this" instead of simply returning an object

From Dev

Using an object in a single class with multiple constructors

From Dev

Using multiple constructors with one object in Java

From Dev

Using different minimal complete definitions for different datatype constructors

From Dev

Object Constructors

From Dev

how I create objects of the given object type in a method with different constructors

From Dev

Instantiation exception: no empty constructors found in Main Activity

From Dev

Does object instantiation have to occur inside using block expression?

From Dev

How to get the class identifier from object instantiation using Roslyn

From Java

New object instantiation when using Java 8 streams

From Dev

Using constructor parameter variable names during object instantiation in Python?

From Dev

creating objects by using different constructors and freeing memory of the objects in the end

From Dev

Different static instantiation

From Dev

Using Object.assign to avoid redundancy in Typescript constructors

From Java

Can I Instantiate a class using the class object? What about Constructors?

From Dev

Definition of object and instantiation

From Dev

Instantiation fails in a python object

From Dev

Python mock object instantiation

From Dev

Object instantiation in Python

From Dev

Python conditional object instantiation

From Java

JUnit TestCase object instantiation

From

Object instantiation by reference

From Dev

Object instantiation variants in scala

Related Related

  1. 1

    Explicit Instantiation of Templated Constructors

  2. 2

    Object instantiation fails when using overloaded constructor

  3. 3

    create instances of the class by using different constructors

  4. 4

    Java Simple Factory with constructors using different parameters

  5. 5

    Using this when declaring constructors with different parameters

  6. 6

    Using Delphi object constructors on preallocated memory

  7. 7

    Creating constructors using "this" instead of simply returning an object

  8. 8

    Using an object in a single class with multiple constructors

  9. 9

    Using multiple constructors with one object in Java

  10. 10

    Using different minimal complete definitions for different datatype constructors

  11. 11

    Object Constructors

  12. 12

    how I create objects of the given object type in a method with different constructors

  13. 13

    Instantiation exception: no empty constructors found in Main Activity

  14. 14

    Does object instantiation have to occur inside using block expression?

  15. 15

    How to get the class identifier from object instantiation using Roslyn

  16. 16

    New object instantiation when using Java 8 streams

  17. 17

    Using constructor parameter variable names during object instantiation in Python?

  18. 18

    creating objects by using different constructors and freeing memory of the objects in the end

  19. 19

    Different static instantiation

  20. 20

    Using Object.assign to avoid redundancy in Typescript constructors

  21. 21

    Can I Instantiate a class using the class object? What about Constructors?

  22. 22

    Definition of object and instantiation

  23. 23

    Instantiation fails in a python object

  24. 24

    Python mock object instantiation

  25. 25

    Object instantiation in Python

  26. 26

    Python conditional object instantiation

  27. 27

    JUnit TestCase object instantiation

  28. 28

    Object instantiation by reference

  29. 29

    Object instantiation variants in scala

HotTag

Archive