java - Call constructor in an abstract class -
java - Call constructor in an abstract class -
is possible phone call constructor in abstract class?
i read constructor can called through 1 of non-abstract subclasses. don't understand statement. can explain example?
you can define constructor in abstract class, can't build object. however, concrete sub-classes can (and must) phone call 1 of constructors defined in abstract parent class.
consider next code example:
public abstract class test { // abstract class constructor public test() { system.out.println("foo"); } // concrete sub class public static class subtest extends test { // no constructor defined, implicitly calls no-arg constructor // parent class } public static void main(string[] args) throws exception { test foo = new test(); // not allowed (compiler error) subtest bar = new subtest(); // allowed, prints "foo" } } java oop constructor abstract-class
Comments
Post a Comment