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

Java Object-Oriented Programming

Java allows for object-oriented programming through classes, objects, attributes, and methods. A class defines the structure and behavior of an object using attributes like variables and methods like functions. An object is an instance of a class that is created using the new keyword. Attributes store data within each object and can be accessed and modified using dot notation with the object reference. Methods contain reusable blocks of code that are called on objects. Constructors initialize new objects when they are created.

Uploaded by

Aljon Catiban
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Java Object-Oriented Programming

Java allows for object-oriented programming through classes, objects, attributes, and methods. A class defines the structure and behavior of an object using attributes like variables and methods like functions. An object is an instance of a class that is created using the new keyword. Attributes store data within each object and can be accessed and modified using dot notation with the object reference. Methods contain reusable blocks of code that are called on objects. Constructors initialize new objects when they are created.

Uploaded by

Aljon Catiban
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Object-oriented Programming

Create a new class


 Use class keyword
 Separate file if creating a public class
Syntax:
(for in-file class)
class <className> {
}

(for public class; separate file)


public class <className> {
//className should be same as the file name. (Example: public class Hoy {…} should have the file
name Hoy.java)
}

Create a new object


 Created from class.
 Specify the class name then object name (personalized; can use any names you want), and use the keyword
new
Syntax:
className <objectName> = new className();

Attributes
 Variables found inside the class.
 To access, use the object you’ve created and add a dot (.) and insert the variable name.
Syntax:
<objectName>.<variableName>
 To modify, same procedure as accessing but add an equal sign (=) then the new value.
Syntax:
<objectName>.<variableName> = <newValue>;
 To prevent modification, add final keyword before the variable’s data type. (final keyword is a modifier)
Syntax:
final <datatype> <variableName>;

Methods
 Also called as functions
 block of code which only runs when it is called.
Creating Methods
 must be declared within a class
 specify the access modifier (public, protected, default, private), non-access modifier (static, final,
abstract, etc…), return type you want to return, method name (any name will do), parameters (anything
you need in your method; requirements to access the method)
Syntax:
<access modifier> <non access modifier> <datatype> <method name> (<parameters>) {…}
Or
<non access modifier> <datatype> <method name> (<parameters>) {…}
NOTE: parameters are optional.
Accessing Methods
 just type the method name and insert the parameters on the parenthesis in order.
Syntax:
<method name> (<parameters>)
Overloaded Methods
 methods that share the same name but has different parameters.
RETURN Statement
 will return the variable you specified in the return statement within the method.
Syntax:
return <variable name>;
Constructors
 special method when an object is instantiated (constructed)
 useful when creating multiple objects with different attributes.
Syntax:
<class name> (<parameters>);
when accessing…
<class name> <variable name> = new <constructor>;

You might also like