0% found this document useful (0 votes)
13 views

Object-Oriented-Programming-OOP

Uploaded by

rahimoo2926
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)
13 views

Object-Oriented-Programming-OOP

Uploaded by

rahimoo2926
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/ 10

Object-Oriented

Programming (OOP)
Welcome to the world of Object-Oriented Programming (OOP)! In this
presentation, we'll delve into the concepts of instance and class fields,
exploring how they define the characteristics and behaviors of objects within
a class. We'll also learn how to create, initialize, and display these fields using
objects.

AM by Abdul Malik
Understanding Instance and
Class Fields

1 Instance Fields 2 Class Fields


Instance fields represent the Class fields, also known as
unique data associated with static fields, belong to the
each individual object of a class itself, not individual
class. They store values instances. They hold data
specific to each instance, shared across all objects
making them distinct from created from that class.
others.
Defining Instance Fields
Data Type Field Name Access Modifiers
Each instance field must be declared Choose meaningful and descriptive Access modifiers control the visibility
with a specific data type, such as int, names for instance fields to clearly of instance fields. Options include
string, or boolean, to determine the identify their purpose within the public, private, protected, and
kind of data it can hold. class. internal.
Initializing Instance Fields

1 In-Line Initialization
Assign values to instance fields directly within the class
definition, when the field is declared.

2 Constructor Initialization
Use the constructor of the class to assign values to instance
fields when an object is created.

3 Method Initialization
Initialize instance fields within methods of the class,
providing flexibility based on specific conditions or
calculations.
Accessing Instance Fields
Within the Same Class From Other Classes Visibility Restrictions

Directly access instance fields using the Access instance fields of an object from Access modifiers control the visibility of
dot operator (.) within the same class other classes using the object reference instance fields, restricting access based
where they are defined. and the dot operator. on the modifier used (e.g., public,
private).
Defining Class Fields
Data Type Field Name Access Modifiers
Each class field must be declared with Choose meaningful and descriptive Access modifiers control the visibility
a specific data type, such as int, names for class fields to clearly of class fields. Options include public,
string, or boolean, to determine the identify their purpose within the private, protected, and internal.
kind of data it can hold. class.
Initializing Class Fields

1 In-Line Initialization
Assign values to class fields directly within the class
definition, when the field is declared.

2 Static Initializer Block


Use a static initializer block to initialize class fields,
providing flexibility for complex initialization logic.

3 Method Initialization
Initialize class fields within static methods of the class,
providing flexibility based on specific conditions or
calculations.
Accessing Class Fields
Within the Same Class From Other Classes Visibility Restrictions

Directly access class fields using the Access class fields of a class from other Access modifiers control the visibility of
class name and the dot operator (.) classes using the class name and the dot class fields, restricting access based on
within the same class where they are operator. the modifier used (e.g., public, private).
defined.
Creating Objects and
Initializing Fields
Object Creation

1 Use the 'new' keyword to create an object from a class,


allocating memory for its instance fields.

Instance Field Initialization

2 Values assigned to instance fields through constructors or in-


line initialization become unique to each object.

Class Field Access

3 Access and modify class fields using the class name and the
dot operator, as they are shared across all objects.
Displaying Field Values
Field Type Access Method Output Example

Instance Field Object objectName.instanc


reference.fieldName eField = value

Class Field ClassName.fieldNa ClassName.classFiel


me d = value

You might also like