Let’s construct stuff w/ Java

This constructor may have some arguments…

1. Constructors are code blocks that run every time a class is instantiated by using new keyword.

2. No argument constructor is called default constructor.

3. Every class has to have at least one constructor. If no constructor is given in the class definition, compiler will define default constructor for you automatically. If any parametrized constructor is provided, compiler doesn’t declare the default contractor automatically. So it throws a compilation error when you try to use it.

4. Whenever a child class constructor gets invoked, it implicitly invokes the the constructor of parent class unless you explicitly invoke a constructor of the parent class. It’s almost like the compiler puts super(); in the child class constructor.

5. Constructors are not methods. They have no return types.

6. Interfaces don’t have constructors.

7. Abstract classes can have constructors. (But they are generally made protected. Because making them public has no use, since they cannot be called directly, anyway. So you should use the most restricted scope possible next to public which is protected. )

For more info:

  1. http://stackoverflow.com/questions/260666/can-an-abstract-class-have-a-constructor
  2. http://stackoverflow.com/questions/260744/abstract-class-constructor-access-modifier


public class SuperClassWithNoDefaultConstructor {

public SuperClassWithNoDefaultConstructor(int a) {


public class ConstructorsAreFun extends SuperClassWithNoDefaultConstructor {	int number;
String myName;

//This one will give no errors.
//Because parameterized constructor is called explicitly.
//So default constructor of super
//will not be added implicitly.
public ConstructorsAreFun() {
number = 15;
myName = "Default!";
System.out.println("Default is called!");

//This one will give the error:
//Implicit super constructor
//SuperClassWithNoDefaultConstructor() is undefined.
//Must explicitly invoke another constructor
public ConstructorsAreFun(int number, String name) {
//implicit super();
this.number = number;
this.myName = name;
System.out.println("My name is " + this.myName);
public static void main(String[] args) { ConstructorsAreFun fun = new ConstructorsAreFun(2, "fun");

//Since a parameterized constructor is defined,
//a default constructor will not be added to
//the class definition by compiler.
//So this one will give an error as following:
//The constructor SuperClassWithNoDefaultConstructor() is
SuperClassWithNoDefaultConstructor s
= new SuperClassWithNoDefaultConstructor();




a geek who loves to understand the reasons behind things... and colors... Colors are cool.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reader, Writer, and State Monads in TypeScript with fp-ts

Building Hybrid applications using PhoneGap + Framework7

Iterative Programming with Refactoring

Find the vowels in a given string

State management with city-state

Apps That Work Natively on the Web and Mobile

JavaScript : Hosing Explained Well…

Technologies I chose (React, NestJS, Stripe) to build a E-commerce Web App

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mehmet Akcay

Mehmet Akcay

a geek who loves to understand the reasons behind things... and colors... Colors are cool.

More from Medium

Java 11 : newbie

Abstraction in Object-Oriented Programming- Explained using Java

Abstract Classes and Interfaces in Java

Arithmetic Exception in Java