[go: up one dir, main page]

0% found this document useful (0 votes)
17 views16 pages

Classes and Objects

The document provides an overview of Object Oriented Programming, focusing on concepts such as Nested Classes, Visibility of Class Members, Access Levels, and Constructors. It explains the structure of a class, the process of creating objects, and the distinction between instance and local variables. Additionally, it discusses reference variables and their behavior in relation to memory management.

Uploaded by

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

Classes and Objects

The document provides an overview of Object Oriented Programming, focusing on concepts such as Nested Classes, Visibility of Class Members, Access Levels, and Constructors. It explains the structure of a class, the process of creating objects, and the distinction between instance and local variables. Additionally, it discusses reference variables and their behavior in relation to memory management.

Uploaded by

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

Object Oriented

Programming
Dr. Ayman Soliman

- ENG/Abdelrazek Mohamed

ID / 235001 .
CONTENTS
1. Nested Classes and Nested
Interfaces
2. Visibility of Class Members
3. Access Levels
4. Constructors
5. Objects
6. Dot (.) Operator
NESTED CLASSES
• Nested Classes are classes defined within another class.
• They are divided into two types:
1. Static Nested Classes: These classes cannot access non-static members of the outer class.
2. Inner Classes: These classes can access all members of the outer class, including private ones.
PROPERTIES OF NESTED CLASSES

• Static Nested Classes:


• Cannot access non-static members of the outer class.
• They are like regular classes but defined inside another class.
• Inner Classes:
• Can access both static and non-static members of the outer class, even if they are private.
• They can be more tightly coupled to the outer class and are often used to represent relationships.
• Access Modifiers:
• Nested classes can have different access modifiers like Private, Protected, and Public, depending on the intended
visibility.
Visibility
of
Class
Members
The visibility of class members defines who can access the fields and methods
within a class:
• Public: The member is accessible from anywhere in the program.
• Private: The member is only accessible within the same class and cannot be
accessed from outside.
• Protected: The member is accessible within the same package or by subclasses.
Acces
s
Levels

Access levels in Java define the scope and visibility of class members (fields and
methods). They determine who can access or modify these members:
• Public: The member is accessible from everywhere in the program.
• Protected: The member is accessible within the same package and subclasses
only.
• Private: The member is accessible only within the class where it is declared.
CONSTRUCTOR
S

A constructor is a special method in a class that is called when an object is created. It is used to initialize the object’s
fields with default or specified values.
Key Characteristics of Constructors:
• A constructor has the same name as the class.
• It does not have a return type (not even void).
• It is automatically called when using the new keyword to create an object.
• If no constructor is defined, Java provides a default no-argument constructor.
GENERAL TEMPLATE
OF A CLASS

To define a class, its structure typically includes the following components:


1. Instance Fields (Variables):
• Variables that store the state or properties of the class.
2. Methods:
• Functions that define the behavior or actions of the class.
3. Constructors:
• Special methods used to initialize the object.
4. Inner Classes or Interfaces (Optional):
• Nested classes or interfaces that are part of the class structure.
EXAMPLE OF CLASS
DEFINITION

Components of a Class:
1. Instance Fields:
• Variables that store the state or properties of the class.
2. Methods:
• Functions that define the behavior or actions of the class.
3. Constructors:
• Special methods used to initialize the object.
4. Inner Classes or Interfaces (Optional):
• Nested classes or interfaces within the class.
Creating an object involves defining a variable
of the class type and initializing it using the new
keyword along with the constructor of the class.
This process allocates memory for the object
and calls the constructor to initialize its fields.
After creation, the object can be used to access
its methods and fields.

CREATING OBJECTS
In object-oriented programming, creating an object involves
instantiating a class to form an instance of that class. This
process is done using the new keyword, followed by the
class constructor. The constructor initializes the object and
assigns initial values to its fields. Once an object is created,
we can access its fields and methods using the dot operator.

CREATING AN
OBJECT
In object-oriented programming, variables can be classified into instance variables
and local variables.
• Instance Variables: These variables are declared inside the class but outside any
method. They belong to the object created from the class and are accessible
throughout the class. Each object has its own copy of the instance variables.
• Local Variables: These variables are declared inside methods or constructors and
are only accessible within those methods or constructors. They are temporary and
exist only during the execution of the method or block.

INSTANCE AND LOCAL


VARIABLES
EXAMPLE OF USING
OBJECTS In object-oriented programming,
objects are instances of a class that
can store data and perform actions.
You can create multiple objects
from a single class, each with its
own set of data. Once an object is
created, you can assign values to
its fields and use its methods to
perform actions or retrieve
information.
REFERENCE VARIABLES
In object-oriented programming,
reference variables hold the
memory address of an object. When
you assign one reference variable
to another, both variables refer to
the same object in memory. This
means that changes made through
one reference variable will affect
the object and be visible through
the other reference variable.
REFERENCE VARIABLES
BEHAVIOR
In object-oriented programming,
when a reference variable is
assigned to another, both variables
point to the same object in memory.
This means that modifying the
object through one reference will
reflect in the other, as they both
refer to the same memory location.
ThankYou
-

You might also like