How to inherit the constructor from a super class to a sub class?
Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass.
Here is an example of how this works:
class Foo {
Foo(String str) { }
}
class Bar extends Foo {
Bar(String str) {
// Here I am explicitly calling the superclass
// constructor - since constructors are not inherited
// you must chain them like this.
super(str);
}
}
Superclass constructor CAN'T be inherited in extended class. Although it can be invoked in extended class constructor's with super() as the first statement.
Default constructors -- public constructors with out arguments (either declared or implied) -- are inherited by default. You can try the following code for an example of this:
public class CtorTest {
public static void main(String[] args) {
final Sub sub = new Sub();
System.err.println("Finished.");
}
private static class Base {
public Base() {
System.err.println("In Base ctor");
}
}
private static class Sub extends Base {
public Sub() {
System.err.println("In Sub ctor");
}
}
}
If you want to explicitly call a constructor from a super class, you need to do something like this:
public class Ctor2Test {
public static void main(String[] args) {
final Sub sub = new Sub();
System.err.println("Finished.");
}
private static class Base {
public Base() {
System.err.println("In Base ctor");
}
public Base(final String toPrint) {
System.err.println("In Base ctor. To Print: " + toPrint);
}
}
private static class Sub extends Base {
public Sub() {
super("Hello World!");
System.err.println("In Sub ctor");
}
}
}
The only caveat is that the super() call must come as the first line of your constructor, else the compiler will get mad at you.
Read about the super keyword (Scroll down the Subclass Constructors). If I understand your question, you probably want to call a superclass constructor?
It is worth noting that the Java compiler will automatically put in a no-arg constructor call to the superclass if you do not explicitly invoke a superclass constructor.
Say if you have
/**
*
*/
public KKSSocket(final KKSApp app, final String name) {
this.app = app;
this.name = name;
...
}
then a sub-class named KKSUDPSocket extending KKSSocket could have:
/**
* #param app
* #param path
* #param remoteAddr
*/
public KKSUDPSocket(KKSApp app, String path, KKSAddress remoteAddr) {
super(app, path, remoteAddr);
}
and
/**
* #param app
* #param path
*/
public KKSUDPSocket(KKSApp app, String path) {
super(app, path);
}
You simply pass the arguments up the constructor chain, like method calls to super classes, but using super(...) which references the super-class constructor and passes in the given args.
You inherit class attributes, not class constructors .This is how it goes :
If no constructor is added in the super class, if no then the compiler adds a no argument contructor. This default constructor is invoked implicitly whenever a new instance of the sub class is created . Here the sub class may or may not have constructor, all is ok .
if a constructor is provided in the super class, the compiler will see if it is a no arg constructor or a constructor with parameters.
if no args, then the compiler will invoke it for any sub class instanciation . Here also the sub class may or may not have constructor, all is ok .
if 1 or more contructors in the parent class have parameters and no args constructor is absent, then the subclass has to have at least 1 constructor where an implicit call for the parent class construct is made via super (parent_contractor params) .
this way you are sure that the inherited class attributes are always instanciated .
Related
I want to implement an Abstract Java class. One of the abstract methods must be implemented by each child class, to ensure that that part of the code will be executed individually in each child class.
Is there any other way to do it and thus avoid the "warning" that appears by calling an abstract method from the constructor of the main class?
public abstract class Listener {
protected Date checkTime = new Date();
protected TypeUpdate type = UNKNOW;
public Listener(){
super();
this.setTypeListener();
}
public void setTime(Date date) {
if (date != null) {
return;
}
this.checkTime = date;
}
/* Abstract methods */
public abstract void execute();
protected abstract void setTypeListener();
}
Thank you.
------------------------------ EDITED ----------
Ok, It's an error call an abstract method in constructor. So, what can I do to force inheriting classes to make a concrete constructor implementation (for example, initialize a member in one way or another?)
You are arriving in the base class constructor and on returning from that constructor,
in the child class constructor all field initialisations will happen and the remaining code form the child constructor.
To ensure that some method is called you can either call that method lazily later.
Or pass an independent object, as "part" of the entire child.
public Child() {
super(createPart());
}
private static Part createPart() {
return new Part().withAnswer(42);
}
public Base(Part part) { ... }
One may also have a case of
service offered (public final)
requirement implemented (abstract protected)
So:
class Base {
public final void foo() {
onFoo();
}
protected void onFoo() {
throw new IllegalStateException("Missing onFoo implementation "
+ getClass().getName());
}
}
Method calls are always determined based on the runtime type of the object, however how can I call shadowed methods of base classes from an inherit instance?
class Base {
String str = "base";
String str() { return str; }
}
class Impl extends Base {
String str = "impl";
String str() { return str; }
}
class Test {
public static void main(String[] args) {
System.out.println(((Base) new Impl()).str); // print 'base'
System.out.println(((Base) new Impl()).str()); // print 'impl'
}
}
For example above, how can I call the str() method of Base class from an Impl instance, preferably without using reflection?
Using the Keyword super
Accessing Superclass Members
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). an example below.
public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
calling printMethod since child class
Here is a subclass, called Subclass, that overrides printMethod():
public class Subclass extends Superclass {
// overrides printMethod in Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
}
if you want to read more
https://docs.oracle.com/javase/tutorial/java/IandI/super.html
I'm learning about inheritance and am working with this simple program that has has a superclass and a subclass as shown below. My question isn't specific to this program; however, this is where I've first seen this happen so I'm using it as an example for a more general conceptual question. Why does simply instantiating the class run the constructors and output the contents? My previous understanding was that instantiating the class simply creates the object but it wont do anything.
SuperClass1.java
public class SuperClass1 {
public SuperClass1(){
System.out.println("This is the superclass constructor.");
}
}
SubClass2.java
public class SubClass2 extends SuperClass1
{
public SubClass2()
{
System.out.println("This is the subclass constructor.");
}
}
Main.java
public class Main {
public static void main(String[] args)
{
SubClass2 obj1 = new SubClass2(); // why should this print something?
}
}
Output
This is the superclass constructor.
This is the subclass constructor.
First of all, instantiating an object means calling (and executing) the constructor, that is what it is for.
So, this:
SubClass2 newInstance = <createNewInstance>;
newInstance.<init()>;
is both done by the constructor call new SubClass2() in Java. There is no separation between "constructing" the object and "initialising" its properties.
Furthermore, if you do not explicitly call another constructor of a super class the default constructor (the one without arguments) is automatically called first thing when creating an object of a class. So instantiating an object of the subclass calls the superclass contructor (which prints the first line), and then prints the second line itself.
More in detail, the subclass looks like this behind the scene:
public class SubClass2 extends SuperClass1
{
public SubClass2()
{
super(); // calls the superclass constructor
System.out.println("This is the subclass constructor.");
}
}
Because the constructor you call includes a print statement.
You call the constructor method SubClass2() which has a print statement in it.
The statements are not printed because the class ist loaded, but because an object of that class in instantiated and the constructors are called:
That a class can be loaded without using constructor is demonstrated by the following code:
public class Test {
public static void main(String[] args) {
try {
Class.forName("Test$Inner");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
static class Inner {
static {
System.out.println("static initializer");
}
public Inner() {
System.out.println("inner ctor");
}
}
}
running that program shows that only the static class initializer is called and no constructor.
I'm extending from an abstract class named ChildClass, it has 4 constructors which should be implemented.
There is a set of general configuration common to all constructors.
I could abstract these tasks and call it in all constructors.
Is there anyway to call a specif method when an object is going to be initialized rather than calling it in all of the constructor signatures?
Since Java compiler must ensure a call to a constructor of the base class, you can place the common code in a constructor of your abstract base class:
abstract class BaseClass {
protected BaseClass(/*put arguments here*/) {
// Code that is common to all child classes
}
}
class ChildClassOne extends BaseClass {
public ChildClassOne(/*put arguments here*/) {
super(arg1, arg2, ...);
// More code here
}
}
As already stated in the comment, one way to call common initialization code would be the use of this(...), i.e. you'd call one constructor from another. The problem, however, is that this call would have to be the first statement of a constructor and thus might not provide what you want.
Alternatively you could call some initialization method (the most common name would be init()) in all constructors and in a place that is appropriate (e.g. at the end of the constructor). There is one problem though: if a subclass would override that method it could create undefined situations where the super constructor calls the method and the method uses non-yet-initialized fields of the subclass. To mitigate that the method should not be overridable, i.e. declare it final or make it private (I'd prefer to have it final though because that's more explicit).
Depending on your needs there's a 3rd option: use the initializer block:
class Super {
{
//this is the initializer block that is called before the corresponding constructors
//are called so it might or might not fit your needs
}
}
Here's an example combining all 3 options:
static class Super {
{
//called before any of the Super constructors
System.out.println( "Super initializer" );
}
private final void init() {
System.out.println( "Super init method" );
}
public Super() {
System.out.println( "Super common constructor" );
}
public Super(String name) {
this(); //needs to be the first statement if used
System.out.println( "Super name constructor" );
init(); //can be called anywhere
}
}
static class Sub extends Super {
{
//called before any of the Sub constructors
System.out.println( "Sub initializer" );
}
private final void init() {
System.out.println( "Sub init method" );
}
public Sub() {
System.out.println( "Sub common constructor" );
}
public Sub(String name) {
super( name ); //needs to be the first statement if used, calls the corrsponding Super constructor
System.out.println( "Sub name constructor" );
init(); //can be called anywhere
}
}
If you now call new Sub("some name"), you'll get the following output:
Super initializer
Super common constructor
Super name constructor
Super init method
Sub initializer
Sub name constructor
Sub init method
You can declare an instance method in the class which can be called from a constructor like this:
Class A{
public A(){
initialize();
}
public void initialize(){
//code goes here
}
}
This concept extends to abstract classes as well.
You could chain your constructors.
public class Test {
public Test() {
// Common initialisations.
}
public Test(String stuff) {
// Call the one ^
this();
// Something else.
}
You can then put your common code in the () constructor.
An alternative is to use an Initializer block.
Class A {
{
// initialize your instance here...
}
// rest of the class...
}
The compiler will make sure the initializer code is called before any of the constructors.
However, I would hesitate a bit to use this use this - perhaps only if there are no other alternatives possible (like putting the code in a base class).
If you can put the common code in one constructor and call it as the first instruction from the other constructors, it is the Java idiomatic way.
If your use case is more complex, you can call a specific method from the constructors provided it is private, final or static (non overidable). An example use case would be:
class Test {
private final void init(int i, String str) {
// do common initialization
}
public Test(int i) {
String str;
// do complex operations to compute str
init(i, str); // this() would not be allowed here, because not 1st statement
}
public Test(int i, String str) {
init(i, str);
}
}
Make a common method and assign it to instance variable. Another way of doing it.
import java.util.List;
public class Test {
int i = commonMethod(1);
Test() {
System.out.println("Inside default constructor");
}
Test(int i) {
System.out.println("Inside argument Constructor ");
}
public int commonMethod(int i) {
System.out.println("Inside commonMethod");
return i;
}
public static void main(String[] args) {
Test test1 = new Test();
Test test2 = new Test(2);
}
}
Say I've made an abstract class called animal, and then I make a subclass called dogs which extends animal. The animal class has a constructor method written inside. If I decide to instantiate a "dog" object, will the animal classes's constructor automatically be called as well?
If someone could clarify, and further expand on this a bit, it'd be greatly appreciated!
Yes , The constructor of an abstract class can be called. why not? Use super() from subclass to call the super class argument constructor(s). If you dont use super(), then by default the no-arg constructor of super class will be called.
If you call
Dog dog = new Dog();
By default the super class default constructor[no-arg constructor] will be called.
If you call
Dog dog = new Dog("doggie1");
Unless you explicitly call super() inside the one argument constructor of Dog, the default super class constructor will not be called.
Try it..
Edit: If you don't call super() inside the one argument constructor of Dog, then also the default super class constructor will be called.
If you want to explicitly call the argument constructor , say one argument constructor of the super class, you have to explicitly make the call super("value");
Animal() {
System.out.println("Animal superconstructor");
}
Edit 02:
Sample program and output
(1)
public class SubClass extends SuperClass {
SubClass(String str) {
super(str);
}
SubClass() {
}
public static void main(String[] args) {
new SubClass("hello");
}
}
abstract class SuperClass {
SuperClass() {
System.out.println("I am SuperClass()");
}
SuperClass(String str) {
System.out.println("I am SuperClass(String str)");
}
}
//output: I am SuperClass(String str)
(2)
public class SubClass extends SuperClass {
SubClass(String str) {
}
SubClass() {
}
public static void main(String[] args) {
new SubClass("hello");
}
}
abstract class SuperClass {
SuperClass() {
System.out.println("I am SuperClass()");
}
SuperClass(String str) {
System.out.println("I am SuperClass(String str)");
}
}
//output: I am SuperClass()
(3)
public class SubClass extends SuperClass {
SubClass(String str) {
}
SubClass() {
}
public static void main(String[] args) {
new SubClass();
}
}
abstract class SuperClass {
SuperClass() {
System.out.println("I am SuperClass()");
}
SuperClass(String str) {
System.out.println("I am SuperClass(String str)");
}
}
// output: I am SuperClass()
If Animal has a no-arg constructor, then Animal's subclasses will call it automatically. Otherwise, the compiler will complain. In such a case, you have to call super() explicitly with the appropriate parameters for the Animal constructor you wish to use.
If I remember correctly, the abstract class's constructor (only the default constructor) is called by default only in the instance of the default constructor. To call the other constructors of the inherited class, a super() call needs to be made inside the constructors of the subclass.