Write a parameterized constructor with String as a parameter in java pls pls give answer
Answers
Answer:
Java is one of the many programming languages which follows Object Oriented Approach. That means while programming in Java we have all the powerful features of Data Abstraction, Polymorphism, Inheritance etc. The core of all the OOP features is the implementation of Classes and Objects and their interaction with one another. In this article we will particularly look at how to initialize an object using parameterized constructors in Java. Please note that a basic understanding of classes and objects is required before you continue to constructors.
What Is A Constructor?
Default Constructor vs Parameterized Constructor
Passing Objects As Arguments
Calling Default Constructor From A Parameterized Constructor
Constructor Overloading
What Is A Constructor?
A constructor is basically a method that is automatically called when an object(instance) is created of that class. It is used to initialize an object’s data members.
1
2
3
public class(){ System.out.println("constructor is made");}
}
Some features of constructor include:
It has the same name as the class name
It has no return type
Types Of Constructor
Default Constructor
Parameterized Constructor
Default Constructor vs Parameterized Constructor
Default Constructor – A constructor that accepts no parameter is called Default Constructor. It is not necessary to have a constructor block in your class definition. If you don’t explicitly write a constructor, the compiler automatically inserts one for you.
Example illustrating Default Constructor in Java:
1
2
3
4
5
6
7
public class
()
{ System.out.println("I am a constructor");}
public static void main(String args[]){
obj = new ();
}
}
Output: I am a constructor
Parameterized Constructor – A constructor is called Parameterized Constructor when it accepts a specific number of parameters. To initialize data members of a class with distinct values.
Explanation: