Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Objected-Oriented Programming with Java
1. OBJECT-ORIENTED PROGRAMMING
WITH JAVA
Oum Saokosal, Chief of Computer Science
National Polytechnic Institute of Cambodia
Tel: (855)-12-252-752
E-mail: oum_saokosal@yahoo.com
2. Chapter 6 Objects and Classes
(Revision)
• OOP
• Classes
• Objects
• Constructors
• Accessing Object’s Data and Methods
3. Object-Oriented Programming
• Why learning OOP?
• Java is a pure OOP
• OOP provides more flexibility, modularity, clarity, reusability
• Other Programming: C++, C#, VB .NET, ActionScript 3.0, Ruby etc.
also use OOP.
• 3 concepts of OOP:
• Class Encapsulation
• Class Inheritance
• Polymorphism
4. Classes (1)
• What is a class?
• A class is similar to a template, blueprint or symbol (in Adobe
Flash).
• A class creates many objects.
Class
Objects
5. Classes (2)
• In a class, there are :
• Data Fields (Properties)
• Behavior:
• Constructors
• Methods
public class Student {
private int id, String name;
public Student(){}
public Student (int inputID,
String inputName){
this.id = inputID;
this.name = inputName;
}
public String toString(){
return id + name;
}
}
6. Constructors (1)
• What is constructor?
• A constructor is to construct (create) objects from a class.
public class Student {
private int id, String name;
public Student(){}
public Student (int inputID,String inputName){
this.id = inputID;
this.name = inputName;
}
public String toString(){
return id + name;
}
}
7. Constructors (2)
• How to construct objects?
• Usually, we have many classes in a projects. So the student class
will be created in other classes.
• To construct objects from student class:
public class TestStudent {
public static void main(String[] args){
Student stu;
//To construct an object
stu = new Student();
System.out.println(stu.toString());
}
}
8. Access Object’s Data and Methods
• To access method:
public class TestStudent {
public static void main(String[] args){
Student stu;
//To construct an object
stu = new Student(“123”,”Veasna”);
System.out.println(stu.toString());
}
}
• To access data:
• Usually, We don’t access the data. If you like, you can do like :
stu.id = 123 in case id is not private.