🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Topic 1.5 Constructors_ CS 002-CS21S1 - Advanced Object-Oriented Programming.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

 CS 002-CS21S1 - Advanced Object-Oriented Programming Pages Topic 1.5 Constructors...

 CS 002-CS21S1 - Advanced Object-Oriented Programming Pages Topic 1.5 Constructors Immersive Reader First Semester SY 2024-202… Account Home Syllabus Topic 1.5 Constructors Dashboard Modules Discussions Courses Assignments Module 1 - Topic 5: Constructors Groups Quizzes Chat Calendar BigBlueBu on 44 People Currently, the examples, light bulb and bank account, contained the fields and methods of the object. However, what would be the ini al values of the fields defined in the classes? Inbox Grades 2 In order to provide or assign an ini al value, a CONSTRUCTOR must be defined. A constructor is an op onal special method is called when an instance is being created in a process called INSTANTIATION. Remember that constructor is an op onal History Collabora ons component, what happens now when no constructor is defined? When no constructor is defined, the interpreter defines one. The default constructor will assign default values to all the fields inside the class according to their type. Help 1. Numeric - default is 0 2. Character - Unicode empty character '\u0000' is assigned 3. Boolean - variable value will be false 4. Object - null will be the value But when a constructor is already defined, the default constructor will never be created. Syntax A constructor can be defined by the following. 1. public - modifier to make the constructor available. 2. name - class name 3. pair of parenthesis - enclosing the parameter list 4. parameter list - same as in method Bulb Class with Constructor public class LightBulb{ private boolean isPowered; private int watts; private String color; public LightBulb(String init_color, int init_watts){ isPowered = false; color = init_color; watts = init_watts; } public void turnOn(){ if(!isPowered){ isPowered = true; } } public void turnOff(){ if(isPowered){ isPowered = false; } } public void flipSwitch(){ isPowered = !isPowered; } public boolean checkStatus(){ return isPowered; } public String checkColor(){ return color; } public int checkWatts(){ return watts; } } Note: Constructors are also methods except that it requires no return type since it will return the instance of the object. It is always invoked when using the new keyword. this Keyword There will be mes when you want some of your methods use the same variable names defined with fields in the class. This provides us that the purpose of the variable is to be assigned to the field of the instance. However, by doing this, we might confuse the interpreter to which variable are we pertaining to. In order to alleviate this, the this keyword is provided. This keyword specifically points to the current instance from which the method was triggered. public class Person{ private String name; public void changeName(String name){ this.name = name; } } In the given code above, the class has a field name and a method changeName(). The method required 1 parameter which is named as name. By using the keyword this, the interpreter was able to iden fy that the argument name must be assigned to the field name. Assignment Please click the link to answer the assignment: Assignment 1.2 Constructors Home Page Back to Top Previous Next

Use Quizgecko on...
Browser
Browser