OOPs Through Java Unit-04 Notes
OOPs Through Java Unit-04 Notes
Questions to be discussed:
1. What is class in java? How to declare class in java?
2. What is constructor? Explain different types of constructor in java.
3. What do you mean by constructor and destructor?
4. Differentiate between final and finalize in java.
5. What do you mean by method in java? Why we use method in java program?
6. Discuss about method overloading and constructor overloading in java.
7. What do you mean by recursive method? Explain it with example.
8. Write short notes on:
a. This keyword
b. Static keyword
c. Garbage collection
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)
2 CTH EDUCATION
What is an object in Java?
An object is a real-world entity.
An entity that has state and behavior is known as an object.
Example: chair, bike, marker, pen, table, car, etc.
An object is an instance of a class.
A class is a template or blueprint from which objects are created.
So, an object is the instance (result) of a class.
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)
3 CTH EDUCATION
Difference between object and class:
Object Class
Object is a real world entity such as pen, laptop, Class is a group of similar objects.
mobile, bed, mouse, chair etc.
Object is created through new keyword. Class is declared using class keyword.
Object allocates memory when it is created. Class doesn't allocated memory when it is created.
Constructor in java:
Constructor are the special member function.
It is used to initialize the data members of the class.
Constructor has the same name as the class name.
A constructor has no return type not even void.
Constructors are automatically invoked (call) as soon as the object of its class is created.
It is called constructor because it constructs the values at the time of object creation.
It is not necessary to write a constructor for a class, because java compiler creates a default constructor
if your class doesn't have any.
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)
4 CTH EDUCATION
Types of constructor:
There are two types of constructors in Java:
1. Default constructor, and
2. Parameterized constructor.
Default Constructor:
A constructor having no arguments is called "Default Constructor.
The default constructor is used to provide the default values to the object like 0, null, etc.
Parameterized Constructor:
A constructor having arguments is called a parameterized constructor.
The parameterized constructor is used to provide different values to distinct objects.
We can have any number of parameters in the constructor.
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)
5 CTH EDUCATION
What is Destructor?
A destructor is a member function of a class that deallocates the memory allocated to an object.
A destructor is also declared and defined with the same name as that of the class.
A destructor is preceded by a tilde (~) symbol.
A single class has only a single destructor.
CONSTRUCTOR DESTRUCTOR
There can be multiple constructors in a class. There is always a single destructor in the class.
This keyword:
It is a keyword that refers to the current object in a method or constructor.
If the name of instance variable and local variable are same then at the runtime JVM gets confused.
To avoid this type of problem “this” keyword is used.
It is used to call default constructor of its own class.
It is used to called parametrized constructor of its own class.
“this” can be passed as argument in the constructor call.
“this” can be used to return the current class instance from the method.
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)
6 CTH EDUCATION
Static keyword:
It is a keyword which is used to make a variable or method constant.
If you declare any variable as static, it is known as a static variable.
The static keyword is used for efficient memory management.
The static variable gets memory only once in the class area at the time of class loading.
We can apply static keyword with variables, methods, blocks and nested classes.
Final Keyword:
The final keyword in java is used to restrict the user.
Using the final keyword means that the value can’t be modified in future.
The final keyword can be used with class method and variable.
A final class cannot be inherited, a final method cannot be overridden and a final variable cannot be
reassigned.
final is the keyword which is used finally is the block in Java finalize is the method in Java
to apply restrictions on a class, Exception Handling to execute which is used to perform clean up
method or variable. the important code whether the processing just before object is
exception occurs or not. garbage collected.
Final keyword is used with the Finally block is always related to finalize() method is used with the
classes, methods and variables. the try and catch block in objects.
exception handling.
Once declared, final variable then finally block runs the important Finalize method performs the
it cannot be modified, overridden code even if exception occurs or cleaning activities with respect to
& inherited. not & cleans up all the resources the object before its destruction.
used in try block
Final method is executed only Finally block is executed as soon finalize method is executed just
when we call it. as the try-catch block is before the object is destroyed.
executed.
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)
7 CTH EDUCATION
What are Methods in Java?
A method in Java is a block of code that performs specific actions mentioned in it.
Method runs only when it is called.
You can insert values or parameters into methods, and they will only be executed when called.
They are also referred to as functions.
Return Type:
It defines the return type of the method.
In the above syntax, “int” is the return type.
We can mention void as the return type if the method returns no value.
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)
8 CTH EDUCATION
Recursion in Java:
The process in which a method calls itself directly or indirectly is called recursion.
A method in java that calls itself is called recursive method.
It makes the code compact but complex to understand.
Using recursive algorithm, certain problems can be solved quite easily.
Syntax:
return_type method_name( )
{
//code to be executed
method_name();//calling same method
}
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)
9 CTH EDUCATION
Garbage collection:
When Java programs run on the JVM, objects are created on the heap, which is a portion of memory
dedicated to the program.
Eventually, some objects will no longer be needed.
The garbage collector finds these unused objects and deletes them to free up memory.
It is the process by which Java programs perform automatic memory management.
In other words, it is a way to destroy the unused objects.
Diploma : CSE (All Paper) By : Alok Sir (Mob. No.: +91-80 84 370 470)