Oop 2023
Oop 2023
Concepts
Good Questions
Variable
Method
Software Bicycle
Rectangle2.java
The Class Declaration
Constructor
A method in a class that initialize an instance of an
object before it's used.
• The same name as the class and have no return type
Multiple Constructors: the name name but a different
number of arguments or different typed arguments
• Method Overloading
Java Provides default constructors.
The special variable, this, can be used inside a method
to refer to the object instance.
Rectangle.java
Member Variables
Declaration
Instance and Local Variable
Local variable is defined public int getHalf() {
inside a block of code or a int half; // local
method. variable
Example (FirstClass.java) half = firstVariable / 2;
• Instance Variable: return half;
firstVariable }
• Local Variable: half
Methods
Method Declaration
Return a Value from a
Method
Use return operator in the method to return the
value.
Methods can return a primitive type or a reference
type.
The class of the returned object must be either a
subclass of or the exact class of the return type.
Method Overload
Signature of a Method: return value, name, parameter
list
Method Overloading: Use the same method name with
different arguments to group together related methods.
Constructors can also be overloaded.
• this(parameters) : Call another constructor within a
constructor
Example: FirstClass.java, SecondClass.java,
Rectangle.java
Passing Information into a
Method
Argument types
• primitive and reference data type: Yes
• method: No
Argument Names
• Can have the same name as one of the class's member
variable
Use this to refer to the member variable
Creating Objects
Using Objects
Cleaning Up Unused Objects
Creating Objects
Call a constructor
private O
protected O O O
public O O O O
package O O
Private
How About one Alpha object access the private member of another
Alpha object?
Protected
Object
Vehicle Building
Van Truck