Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Bharat's blog

Bharat's picture

How to call super class constructor from subclass constructor?

3.5
Your rating: None Average: 3.5 (2 votes)
Constructor of the super class can be called using super.

 
Bharat's picture

How to call a constructor from another constructor within the class?

0
Your rating: None
Different constructor of the same class can be called using “this”. e.g.
  1. public class Q1 {
  2.  Q1(){
  3.   System.out.println("Default Constructor");
  4.  }
  5.  
  6.  Q1(String msg){
  7.   this(); //calls default constructor
  8.   System.out.println("One Argument Constructor");
  9.  }
  10. }
Bharat's picture

What happens if you create one argument constructor and do not create constructor without argument?

5
Your rating: None Average: 5 (1 vote)
If class does not define any constructors, Java automatically creates default constructor with no arguments.  However, if constructor with arguments is specified, Java does not create default one.
 
Nothing happens if we do not specify the default constructor. However,
Bharat's picture

What is difference between method and constructor?

4
Your rating: None Average: 4 (6 votes)
Constructor is a special member function of a class which is used to create object of that class. Constructor has a same name as that of the class. It also does not have any return type. However, constructor may have arguments.
 
Syndicate content