Essential Object-Oriented Programming Concepts Defined
Core OOP Definitions
Class and Object
- Class: A user-defined data structure that binds data members and operations (methods) into a single unit.
 - Object: An instance of a class. Objects are used to perform actions or allow interactions based on the class definition.
 
Variables and Attributes
- Method: An action performed using the object's attributes.
 - Attributes: Characteristics or properties of a class. Also known as instance variables (declared outside methods, belonging to one object). They are accessible through static and public methods.
 - Class Variable: Declared using the 
statickeyword; shared among all objects of the class. - Local Variables: Declared inside methods, constructors, or blocks; they exist only while the method runs. They cannot be accessed using static or public methods outside their scope, as they are local to the method/block.
 
Essential Keywords and Modifiers
Access and Behavior Modifiers
public: Accessible from anywhere in the program.static: Allows a method or variable to be called without creating an instance of the class.- This member belongs to the class itself, not to any particular object (instance).
 - Inside a static method, you do not have access to the current object (
this). To call an instance attribute within a static method, you need an object reference. - If a variable is a class variable (declared 
static), it can be accessed without any object reference within a static method. 
void: Indicates that a method does not return any value.final: Used to make a variable fixed (a constant), or to prevent a class from being subclassed or a method from being overridden.
The 'this' Keyword
this: Refers to the current object instance. This is typically used when a parameter name in a method conflicts with an instance variable name.- Example of potential error without 
this: If you writename = name, the assignment often has no effect on the instance variable (e.g., "The assignment to variable modelName has no effect"). The instance variable might remain uninitialized (e.g.,null,0, or0.0depending on the type), though the code will still compile and run. 
Object Instantiation and Constructors
Creating an Object Instance
Syntax Example:
Lect_Ex11_Vehicle mytesla = new Lect_Ex11_Vehicle();- An object named 
myteslais created. myteslais of typeLect_Ex11_Vehicle.- The 
new()keyword creates a new object and invokes a constructor (default or custom) for initialization. 
Understanding Constructors
Syntax Example:
public Class_Name() { }- A constructor must have the same name as the class.
 - A constructor is not a method and therefore has no return type (you cannot use 
void, asvoidis a return type for methods). - It is called automatically when you use 
new()(either the default no-argument constructor or a custom constructor). 
Accessing Class Members Across Files
Scenario: Class A (Vehicle) and Class B (Main_File)
To access attributes and methods of Class A (e.g., Vehicle) within Class B (e.g., Main_File), you must create an object instance of Vehicle in Main_File.
- You cannot directly access declared instance variables/attributes; an object instance is required.
 - To access a 
staticmethod or variable in Class A from Class B, reference the class name directly (instead of an object instance).
Example:ClassA.method(). 
Method and Constructor Overloading
Overloading Definition
Overloading allows you to reuse the same method name (or constructor name) by defining several methods with different numbers or types of input parameters (signatures).
Constructor Overloading Example
int Year;
String Name;
double Weight;
public Tut13_ConstructorOverloading(int year) {
    Year = year;
}
public Tut13_ConstructorOverloading(int year, String name) {
    Year = year;
    Name = name;
}
public Tut13_ConstructorOverloading(int year, String name, double weight) {
    Year = year;
    Name = name;
    Weight = weight;
}Handling Multiple Objects
To create and process multiple objects efficiently, you typically use constructors and arrays or collections.
- Define a constructor, e.g., 
Tut15_MultipleObjs(String brandname, String type) {}. - Create an array (or list) of that object type:
Tut15_MultipleObjs[] MyObjects = new Tut15_MultipleObjs[4]; - Initialize each object within the 
MyObjectsarray. 
References: Arrays and ArrayLists
For detailed information on arrays and dynamic lists, refer to the following topics:
Arrays (Lect_Ex16_aboutArrays)
- Note: You cannot simply call the array name (e.g., 
frenzArray) to print the entire array content directly. 
ArrayLists (Lect_Ex17_aboutArrayList)
- The 
ArrayListclass is a resizable array found in thejava.utilpackage. ArrayListuses methods (e.g.,.add(),.get()) rather than operators.- Elements stored in an 
ArrayListare objects. Remember thatStringin Java is an object, not a primitive type. - To use primitive types (like 
intordouble) in anArrayList, you must use their equivalent wrapper classes (e.g.,IntegerorDouble). 
Related Tutorials
Tut_14_LongWordFinderLab0andLab1
OOP Principles and Object Relationships
Data Encapsulation
- Allows controlled access to members and methods, typically by using the 
privateaccess modifier. - Hides internal representations (implementation details) from outside access.
 
