Classes and Objects
Classes and Objects
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
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
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.