Skip to main content

Types of constructor in java

 There are 3 types of constructor in java

        

Default constructor 

Parameterized constructor 

Parameterless constructor

 Default constructor 

  If we do not define any constructor then compiler generates one constructor for class is called          default constructor. 

 Compiler generated default constructor is parameterless. 

 Compiler never generate default parameterized constructor, if we want to define instance of a class by passing arguments then we must define parameterized constructor inside class

Parameterized Constructor

If we define constructor of a class having one or more parameter then it is called parameterized constructor.

  When we create instance of a class by passing arguments then matching parameterized constructor gets called.

  Example:

 public Employee(int empId,String empName,String companyName)

}

 Employee employee=new Employee(123,”bob”,”Vodafone”)

ParameterlessConstructor

If we define constructor of a class without any parameter then it is called parameterless constructor or zero argument constructor. 

 When we create instance of a class without passing any argument then parameterless constructor of a class get called. 

 It is also called as zero argument constructor or user defined default constructor. 

 Example:

 public Employee(){ } 

Employee employee=new Employee()

constructorchaining

Comments

Popular posts from this blog

equals method of object class in java

  In Java, the equals method is a fundamental method inherited by all classes from the Object class.           Here’s an explanation of the equals method: Purpose: The equals method is used to compare two objects to determine if they are logically equivalent. By default, the implementation in the Object class checks if the two object references point to the same memory location (i.e., if they are the same instance). Signature : public boolean equals(Object obj) Explanation: Method Override: Classes can override the equals method to provide their own definition of object equality based on their specific needs. When overriding, the method should adhere to certain principles to ensure consistency and correctness of equality comparisons. Default Behavior (from Object class): The default implementation in the Object class checks if the two object references ( this and obj ) refer to the exact same object in memory using the == operator: public boo...

What is real use of interface: mostly asked interview question

Actually in real project development interfaces are use to write business logic code . Also Interfaces in java are powerful tools that allow you to define a contract for what a class can do, without specifying how it does it. it means we can give method declaration in interface and what that method does actually that responsibility is given to the class which is going to implement that class. They are used for several real-world purposes like enabling polymorphism , creating loosely coupled systems , and defining common behaviors across different classes . Real-World Use of Interfaces Let's look at some practical scenarios where interfaces are commonly used: 1. Multiple Inheritance (via Interfaces):  Java doesn't support multiple inheritance with classes, but it allows multiple inheritance with interfaces. This allows a class to implement multiple interfaces, enabling it to inherit behaviors from more than one source. 2. Polymorphism: Interfaces allow you to treat different o...

Roadmap to become a fullstack developer

 Becoming a Full Stack Developer means gaining proficiency in both frontend and backend development, as well as understanding the tools and practices that tie them together. Below is a roadmap to guide your journey toward becoming a full-stack developer. This roadmap covers the key areas, from foundational knowledge to advanced topics. Roadmap to Becoming a Full Stack Developer 1. Learn the Basics of Web Development 1.1 HTML (Hypertext Markup Language) Learn the basic structure of web pages. Understand the role of elements like <div>, <span>, <a>, <img>, etc. Learn about semantic HTML (<header>, <footer>, <article>, etc.). 1.2 CSS (Cascading Style Sheets) Learn how to style web pages (colors, fonts, spacing, etc.). Master CSS layout techniques like Flexbox and Grid. Understand the importance of Responsive Design (media queries, mobile-first approach). 1.3 JavaScript (JS) Learn the fundamentals: variables, loops, conditionals, functions. Und...