Object Interactions
Inter-class communication achieved via object references and accessor methods (Getters and Setters).
- Class B declares certain 
privatevariables. - Class B provides public accessor methods for each private member:
public 'Return Type' get_membername() {} public void set_membername(arg) {} - Class A defines a public method that accepts an object of Class B (Object Reference):
Class A then uses the accessors (public void interactWithB(Tut22b_ClassB objB)get()andset()) methods to access and modifyobjB's instance variables. - The Main class creates instances of both A and B, and passes object B into object A's interaction method.
 
Object Composition (Strong "Has-a" Relationship)
One class contains references to other class objects as instance variables. The contained object (the part) is usually created and owned by the container (the whole), meaning its lifetime depends on the container.
Example: A Car has an Engine, and the Car controls the Engine's creation.
- Define the Part Class (e.g., 
Engine) with variables, methods, constructor, and accessors. - Define the Whole Class (e.g., 
Car):- Declare an 
Engineclass instance variable as aprivatevariable. (OnlyCarcontrols how theEngineis used.) - Create the 
Engineobject inside theCarconstructor. (This enforces Encapsulation: theCarclass decides how its engine is built.) - Note: If you create the 
Engineobject during variable declaration, it limits flexibility. Creating it in the constructor allows for unique initialization per object. 
 - Declare an 
 - The Main Class creates a 
Carinstance and calls car-related methods. 
Object Aggregation (Loose "Has-a" Relationship)
One class has a reference to another class, but the lifetime of the contained object is independent of the container. The part is created outside and passed in, potentially shared between multiple objects.
Example: A Car has an Engine, but the Engine might exist independently or be shared.
- Define the Part Class (e.g., 
Engine) with variables, methods, constructor, and accessors. - Define the Whole Class (e.g., 
Car):- Declare an 
Engineclass instance variable as aprivatevariable. - Define the 
Carconstructor to accept anEngineobject as an input parameter. - In the constructor, assign the passed 
Engineobject to theEngineinstance variable declared in class variables. 
 - Declare an 
 - The Main Class handles object creation:
- Create an 
Engineinstance first. - Create a 
Carinstance and pass the createdEngineinstance as a parameter. 
 - Create an 
 
Access Modifiers in OOP
Access modifiers determine the visibility or access level of variables, methods, constructors, or classes to other parts of the code.
Private Access (Enforcing Encapsulation)
Private members can only be accessed within the same class.
Private Variables
- Variable values can be set and reset within the same class.
 - In a different class, a private variable is not accessible, even if you create an object of that class. You cannot use dot notation like 
obj.private_var_name1. - To allow controlled access from different classes, you must provide public 
get()andset()methods (accessors). 
Private Methods
- Declare your class and its instance variables (including private ones).
 - Write constructors and public methods. Use these public methods as entry points to call and utilize the private methods internally.
 
- There is no other way to call a private method directly when you are in another class, even by creating and using objects of the defining class.
 
Access Level Summary Table
| Modifier | Access Level | Same Class | Same Package | Subclass (Different Package) | Anywhere (Different Package) | 
|---|---|---|---|---|---|
public | Anywhere in the program | Yes | Yes | Yes | Yes | 
private | ONLY within same class | Yes | No | No | No | 
protected | Same package and subclasses | Yes | Yes | Yes | No | 
default (No Modifier) | Within same package | Yes | Yes | No | No | 
English with a size of 11.61 KB