Sei sulla pagina 1di 10

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY Introduction Object-Oriented Programming (OOP) a programming discipline that revolves around the

e concept of objects as the basic elements of your program. Objects can have properties and behaviors. Example: Properties Behavior Car type of transmission burning manufacturer braking color accelerating Lion weight roaring color sleeping tamed of wild hunting Objects in the physical world can be easily modeled as a software object using the properties as data and the behaviors as methods. Classes and Objects Object a software component whose structure is similar to objects in the real world. It is composed of data(properties) which are variables describing the characteristics of an object and a set of methods(behavior) that describe how an object behaves. The variables and methods in a Java object are formally known as instance variables and instance methods. Class a fundamental structure in object-oriented programming. It can be thought of as a template/prototype/blueprint of an object. It consists of two types of members which are called fields and methods. Fields specify data types defined by the class, while methods specify operations. An object is an instance of a class. Example: Car Class Plate Number Color Instance Variables: Manufacturer Current Speed Instance Methods: Object Car A ABC 111 Blue Mitsubishi 50 kph Accelerate Method Turn Method Brake Method Object Car B XYZ 123 Red Toyota 100 kph Object

Class Variables and Methods Class variables are variables that belong to the whole class. This means that it has the same value for all the objects in the same class. They are also called static member variables.

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY Car Class Plate Number Color Instance Variables: Manufacturer Current Speed Instance Methods: Class Variables Object Car A ABC 111 Blue Mitsubishi 50 kph Accelerate Method Turn Method Brake Method number_of_wheels = 4; Object Car B XYZ 123 Red Toyota 100 kph

