163 subscribers
3.42K photos
24 videos
39 files
371 links
Link: @java_posts

Contact: java.response.email@gmail.com
Download Telegram
Difference between constructor and method in Java
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.
// 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();
}
}
Output
Copying values without constructor
We can copy the values of one object into another by assigning the object values to another object. In this case, there is no need to create the constructor.
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();
}
}
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. .
// 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();
}
}
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.
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();
}
}
output
2) Use of super keyword to invoke constructor of parent class
When we create the object of sub class, the new keyword invokes the constructor of child class, which implicitly invokes the constructor of parent class. So the order to execution when we create the object of child class is: parent class constructor is executed first and then the child class constructor is executed. It happens because compiler itself adds super()(this invokes the no-arg constructor of parent class) as the first statement in the constructor of child class.
class Parentclass
{
Parentclass() {
System.out.println("Constructor of parent class");
}
}
class Subclass extends Parentclass
{
Subclass() {
/* Compile implicitly adds super() here as the
* first statement of this constructor
*/
System.out.println("constructor of child class");
}
Subclass(int num) {
/* Even though it is a parameterized constructor
* The compiler still adds the no-arg super() here
* /
System.out.println("arg constructor of child class");
}
void display() {
System.out.println("Hello!");
}
public static void main(String args[]) {
/* Creating object using default constructor. This
* will invoke child class constructor, which will
* invoke parent class constructor
*/
Subclass obj = new Subclass();
// Calling sub class method
obj.display();
/* Creating second object using arg constructor
* it will invoke arg constructor is child class which will
* invoke no-arg constructor of parent class automatically
*/
Subclass obj2 = new Subclass(10);
obj2.dislpay();
}
}
Output
Parameterized super() call to invoke parameterized constructor of parent class
We can call super() explicitly in the constructor of child class, but it would not make any sense because it would be redundant. It’s like explicitly doing something which would be implicitly done otherwise.
However when we have a constructor in parent class that takes arguments then we can use parameterized super, like super(100); to invoke parameterized constructor of parent class from the constructor of child class.
Let’s see an example to understand this:
class Parentclass
{
// no-arg constructor
Parentclass() {
System.out.println("no-arg constructor of parent class");
}
// arg or parameterized constructor
Parentclass(String str) {
System.out.println("parameterized constructor of parent class");
}
}
class Subclass extends Parentclass
{
Subclass() {
/* super() must be added to the first statement of constructor
* otherwise you will get a compilation error. another important
* point to note is that when we explicitly use super in constructor
* the compiler doesn't invoke the parent constructor automatically
*/
super("hahahaha");
System.out.println("Constructor of child class");

}

void display() {
System.out.println("Hello");
}
public static void main(String args[]) {
Subclass obj = new Subclass();
obj.display();
}
}
Output
There are few important points to note in this example:
1) super()(or parameterized super must be the first statement in constructor otherwise you will get the compilation error: “Constructor call must be the first statement in a constructor”
2) When we explicitly placed super in the constructor, the java compiler didn’t call the default no-arg constructor of parent class.
3) How to use super keyword in case of method overriding
When a child class declares a same method which is already present in the parent class then this is called method overriding. We will learn method overriding in the next tutorials of this series. For now you just need to remember this: When a child class overrides a method of parent class, then the call to the method from child class object always call the child class version of the method. However by using super keyword like this: super.method_name you can call the method of parent class (the method which is overridden). In case of method overriding, these terminologies are used: Overridden method: The method of parent class Overriding method: The method of child class Lets take an example to understand this concept:
class Parentclass
{
// overriding method
void display() {
System.out.println("Child class method");
}
}
class Subclass extends Parentclass
{
// overriding method
void display() {
System.out.println("Child class method");
}
void printMsg() {
// this would call Overriding method
display();
// this would call overridden method
super.display();
}
public static void main(String args[]) {
Subclass obj = new Subclass();
obj.printMsg();
}
}