objected-oriented programming with java

Post on 14-Feb-2017

212 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

OBJECT-ORIENTED PROGRAMMING WITH JAVAOum Saokosal, Chief of Computer ScienceNational Polytechnic Institute of CambodiaTel: (855)-12-252-752E-mail: oum_saokosal@yahoo.com

Chapter 6 Objects and Classes(Revision)

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

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

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

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

}

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

}

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());

}}

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.

END OF REVISION

top related