0

Say that I have a super class and a sub class like below.

class Super { 
 public void action( ) { 
 System.out.println( “Super’s action” ); 
 } 
} 

class Sub extends Super( ) { 
 public void action( ) { 
 System.out.println( “Sub’s action” ); 
 } 
}

In the main method I call this.

Super s1 = new Super( ); 
s1.action( ); 

Super s2 = new Sub( ); 
s2.action( ); 

Sub s3 = new Sub( ); 
s3.action( ); 

I know that the output will be

Super’s action 
 Sub’s action 
 Sub’s action 

I've learnt that in the constructor, the first word is the static class type and the part with the () is the dynamic class. I know that the method lookup will always begin in the dynamic class so what I want to know is, what is the difference between s2 and s3? i.e Having the static class be of the parent type, or the static class be it's own type

Jared Y
  • 35
  • 3

1 Answers1

0

If Sub defines a method that isn't in Super, then you can't call it from s2. Also, if a method takes an argument of type Sub, you can't pass s2 to it.

Ismail Badawi
  • 36,054
  • 7
  • 85
  • 97