Creating Your Own Classes Before creating your class, think first on where you will be using your class and how your class will be used. Think of an appropriate name for that class, and list all the information or properties and methods that you want your class to contain. Then, declare your class using the following form: <modifiers> class <class_name>{ [<attribute declaration>] [<constructor declaration>] [<method declaration>] } Where: <class_name> can be any legal identifier. <modifiers> describes whether the class is public or private. The body of the class declares a set of data attributes and methods associated with the class. The following example creates a class that will contain a student record. Example: 1 2 3 4 public class StudentRecord { //Code here }

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY Declaring Fields After the class has been defined, write down a list of information that a student record can contain: Identifier Name Address Age mathGrade englishGrade scienceGrade averageGrade String String int double double double double Data Type

Instance Variables After listing all the properties, add them to our code using the following form: <modifiers> <type> <name>; Since these properties are unique for each object, declare them as instance variables. Example: 1 2 3 4 5 6 7 8 9 10 11 public class StudentRecord { private String name; private String address; private int age; private double mathGrade; private double englishGrade; private double scienceGrade; private double average; //Well add more code later }

Declaring Methods After creating the class, create methods that will add more functionality of the class. accessor method used to read values from class variables. mutator method used to write or change values to class variables. An accessor method usually starts with a get[NameofInstanceVariable] and a mutator method is usually written as set[NameOfInstanceVariable].

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY Example: Instance Variable Name Address Age mathGrade englishGrade scienceGrade averageGrade Method getName, setName getAddress, setAddress getAge, setAge getMathGrade, setMathGrade getEnglishGrade, setEnglishGrade getScienceGrade, setScienceGrade getAverageGrade

get() Methods Below is an implementation of a get() method: 1 2 3 4 5 6 7 public class StudentRecord { private String name; . public String getName(){ return name; }

The statement name = temp; assigns the value of temp to name thus changes the data inside the instance variable. Another example of the get() method for computing the average grade: 1 2 3 4 5 6 7 8 9 public class StudentRecord { private String name; public double getAverage(){ double result = 0; result = (mathGrade + englishGrade + scienceGrade)/3; return result }

The getAverage() method computes for the average of the 3 grades and returns the result.

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY set() Methods Below is an implementation of a set() method: 1 2 3 4 5 6 7 8 public class StudentRecord { private String name; .. public void setName(String temp){ name = temp; } }

The this Reference The this reference is used to access the instance variables shadowed by the parameters. Suppose you have the following declaration for setAge. 1 2 3 public void setAge(int age){ age = age; //wrong!! }

In the statement age = age; the method assigns the value of the parameter to itself! In order to correct this mistake, use the this reference:

1 2 3

public void setAge(int age){ this.age = age;//correct!! }

This method will then assign the value of the parameter age to the instance variable of the object StudentRecord. Here is the initial code for the StudentRecord class: 1 2 3 public class StudentRecord { private String name;

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 private String address; private int age; private double mathGrade; private double englishGrade; private double scienceGrade; private double average; public String getName(){ return name; } public void setName(String temp){ name = temp; } public double getAverage(){ double result = 0; result = (mathGrade + englishGrade + scienceGrade)/3; return result; } //Well add more code later }

Creating Your Test Class Remember that the class StudentRecord does not contain a main method, thus the class will not execute once you run the program. To test the StudentRecordClass, create a separate class which will contain a main method. Example: 1 2 3 4 5 6 7 8 9 public class Main { public static void main(String args[]){ //Create 3 objects for StudentRecord StudentRecord annaRecord = new StudentRecord(); StudentRecord beahRecord = new StudentRecord(); StudentRecord chrisRecord = new StudentRecord(); } }

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY Class Instantiation To create an object or an instance of a class, use the new operator: TemplateClassName ObjectName = new TemplateClassName(*constructors parameter+); The new operator allocates a memory for that object and returns a reference of that memory location. When you create an object, you invoke the class constructor. The constructor is a method where you place all the initializations. Invoking Objects Members An object consists of both variables and methods. Both of these are called members of an object. Java uses the dot notation for both: referencaToAnObject.memberOfAnObject Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Main { public static void main(String[] args){ //Create three objects for StudentRecord StudentRecord annaRecord = new StudentRecord(); StudentRecord beahRecord = new StudentRecord(); StudentRecord chrisRecord = new StudentRecord(); //Set the name of the students annaRecord.setName(Anna); beahRecord.setName(Beah); crisRecord.setName(Cris); //print Annas Name System.out.println(annaRecord.getName()); } }

Class Variables or Static Variables Aside from instance variables, you can also declare class variables or variables that belong to the class as a whole. The values of these variables are the same for all the objects of the same class. Now suppose, you want to know the total number of student records you have for the whole class, you can declare one static variable named studentCount to hold this value.

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY To declare a static variable, use the keyword static: 1 2 3 4 5 public class StudentRecord { . private static int studentCount; }

For the static variable to studentCount, create a static method to access its value: 1 2 3 4 5 6 7 public class StudentRecord { private static int getStudentCount(){ return studentCount(); } }

Declaring Constructors Constructors are important in instantiating an object. It is where all the initializations are placed. The ff. are the properties of a constructor: 1. Constructors have the same name as the class 2. A constructor is just like an ordinary method, however only the following information can be placed in the header of a constructor, scope and accessibility of identifies, constructors name and parameters if it has any. 3. Constructors do not have any return value. 4. You cannot call a constructor directly, it can only be called by using the new operator. Every class has a default constructor. The default constructor is the constructor without any parameters. If the class does not specify any constructors, then an implicit default constructor is created.

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY For example, in the StudentRecord class, the default constructor would look like this: 1 2 3 4 public studentRecord() { //some code here }

Overloading means having more than one constructor in a class or having more than one method with the same name in a class. Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public StudentRecord() { studentCount++; } public StudentRecord(String temp) { this.name = temp; studentCount++; } public StudentRecord(String name, double mathGrade, double englishGrade, double scienceGrade) { this.name = name; this.mathGrade = mathGrade; this.englishGrade = englishGrade; this.scienceGrade = scienceGrade; studentCount++; }

The following code demonstrates how to use these constructors: 1 2 3 public static void main(String[] args){ //create three objects for StudentRecord StudentRecord annaRecord = new StudentRecord();

University of La Salette COLLEGE OF INFORMATION TECHNOLOGY 4 5 6 7 StudentRecord beahRecord = new StudentRecord(Beah); StudentRecord crisRecord = new StudentRecord(Cris,80,85,83); //some code here }

Access Modifiers There are four different types of member access modifiers in Java: public, private, protected and default. The first three modifiers are explicitly written while no keyword is used for default. Default access only classes in the same package can have access to the class variables and methods. Public access class members are accessible to anyone both inside and outside the class. Protected access - class members are accessible only to methods in that class. Private access class members are only accessible by the class they are defined in. The accessibility criteria is summarized in the table below: Modifier Private Default Protected Public Same Class Yes Yes Yes Yes Same Package Yes Yes Yes Subclass Universe

Yes Yes

Yes

Potrebbero piacerti anche