objected-oriented programming with java

9
OBJECT-ORIENTED PROGRAMMING WITH JAVA Oum Saokosal, Chief of Computer Science National Polytechnic Institute of Cambodia Tel: (855)-12-252-752 E-mail: [email protected]

Upload: oum-saokosal

Post on 14-Feb-2017

212 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Objected-Oriented Programming with Java

OBJECT-ORIENTED PROGRAMMING WITH JAVAOum Saokosal, Chief of Computer ScienceNational Polytechnic Institute of CambodiaTel: (855)-12-252-752E-mail: [email protected]

Page 2: Objected-Oriented Programming with Java

Chapter 6 Objects and Classes(Revision)

• OOP• Classes• Objects• Constructors• Accessing Object’s Data and Methods

Page 3: Objected-Oriented Programming with Java

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

Page 4: Objected-Oriented Programming with Java

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

Page 5: Objected-Oriented Programming with Java

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;}

}

Page 6: Objected-Oriented Programming with Java

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;}

}

Page 7: Objected-Oriented Programming with Java

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 objectstu = new Student();System.out.println(stu.toString());

}}

Page 8: Objected-Oriented Programming with Java

Access Object’s Data and Methods• To access method:public class TestStudent {public static void main(String[] args){

Student stu;//To construct an objectstu = 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.

Page 9: Objected-Oriented Programming with Java

END OF REVISION