The document provides an overview of object-oriented programming concepts, specifically focusing on class and object creation in Java. It discusses constructors, method overloading, inheritance, encapsulation, and the importance of access modifiers. Additionally, it covers debugging techniques and the process of compiling and running Java programs.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
5 views5 pages
Gmetrix Domain 4
The document provides an overview of object-oriented programming concepts, specifically focusing on class and object creation in Java. It discusses constructors, method overloading, inheritance, encapsulation, and the importance of access modifiers. Additionally, it covers debugging techniques and the process of compiling and running Java programs.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5
DOMAIN 4: OBJECT-ORIENTED nagcreate ka ng object ng Product
PROGRAMMING class, meron dapat siyang
productIdValue, typeValue, Class – base definition for an object; productNameValue, at price. defines what characteristics an object has. Creating an object of a class with constructor:
Product prod1 = new Product(1,
“Tea”, “Green Tea”, 8.99);
Accessing a property with an object:
String type = prod1.typeValue;
NOTE: class Product should be static
para makagawa ng object nito sa main method. If hindi static, hindi siya valid magkakaron ng error.
Constructor Overloading – same
NOTE: A class name should have a function as method overloading. Same CAPITAL LETTER for each word (ex: constructor name, different BukengkengClass) parameters.
Constructors – A method with the Ex:
same name as the class it is using to Product(int idValue, String typeValue){ create an object. In simpler terms, defines how an object is to be created. }
Product(int idValue){
IMPORTANT NOTES:
1. A java file can hold many
classes. 2. The initial class name and the ADDITIONAL EXPLANATION: file name NEED to match. Ginagamit ang constructors para 3. Per java file, you can only have mamodify kung ano data dapat ang one public non-static class. meron ang isang object ng isang class. The “this” keyword – can be used as a Kunwari sa example, sa first picture shortcut within methods and yung Product class while sa second constructors. picture is yung Product constructor. Syntax for Constructor (new Ang ginagawa lang sa second picture constructor with no parameters): ay sinasabi niyang dapat kapag Product(){ 1. Instance a. Creates a memory space this(“tea”, “Green tea”) every time that it is used. { b. Use for variables that can be changed / aren’t the Creating new object: same every time. Product prod2 = new Product(); 2. Static a. Only uses one memory Explanation: Sa example syntax dahil space no matter how sa this keyword, automatic na many times it is called. magkakaron ng default value based sa b. Use for members that are nakasulat sa constructor yung new going to be the same object na ginawa(prod2). Kumbaga every time. shortcut lang siya imbis na sa paggawa ng new object ilagay yung STATIC FINAL – used for members / characteristics ng object. variables that are final and would not change for the whole program. Used INHERITANCE – the same as we to create a constant member. discussed, uses extends keywords. Used to inherit properties and Final keyword indicates that it would methods of a parent class. always be the same value unless changed within the program. OVERRIDING – Also same as we discussed, overriding happens when a ENCAPSULATION – private data child class uses the same method as members; hiding information. Making the parent class but implements a variable private means you can only different function for it. Kumbaga use it in its class / native class. same name pero iba yung ginagawa. NOTE: String.format(“%.2f”, floatVariable), syntax of formatting any number into 2 decimal places CLASS DATA MEMBERS number. SCOPE OF MEMBERS
1. Public – can be used anywhere METHODS
in the app or project. 2. Protected – can be used in - Methods perform actions, often native class and the class that in the form of calculations. would inherit. PRIVATE METHOD – can only be used 3. Private – can only be used in within its native class. native class. PUBLIC METHOD – can be called INSTANCE AND STATIC DATA MEMBERS anywhere within the app.
PROTECTED METHOD – can be
called anywhere within the package. Whenever a method needs to return to a value it needs to have these two: METHOD PARAMETERS 1. Return Type
2. Return Statement at the end of
the method
VOID METHOD – a void method runs
and then its done; IT DOES NOT RETURN A VALUE.
Parameters act as variables inside NOTE: Return Type of method and
the method. Parameters are specified return value should be of the same after the method name, inside the data type or an error will occur.
parentheses.
RETURN TYPE, VOID, AND RETURN INSTANCE AND STATIC METHODS
VALUE 1. Instance a. Don’t have static keyword b. Creates an instance of a class. 2. Static a. Uses static keyword b. Standalone and runs on its own Main Method NEEDS TO BE STATIC so Wildcard Import – imports all utilities that it can be run without an instance within the imported package. of it being created.
OVERLOADING
Same as mentioned constructor
IMPORTING CLASSES overloading or method overloading. Same name but different parameters. No need to import classes as long as its not private. You can create instances of classes too. INSTANTIATE AND USE A CLASS DOMAIN 5: CODE OBJECT COMPILATION AND INITIALIZING A CLASS – simply, DEBUGGING building a class. PRINT STATEMENT DEBUGGING
Print Statements – used to check or
debug calculations.
JAVAC COMMAND OUTPUT
Used in the TERMINAL tab, to run a
java file in the terminal tab the syntax is:
Javac filename.java
INSTANTIATING A CLASS – creating To appoint a value to two variables
an object of a class. and run the java file, syntax:
java filename 0 1
WHEN COMPILING: remember to
put .java after filename, if the program NULL(Data type) – useful for takes arguments remember to put declaring a variable. those in terminal command. When you initialize variables / ANALYZE FOR LOGIC ERRORS properties in a class you can set default value on those. If you have an else-if statements, it’s best to debug by checking the IMPORTING PACKAGES AND conditions one by one. You can do this CLASSES by modifying the variable used in the IMPORTING condition of the else-if statement.