简体   繁体   中英

Extending a class in Java without initializing

public class LinkedListStructs {
    public LinkedList l1 = new LinkedList();

    public LinkedListStructs(){
        ListNode h1 = new ListNode(4);
        ListNode h2 = new ListNode(3);
        ListNode h3 = new ListNode(12);
        ListNode h4 = new ListNode(9);
        ListNode h5 = new ListNode(9);
        ListNode h6 = new ListNode(4);
        l1.head = h1;
        h1.next = h2;
        h2.next = h3;
        h3.next = h4;
        h4.next = h5;
        h5.next = h6;
    }

}

Then I extend this in my other class:

public class Tester extends LinkedListStructs{

    public void removeDupsTest(){
      l1.printList();
      l1.removeDups();
      l1.printList();
      }

    }

I didn't initialize a new instance of LinkedListStructs . My question is, when I extend a class, is an instance automatically created?

I am confused because I used LinkedList l1 in my tester class, but the object itself needs to be initialized with a constructor as you see with public LinkedListStructs(){}

So, how does it work? If I don't create an instance, initialize properties of linked list then how is it possible to use it?

Thanks

To use your Tester , you have to create an instance of it. When you create it, you are calling its constructor.

Since you haven't specified a constructor, the compiler creates one for you. Your empty constructor will call the superclass constructor.

To test this, add an empty constructor to your Tester class and printLns in both the Tester and LinkedListStructs constructor. You will see the tester constructor called, which in turn will call the superclass constructor.

Note that your empty constructor must call super(), which calls the superclass constructor.

Java figures it out for you: when you extend a class that has a parameterless constructor, and do not define a constructor, your derived class automatically gets a default constructor:

JLS section 8.8.9:

If a class contains no constructor declarations, then a default constructor with no parameters is automatically provided.

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

When you construct an instance of Tester , the constructor of its base class LinkedListStructs gets called, initializing the list l1 .

To answer your doubt:

Since you have extended the LinkedListStructs and created a new class Tester (meaning of 'public class Tester extends LinkedListStructs')

All the behaviour will be available in the derived(or extended) class. This is the fundamental of Inheritance (and hence reusability). You may choose to override it as well. This is for polymorphism (you can have 'implements' to achive this).

Since, there is no explicit constructor in Tester class. The default constructor will be called, which in turn will call all its super classes constructor. Default constructor documentation can be found here.

I guess that's the direct answer to your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM