How does inheritance work in Java?

We have the following classes:

class Super { void foo() { System.out.println("Super"); } } class Sub extends Super { void foo() { super.foo(); System.out.println("Sub"); } } public class Clazz { public static void main(String[] args) { new Sub().foo(); } } 

Exit:

Super

Sub

Questions:

What is super ? Is this an object of the parent class, which child saves as a field?

  • If so, how does inheriting abstract classes work? You cannot instantiate an abstract class.
  • If not, then where is the overridden method held?

I tried Google, but all I found is general information on how to inherit classes, etc.

Update:

You still tell me the obvious things. Perhaps my question was a little misleading, but I will try to rephrase it:

  • When we call a method with super , you say that we are referring to the parent method. But how can we call this method without a parent?
  • Is super the same as this ? this is a reference to a specific object, as you know.
+8
java inheritance
source share
6 answers

A child class does not support any special field that represents its parent. You can think of something along the lines of inner classes that do maintain a reference to their outer class. This is a special case and does not reflect how superclasses relate to each other.

Internally, the JVM maintains a "method table" by matching each class with its loaded methods available for that class. The JVM is also aware of the relationships between all loaded classes, including super-sub relationships.

When you call the super function, the JVM actually does a couple of things:

  • Defines the parent class of the class called by the method from
  • Defines the parent method to be called
  • Invokes a method with a special instruction ( invokespecial )

If you should examine the class file for your Sub class, you will see something like this for the foo method:

 void foo(); flags: Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #2 // Method Super.foo:()V 4: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 7: ldc #4 // String Sub 9: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 

Line 1 in the listing shows a special statement that invokes the superclass method.

A good reading source would be the Java Virtual Machine Specification , in particular Section 2.11.8 .

+5
source share

Good. Release, in turn, through your code.

Your first statement in your "foo" method

 super.foo(); 

It is good that the explicit call to the superclass method foo . What is:

  void foo() { System.out.println("Super"); } 

Thus, "Super" is displayed on the console because you explicitly called this method using the super keyword. super refers to the superclass of the class, in the same way that this refers to the current class.

Next is the rest of the foo method in a subclass:

  void foo() { super.foo(); System.out.println("Sub"); } 

Well, after super.foo() is called, it's time to move on to the next statement that prints "Sub".


The reason your program goes to the subclass method first rather than the superclass is because of a principle called Polymorphism . That is, a subclass takes a method from a superclass and changes its behavior.

Abstract classes

You cannot instantiate abstract classes, but, but with the super keyword, you can still access the functionality of the superclass.

In the context of a Java virtual machine

So, what happens when you call a method call, the Java virtual machine will look for the method in the local class, if it is an instance method. If he cannot find him, he will move to the superclass. When you use the Polymorphism principle, the JVM finds a method with the correct signature in the subclass and stops looking. This is how inheritance and Polymorphism work in simple terms in the context of Java.

When overriding a method, you add a method with the same method signature (name, number and type of method fields) to the subclass definition. So the JVM finds this, and an overridden method is stored here.

+3
source share

super is a keyword that allows you to call a method implementation defined in a superclass. This is not a field of your subclass.

If not, where is the override method stored?

I'm not quite sure what you mean by that, but:

  • the method that prints "Super" is stored in the class definition of the superclass
  • the method that prints "Sub" is stored in the class definition of the subclass.

Since Sub extends super , the definition of the Sub class includes a reference to the definition of the super class.

Answering updated questions:

When we call a method with super, you say that we are executing the parent method. But how can we call this method without a parent?

A method is just a block of code, just a sequence of bytecode instructions that we need to execute. When you call a method, the task of the JVM is to determine the name and parameters of the method that you give, where to find this block of code. Usually, as others have said, he will first consider the class definition of the class of the object on which the method was called. When you use super , you tell the JVM not to look here and instead look at the definition of the parent class.

Therefore, you do not need separate instances of super and Sub , because Sub is super ( new Sub() instanceof Super is true ), but because the JVM knows that the super keyword means that it must look for the code that makes up the method in the definition super class.

Is this compatible with this? this is a link to a specific object, as you know.

No, they are not the same. this is a reference to the current object, while super not a reference to the object, instead it is a keyword that affects where the JVM will look for code that defines the method that is being called.

+3
source share

when you write super.foo(); , you call the superclass method.

The foo method of the sub class overrides the foo method of the Super class by adding instructions to the superclass method.

+1
source share

The foo method overrides .super.foo () in a subclass, calls print super, and then System.out.println ("Sub"); shows Sub.

try this for inheritance

 class Super { Super() { System.out.println("1"); } void foo() { System.out.println("Super"); } } class Sub extends Super { public Sub() { // TODO Auto-generated constructor stub System.out.println("2"); } void foo() { super.foo(); System.out.println("Sub"); } } 
+1
source share

There is only one object, which is simultaneously Sub, Super and Object. It has all non-static fields for each of these classes. A class really only needs one copy of the code for its methods, even non-static ones.

0
source share

All Articles