Week 3-LectureA OOPs
Week 3-LectureA OOPs
overloading, constructor
chaining, and copy
constructor
(Java)
Contents
2
• Default constructor
• No-argument constructor
• Parametrized constructor
Function overloading
4
• Method Overloading is a
feature that allows a class to
have more than one method
having the same name, if
their argument lists are
different.
• It is similar to constructor
overloading in Java, that
allows a class to have more
than one constructor having
different argument lists.
Constructor overloading
5
• There are two ways by which we can use constructor chaining in Java. These ways
depend on whether we are using it in the same class or the different class.
• It creates a new object by initializing the object with the instance of the same
class.
• The Java Copy Constructor provides a copy of the specified object by taking the
argument as the existing object of the same class.
• Java does not implicitly provide the facility of a Copy constructor as in C language.
• But, we can define it by copying the values of one object to another object.
Creating a Copy Constructor in Java
11
• After declaring a copy constructor, we need to copy each field of the input object
of the class into the new object. For example:
package Xyz;
public class Student
{
private int roll;
private String name;
//constructor to initialize roll number and name of the student
Student(int rollNo, String sName)
{
//method to return roll number
roll = rollNo;
int printRoll()
name = sName;
{
}
return roll;
//copy constructor
}
Student(Student student)
//Method to return name of the student
{
String printName()
System.out.println("\n---Copy Constructor Invoked---");
{
roll = student.roll;
return name;
name = student.name;
}
}
}
Copy Constructor example
14
//class to create student object and print roll number and name of the student
package Xyz;
public class Xyz
{
public static void main(String[] args)
{
Student student1 = new Student(101, “ali");
System.out.println("Roll number of the first student: "+ student1.printRoll());
System.out.println("Name of the first student: "+ student1.printName());
• From the previous code, we created a shallow copy of the object. It will work fine
as we are using the immutable and primitive types, i.e int and String.
• Constructor overloading
• Constructor chaining
• Copy constructor
Static data
19
• The static variable can be used to refer to the common property of all objects, for
example, the company name of employees, college name of students, etc.
• The static variable gets memory only once in the class area at the time of class
loading.
class Student{
int regno;
String name;
String uni=“CUI";
}
Suppose there are 500 students in my university, now all instance data members will get
memory each time when the object is created.
All students have its unique regno and name, so instance data member is good in such case.
Here, “uni" refers to the common property of all objects.
If we make it static, this field will get the memory only once.
• If you apply static keyword with any method, it is known as static method.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an instance of a
class.
• A static method can access static data member and can change the value of it.
Static method
23
There are two main restrictions for the static method. They are:
• The static method can not use non static data member or call non-static method
directly.
• this and super cannot be used in static context.
class A
{
int a=40;//non static
class A2
{
static
{
System.out.println("static block is invoked");
}
public static void main(String args[])
{
Output: System.out.println("Hello main");
static block is invoked }
Hello main }
Inner classes
26
• The purpose of nested classes is to group classes that belong together, which
makes your code more readable and maintainable.
• To access the inner class, create an object of the outer class, and then create an
object of the inner class:
Using reference of outer object you are creating object for Iner class, because Iner is
part of Outer class.
Inner classes example
27
class OuterClass
{
int x = 10;
class InnerClass
{
int y = 5;
}
}
public class MyMainClass
{
public static void main(String[] args)
{
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
} // Outputs 15 (5 + 10)
}
Private inner classes and example
28
• If you don't want outside objects to access the inner class, declare the class as
private class OuterClass
{
int x = 10;
private class InnerClass
{int y = 5;}
}
public class MyMainClass
{
public static void main(String[] args)
{
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
} MyMainClass.java:12: error:
Static inner/nested class
29
• A static class i.e. created inside a class is called static nested class in java.
• An inner class can also be static, which means that you can access it without
creating an object of the outer class:
• Static nested class cannot access non-static (instance) data member or method.
Static inner/nested class example
30
class TestOuter1
{
static int data=30; A static inner class is a nested class
which is a static member of the outer
static class Inner
class.
{
void msg() It can be accessed without instantiating
the outer class, using other static
{System.out.println("data is "+data);}
members.
}
public static void main(String args[]) Just like static members, a static nested
class does not have access to the
{
instance variables and methods of the
TestOuter1.Inner obj=new TestOuter1.Inner(); outer class.
obj.msg();
}
}
Static nested class example
31
class TestOuter1
{
static int data=30;
In this example, you need to
static class Inner create the instance of static nested
{ class because it has instance
void msg() method msg().
class Calculation
{
void fact(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("factorial is "+fact);
}
public static void main(String args[])
{
new Calculation().fact(5);//calling method with anonymous object
}
}
34
THANK YOU