KEMBAR78
Unit 05 Objects and Classes | PDF | Class (Computer Programming) | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
3 views5 pages

Unit 05 Objects and Classes

The document provides an overview of object-oriented concepts in Core Java, focusing on classes and objects. It explains the structure of classes, the creation and manipulation of objects, access control modifiers, argument passing methods, constructors, the 'this' keyword, and the finalize() method for garbage collection. Key concepts include predefined and user-defined classes, method declaration, and the distinction between call by value and call by reference for argument passing.

Uploaded by

rinkubaria465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Unit 05 Objects and Classes

The document provides an overview of object-oriented concepts in Core Java, focusing on classes and objects. It explains the structure of classes, the creation and manipulation of objects, access control modifiers, argument passing methods, constructors, the 'this' keyword, and the finalize() method for garbage collection. Key concepts include predefined and user-defined classes, method declaration, and the distinction between call by value and call by reference for argument passing.

Uploaded by

rinkubaria465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

OBJECT ORIENTED CONCEPTS & PROGRAMMING–1 (CORE JAVA)

Unit 05: OBJECTS AND CLASSES


❖ Class :-
• Class is a collection of objects and it doesn't take any space on memory, class is also
called as blueprint/logical entity.
• Two types of Class
1. Pre defined class
o Scanner
o Console
o System
o string
2. User defined class
o Test
o Student
o demo
• A class can be defined as a entity in which data and functions are put together.
• A class can be declared by using the keyword class. The general form of class is,

class classname
{
Type variable;
Type method(parameter-list)
{
……
……
}
}
• Data field Declaration
o The data lies within the class and the data fields are accessed by the methods
of that class.
o For example:
Class Test
{
Int a;
Int b;
}
• Method field declaration
o In object oriented programming any two objects communicate with each
other using methods.
o All the methods have the same general form as the method main().Most of
the methods are specified as either static or public.
o Java classes do not need the method main, but if you want particular class as
a starting point for your program then the main method is included in that
class.
o The general form of method is-
Type method(parameter-list)
{
……
……
}
❖ Object
• Object are nothing but instance of the class that executes the class.
• Once the object is created it takes up space like other variable in memory.
• For creating objects in java the operator new is used.
• Syntax:
Classname objectname=new classname();
Ex:
Test obj=new Test();
• Accessing class members
o There are two types of class members –the data members and the method.
o These members can be accessed using dot operator.
o For accessing the data members the syntax is-
Name_of_object.variable_name=value;
For example:
Obj.name=“xyz”
• For accessing the method of the class the syntax is-
Name_of_object.methodname(parameterlist);
For example:
Obj.display();
❖ Access control and modifiers
• Access modifiers control access to data fields, methods, and classes.
• There are three modifiers used in java:
1. Public
2. Private
3. Default modifier
• Private allows classes, method and data fields accessible only from within the own
class.
• If public or private is not used then by default the classes, methods, data fields are
assessable by any class in the same package.

2
❖ Argument Passing :
• Generally, there are two different methods of passing an argument to a function.
• These methods are : a. Call by value b. Call by reference
o Call by value – In call by value method, the value of an argument is copied to
the formal parameter. That is, the changes made in the actual argument are
not reflected into the formal parameter.
o Call by Reference – In the Call by Reference method, the changes made to
the actual argument are also reflected in the formal argument.
❖ Constructors :
• The constructor is a specialised method used for initialising the objects.
• Note that the name of the constructor and the name of the class must be the same.
• The constructor is invoked whenever an object of its associated class is created.
• It is called constructor because it creates the values for the data fields of the class.
• Properties of constructor :-
o Name of the constructor must be the same as the name of the class.
o The constructor must be declared in the public mode.
o The constructor gets invoked automatically when object gets created.
o The constructor should not have nay return type.
o The constructor can have default arguments.
o Multiple constructors can be used by the same class.
• Types of constructor :-
1. Simple/Default constructor
2. Parameterised constructor
1.Simple/Default constructor:-
• A constructor with out argument or constructor which have no argument called
default constructor.
Ex:
Public class Rectangle
{
Int height;
Int width;
Rectangle()
{
System.out.println(“Simple constructor”);
Height=10;
Width=20;
}
}

3
2.Parameterized constructor :-
• Constructor can have any number of arguments.
• Constructor with input parameters are called parameterized constructor.
• For invoking parameterized constructors, appropriate parameters should be
provided wile creating object.
Ex:
Public class Rectangle
{
Int height;
Int width;
Rectangle()
{
System.out.println(“Simple constructor”);
height=10;
width=20;
}
Rectangle(int h, int w)
{
System.out.println(“parametersied constructor”);
Height=h;
Width=w;
}
}
❖ This keyword
• When a calling object wants to refer it sown values then the this reference is a used.
• The this keyword is a keyword used for making the this reference.
• Using this reference we can refer to class’s hidden data fields.
• For example:
this.a=a;
• Means assign the value of a to the data field a of calling object.

4
❖ Finalize() method
• Java has facility of automatic garbage collection.
• Hence even though we allocate the memory and then forget to deallocate it then the
objects that are no longer is used get freed.
• Inside the finalize() method you will specify those actions that must be performed
before an object is destroyed.
• The garbage collector runs periodically checking for objects that are no longer
referenced by any running state or indirectly though other referenced objects.
• To add finalizer to a class simply define the finalize method.
• The syntax to finalize() the code is-
Void finalize()
{
Finalization code
}

You might also like