What Are Constructor Methods? How to use Constructor in Java?
------------------------------------------------------------------
Constructors from a conceptual level enable the creation of an object of a given class. Constructor is a method of a class with no return parameters. Constructors also enable the initialization of any variables of a class. You can supply input parameters to constructors.
A class can have more than one constructor. It has the same name as the class itself. A default constructor doesn't have any parameters. In case of inheritance - if your subclass constructor does not explicitly call the superclass constructor, java will call the super class default constructor. If you want subclass constructor to call a specific superclass constructor, use the
super keyword.
Example:java code
Class Book
{
String title;
String publisher;
float price;
public Book()
{
}
public Book(String title)
{
this.title = title;
}
}
Class TextBook extends Book
{
public TextBook(){
}
public TextBook(String title)
{
super(title);
}