0% found this document useful (0 votes)
10 views20 pages

Module 2 - Object and Classes in Java

The document explains the fundamental concepts of objects and classes in Java, which are essential to Object Oriented Programming (OOP). It details the structure of class declarations, types of class variables, and the characteristics of objects, including their state, behavior, and identity. Additionally, it outlines various methods for initializing objects in Java.
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)
10 views20 pages

Module 2 - Object and Classes in Java

The document explains the fundamental concepts of objects and classes in Java, which are essential to Object Oriented Programming (OOP). It details the structure of class declarations, types of class variables, and the characteristics of objects, including their state, behavior, and identity. Additionally, it outlines various methods for initializing objects in Java.
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

OBJECT AND

CLASSES IN JAVA
DCIT 50
Objects and Classes in Java

In Java, classes and objects are basic concepts of


Object Oriented Programming (OOPs) that are used to
represent real-world concepts and entities. The class
represents a group of objects having similar properties and
behavior.
Java Classes

A class in Java is a set of objects which shares


common characteristics/ behavior and common properties/
attributes. It is a user-defined blueprint or prototype from
which objects are created.
Properties of Java Classes
◦ A class does not take any byte of memory.
◦ A class is just like a real-world entity, but it is not a real-world
entity. It's a blueprint where we specify the functionalities.
◦ A class contains mainly two things: Methods and Data Members.
◦ A class can also be a nested class.
◦ Classes follow all of the rules of OOPs such as inheritance,
encapsulation, abstraction, etc.
Types of Class Variables
◦ Local Variables - Variables defined inside methods, constructors
or blocks are called local variables. The variable will be declared
and initialized within the method and the variable will be
destroyed when the method has completed.
◦ Instance Variables - Instance variables are variables within a class
but outside any method. These variables are initialized when the
class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
◦ Class Variables - Class variables are variables declared within a
class, outside any method, with the static keyword.
Class Declaration in Java
Structure of Class Declaration in Java

access_modifier - control the visibility of classes, methods,


and variables.
◦ public: Accessible from any other class.
◦ private: Accessible only within the same class.
◦ protected: Accessible within the same package or
subclasses.
Structure of Class Declaration in Java

class <class_name> - is the name you give to the class, which


should start with a capital letter as per Java naming
conventions.
data member - These are variables declared inside the class
that represent the attributes or properties of the objects
created from the class.
Structure of Class Declaration in Java

Method – it define behaviors or actions that objects of the


class can perform. Methods contain code that can be called
or invoked to perform certain tasks.
Constructor - A constructor is a special method used to
initialize objects. It has the same name as the class and no
return type. When an object is created, the constructor is
called to set up initial values for the object's data members.
Structure of Class Declaration in Java
Nested class - is a class defined within another class. It can be useful
for logically grouping classes that are only used in one place or
enhancing encapsulation.
◦ Static Nested Class - A static class inside another class. It doesn’t
need an instance of the outer class to be created.
◦ Inner Class: A non-static class inside another class. It requires an
instance of the outer class to be created.
Interface - is a reference type that contains only abstract methods
(methods without a body) and constants. It is a way to define a
contract that implementing classes must follow.
Java Objects

An object in Java is a basic unit of Object-Oriented


Programming and represents real-life entities. Objects are
the instances of a class that are created to use the attributes
and methods of a class. A typical Java program creates
many objects, which as you know, interact by invoking
methods.
Example of an Object
Characteristic of an Object

An object typically consists of:


◦ State : It is represented by attributes of an object. It also
reflects the properties of an object.
◦ Behavior : It is represented by the methods of an object. It
also reflects the response of an object with other objects.
◦ Identity : It gives a unique name to an object and enables
one object to interact with other objects.
Ways of Initializing Objects in Java

◦ Using new keyword - It is the most common and general


way to create an object in Java.
◦ Using [Link](String className) method - returns
the Class object associated with the class with the given
string name.
Ways of Initializing Objects in Java

◦ Using clone() method - clone() method is present in the


Object class. It creates and returns a copy of the object.
◦ Deserialization - De-serialization is a technique of reading
an object from the saved state in a file
Object Declaration in Java
Structure of Object Declaration in Java

Class_name - This is the name of the class from which you


are creating an object. It represents the blueprint or template
for the object.
object_name - This is the name you are giving to the object
you are creating. It acts as a reference to the object in
memory, allowing you to interact with the object's data
members and methods.
Structure of Object Declaration in Java

new keyword - is used to allocate memory and initialize a


new object of the class.
Class_name([parameters]) - This is a call to the constructor of
the class.

You might also like