Skip to main content
added 20 characters in body
Source Link
Sean Patrick Floyd
  • 300.6k
  • 72
  • 481
  • 598
public class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public class SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(); } } 

my expected output: subclass method1 superclass method1 superclass method2

subclass method1
superclass method1
superclass method2

actual output: subclass method1 superclass method1 subclass method2

subclass method1
superclass method1
subclass method2

I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn't happening. Any ideas as to how I can make it happen?

public class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public class SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(); } } 

my expected output: subclass method1 superclass method1 superclass method2

actual output: subclass method1 superclass method1 subclass method2

I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn't happening. Any ideas as to how I can make it happen?

public class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public class SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(); } } 

my expected output:

subclass method1
superclass method1
superclass method2

actual output:

subclass method1
superclass method1
subclass method2

I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn't happening. Any ideas as to how I can make it happen?

Typo in code
Source Link
Shervin Asgari
  • 24.6k
  • 32
  • 109
  • 145
public Classclass SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public Classclass SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(); } } 

my expected output: subclass method1 superclass method1 superclass method2

actual output: subclass method1 superclass method1 subclass method2

I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn't happening. Any ideas as to how I can make it happen?

public Class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public Class SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(); } } 

my expected output: subclass method1 superclass method1 superclass method2

actual output: subclass method1 superclass method1 subclass method2

I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn't happening. Any ideas as to how I can make it happen?

public class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public class SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(); } } 

my expected output: subclass method1 superclass method1 superclass method2

actual output: subclass method1 superclass method1 subclass method2

I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn't happening. Any ideas as to how I can make it happen?

Source Link
jsonfry
  • 2.1k
  • 2
  • 15
  • 17

Java: Calling a super method which calls an overridden method

public Class SuperClass { public void method1() { System.out.println("superclass method1"); this.method2(); } public void method2() { System.out.println("superclass method2"); } } public Class SubClass extends SuperClass { @Override public void method1() { System.out.println("subclass method1"); super.method1(); } @Override public void method2() { System.out.println("subclass method2"); } } public class Demo { public static void main(String[] args) { SubClass mSubClass = new SubClass(); mSubClass.method1(); } } 

my expected output: subclass method1 superclass method1 superclass method2

actual output: subclass method1 superclass method1 subclass method2

I know technically I have overriden a public method, but I figured that because I was calling the super, any calls within the super would stay in the super, this isn't happening. Any ideas as to how I can make it happen?