JavaM1 P3
JavaM1 P3
Module-1 Part3
1
Constructor
• A constructor automatically initializes an object immediately upon creation.
• No return type not even void. Implicit return type of a class constructor is class itself.
• If we do not define a constructor for a class, Java creates a default constructor for the class.
This constructor initializes all instance variables to zero. If we define a constructor then
default constructor is no longer used.
Default
Parameterized
Copy
Do nothing: can not contain definition. Used for creating objects without
initialization.
Constructor
• we can have private, protected, public or default constructor in Java.
• Constructor is automatically called immediately after the object is created before the
new operator completes.
• Java provides a Constructor class which can be used to get the internal information of
a constructor in the class. It is found in the java.lang.reflect package.
Constructor Overloading
class Box double volume() vol = mybox2.volume();
{ { System.out.println(" Volume of
double width, height, depth; return width * height * depth; mybox2 is " + vol);
}
Box(double w, double h, double d) vol = mycube.volume();
}
{ System.out.println(" Volume of
width = w; height = h; depth = d; public class Test mycube is " + vol);
{ }
}
public static void main(String args[]) }
Box() {
{ Box mybox1 = new Box(10, 20, 15);
width = height = depth = 0; Box mybox2 = new Box(); O/P:
Box mycube = new Box(7); Volume of mybox1 is 3000.0
}
Box(double len) double vol; Volume of mybox2 is 0.0
{
width = height = depth = len; vol = mybox1.volume(); Volume of mycube is 343.0
System.out.println(" Volume of
}
mybox1 is " + vol);
Copy Constructor
class Student{
int id;
String name;
Student(int i,String n){
id = i;
name = n;
}
Student(Student s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+" "+name);}
• In subclass we can call constructor of parent class using super()(as a first statement),
but not vice versa.
8
class Main public void display()
{ {
int age; System.out.println("Name="+name+"\nage="+age);
String name; }
Main() public static void main(String args[])
{ {
this("Constructor Example"); Main obj = new Main();
} obj.display();
}
Main(String s) }
{
this(s,6);
}
Main(String s1,int a)
{
this.name=s1;
age=a;
}
9
Access Modifier
1.private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
2.default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
3.protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be accessed
from outside the package.
4.public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
Access within class within outside outside
Modifier package package by package
subclass only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Garbage Collection
• Objects are dynamically allocated in Java using new operator. In C++, delete operator is
used when object is created dynamically but in Java, different approach(garbage
collection) is used to handle this.
• When no reference to an object exist, that object is assumed to be no longer needed and
the memory occupied by that object can be reclaimed. There is no explicit need to
destroy the objects like C++.
• Garbage collector runs periodically , checking for objects that are no longer referred. It is
low priority thread.
• In C++, destructor is used, which is called when an object goes out of scope. Java
does not support destructor. The finalizer() method only approximates the need
of destructor.
• In Java, objects are not explicitly destroyed when they go out of scope. Object is marked
as unused when there are no longer any references pointing to it.
• finalize() method will not be called until the garbage collector runs. Even if you
execute a call to gc( ) (the garbage collector), there is no guarantee that finalize( ) will
immediately be executed.
this keyword
• this is a reserved keyword in java.
• this is used to refer current-class’s instance as well as static members.
class Main
public static void main(String[] args) If b is static then
{
{ O/P: 40
int a = 10; 30
Main a=new Main(40,60);
int b = 20; //can be static also 30
Main b=new Main(30,30); 30
Main(int a, int b)
a.show();
{
b.show();
this.a=a;
}
this.b=b;
}
}
void show() O/P: 40
{ 60
System.out.println(a); 30
30
System.out.println(b);
}
Super keyword
• super is a reserved keyword in java.
• super can be used to refer immediate parent class instance variable.
• super can be used to invoke immediate parent class method.
• super( ) can be used to invoke immediate parent class constructor.
class A class Test
{ {
int a=10; public static void main(String args[])
} {
class B extends A B obj=new B();
{ obj.print();
int a=20; }
void print () }
{
System.out.println(a); O/P: 20
System.out.println(super.a); 10
}
}
Super Keyword
The super keyword can also be used to invoke parent class method. It should be
used if subclass contains the same method as parent class(if method is
class A
overridden) super.show();
{
}
void show() }
{ class Main
System.out.println("A Class"); {
public static void main(String args[])
} {
} B obj=new B();
class B extends A{ obj.show();
obj.ashow();
void show()
{ }}
System.out.println("B Class");
O/P: B Class
} A class
void ashow()
Super Keyword
}}
static method
• It can access only static data members and will execute without any object.
• It will be invoked before any creating any object and can also be invoked by the class name.
}
static
statement
• Static statements are executed before creating any object.
• Used to initialize static data members like constructers helps to initialize instance members.
class A
{ class Main
static int a=10; {
static public static void main(String args[])
{ {
System.out.println(a); A obj=new A();
} obj.fun();
void fun() }
{ }
System.out.println("Non static function"); O/P: 10
} Non static function
}
Static class
• Java allows a class to be defined within another class. These are called Nested Classes.
• The class in which the nested class is defined is known as the Outer Class. Unlike top-
level classes, Inner classes can be Static. Non-static nested classes are also known
as Inner classes.
• We can declare a class static by using the static keyword. A class can be declared static
only if it is a nested class. It does not require any reference of the outer class. The
property of the static class is that it does not allows us to access the non-static members
of the outer class.
Inner class
• The classes that are non-static and nested are called inner classes.
• We cannot create an instance of the inner class without creating an instance of the outer class.
Without using the reference to the outer class instance, an instance of the inner class can
access the members of its outer class. It makes the program simple and concise.
Outer Class
• The class in which nested class is defined is called outer class.
Nested Class
• Java allows us to define a class within a class that is known as a nested class.
• It may be static or non-static. The major difference between static and non-static class is that:
• An instance of the static nested class can be created without creating an instance of its outer
class.
• The static and non-static members of an outer class can be accessed by an inner class.
.
• All static classes are nested classes but vice-versa is not true.
• Static class can access only static members of the outer class.
• Non-static variable and instance methods cannot be accessed within the static class.
If you try to access a non-static reference from a static field, it throws an
error: Cannot make a static reference to the non-static field.
• We can create static blocks, variables, and methods inside a static class.
• Used as superclass.
• Class having abstract method can not have its definition. Definition of abstract method is given in subclass.
abstract type name(parameter-list)
{
}
Abstract Class and Abstract Method
abstract class Base public class Main
{ {
public static void main(String args[ ])
abstract void display();
{
} Derived obj1 = new Derived();
class Derived extends Base obj1.display();
{ }
}
void display()
{
System. out. println(“abstract method called");
}
}
Final Keyword
• final keyword is used to define final variable, method or class.
• final variable is a variable whose value can not be changed during the execution of
program.
• A final variable is called blank final variable, if it is not initialized while declaration.
31
Final Keyword
• final method is a method which can not be redefine is subclass. It is used to restrict
the method overriding.
• const variables in C++ must be assigned a value when declared. For final variables in
Java, it is not necessary. A final variable can be assigned value later, but only once.
class A final class A{
class A
{ void show( )//final method
{ final void show( )//final method {
final int a=90;//final variable { System. out. println ("show method A");
void show() System. out. println ("show method A"); } }//final class
{ }
} class B extends A
a=400;
class B extends A{ {
} void run()
void show( ){
public static void main(String args[]) System.out.println("show method B"); {
System. out. println(“final class example”)
{ } }
A obj=new A(); public static void main(String args[])
obj. run(); { public static void main(String args[])
B obj= new B(); {
} B obj= new B();
obj.show();
} obj.run();
}}
}
Compile time error } Compile time error
Compile time error