Java Parameterized Constructor
A constructor which has a specific number of parameters is called a parameterized constructor.
Why use the parameterized constructor ?
The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
A constructor which has a specific number of parameters is called a parameterized constructor.
Why use the parameterized constructor ?
The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
// java program to demonstrate the use of the parameterized constructor
class Student4 {
int id;
String name;
// creating a parameterized constructor
Student4(int i, String n) {
id = i;
name = n;
}
// method to display the values
void display(){System.out.println(id+" " + name);}
public static void main(String args[]) {
// creating objects and passing values
Student4 s1= new Student4(111, "Karan");
Student4 s2 = new Student4(222, "Aryan");
// calling method to display the values of object
s1.display();
s2.display();
}
}
class Student4 {
int id;
String name;
// creating a parameterized constructor
Student4(int i, String n) {
id = i;
name = n;
}
// method to display the values
void display(){System.out.println(id+" " + name);}
public static void main(String args[]) {
// creating objects and passing values
Student4 s1= new Student4(111, "Karan");
Student4 s2 = new Student4(222, "Aryan");
// calling method to display the values of object
s1.display();
s2.display();
}
}
Constructor Overloading in Java
In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.
In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.
// java program to overload constructor
class Student5{
int id;
String name;
int age;
// creating two arg constructor
Student5(int i, String n) {
id = i;
name = n;
}
// creating three arg constructor
Student5(int i, String n) {
id = i;
name = n;
}
// creating three arg constructor
Student5(int i, String n, int a) {
id = i;
name = n;
age = a;
}
void display() {System.out.println(id + " " + name + " " age);}
class Student5{
int id;
String name;
int age;
// creating two arg constructor
Student5(int i, String n) {
id = i;
name = n;
}
// creating three arg constructor
Student5(int i, String n) {
id = i;
name = n;
}
// creating three arg constructor
Student5(int i, String n, int a) {
id = i;
name = n;
age = a;
}
void display() {System.out.println(id + " " + name + " " age);}
Java Copy Constructor
There is no copy constructor in Java. However, we can copy the values from one object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in Java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
In this example, we are going to copy the values of one object into another using Java constructor.
There is no copy constructor in Java. However, we can copy the values from one object to another like copy constructor in C++.
There are many ways to copy the values of one object into another in Java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
In this example, we are going to copy the values of one object into another using Java constructor.
// Java program to initialize the values from one object to another object
class Student6 {
int id;
String name;
// constructor to initialize integer and string
Student6(int i, String n) {
id = i;
name = n;
}
// constructor to initialize another object
Student6(Student6 s) {
id = s.id;
name = s.name;
}
void display() {System.out.println(id + " " + name);}
public static void main(String args[]) {
Student6 s1 = new Student6(111, "Karan");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
class Student6 {
int id;
String name;
// constructor to initialize integer and string
Student6(int i, String n) {
id = i;
name = n;
}
// constructor to initialize another object
Student6(Student6 s) {
id = s.id;
name = s.name;
}
void display() {System.out.println(id + " " + name);}
public static void main(String args[]) {
Student6 s1 = new Student6(111, "Karan");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
class Student7 {
int id;
String name;
Student7(int i, String n) {
id = i;
name = n;
}
Student7(){}
void display() {System.out.println(id + " " + name);}
public static void main(String args[]) {
Student7 s1 = new Student7(111, "Karan");
Student7 s2 = new Student7();
s2.id=s1.id;
s2.name=s1name;
s1.display();
s2.display();
}
}
int id;
String name;
Student7(int i, String n) {
id = i;
name = n;
}
Student7(){}
void display() {System.out.println(id + " " + name);}
public static void main(String args[]) {
Student7 s1 = new Student7(111, "Karan");
Student7 s2 = new Student7();
s2.id=s1.id;
s2.name=s1name;
s1.display();
s2.display();
}
}
Java
Chapter 5: Object-Oriented Programming 1. Why OOP ? 2. Java Constructor 3. Accessing Parent Class Variables 4. The Java OOP Concepts 5. Abstraction 6. Encapsulation 7. Polymorphism 8. Inheritance 9. Association 10. Aggregation 11. Composition 12.…
How to use super keyword to access the variables of parent class
When you have a variable in child class which is already present in the parent class then in order to access the variable of parent class, you need to use the super keyword.
Lets take an example to understand this: In the following program, we have a data member num declared in the child class, the member with the same name is already present in the parent class. There is no way you can access the num variable of parent class without using super keyword. .
When you have a variable in child class which is already present in the parent class then in order to access the variable of parent class, you need to use the super keyword.
Lets take an example to understand this: In the following program, we have a data member num declared in the child class, the member with the same name is already present in the parent class. There is no way you can access the num variable of parent class without using super keyword. .
// parent class or superclass or base class
class Superclass
{
int num = 100;
}
// child class or subclass or derived class
class Subclass extends Superclass
{
/* The same variable num is declared in the subclass
* which is already present in the superclass
*/
int num = 110;
void printNumber() {
System.out.println(num);
}
public static void main(String args[]) {
Subclass obj = new Subclass();
obj.printNumber();
}
}
class Superclass
{
int num = 100;
}
// child class or subclass or derived class
class Subclass extends Superclass
{
/* The same variable num is declared in the subclass
* which is already present in the superclass
*/
int num = 110;
void printNumber() {
System.out.println(num);
}
public static void main(String args[]) {
Subclass obj = new Subclass();
obj.printNumber();
}
}
output
Accessing the num variable of parent class:
By calling a variable like this, we can access the variable of parent class if both the classes (parent and child) have same variable.
super.variable_name
Let’s take the same example that we have seen above, this time in print statement we are passing super.num instead of num.
Accessing the num variable of parent class:
By calling a variable like this, we can access the variable of parent class if both the classes (parent and child) have same variable.
super.variable_name
Let’s take the same example that we have seen above, this time in print statement we are passing super.num instead of num.
class Superclass
{
int num = 100;
}
class Subclass extends Superclass
{
int num = 110;
void printNumber() {
/* Note that instead of writing num we are
* writing super.num in the print statement
* this refers to the num variable of superclass
*/
System.out.println(super.num);
}
public static void main(String args[]) {
Subclass obj = new Subclass();
obj.printNumber();
}
}
{
int num = 100;
}
class Subclass extends Superclass
{
int num = 110;
void printNumber() {
/* Note that instead of writing num we are
* writing super.num in the print statement
* this refers to the num variable of superclass
*/
System.out.println(super.num);
}
public static void main(String args[]) {
Subclass obj = new Subclass();
obj.printNumber();
}
}