90 likes | 265 Views
Chapter 7 Object-Oriented Programing. Lecturer: Ty Rasmey Email:rasmeyt2@gmail.com. Overview. OOP Classes Objects Constructor Accessing an Object’s Data and Methods. Object-Oriented Programming. What is OOP? Modelling real-world object in software Why design application in this way?
E N D
Chapter 7Object-Oriented Programing Lecturer: Ty Rasmey Email:rasmeyt2@gmail.com
Overview • OOP • Classes • Objects • Constructor • Accessing an Object’s Data and Methods
Object-Oriented Programming • What is OOP? • Modelling real-world object in software • Why design application in this way? • We naturally classify objects into different types • By attempting to do this with software aim to make it more maintainable, understandable, easier to reuse • Other Programming: C++, C#, VB .NET, ActionScript, Ruby etc. also use OOP. • 3 Concepts of OOP • Class Encapsulation; protects and manages own information • Class Inheritance; software reuse • Polymorphism
Class(1) • What is Class? • Class is a template or blueprint or model • A class creates many objects
Class(2) • In a class, there are: • State; data field(also known as Properties) • Behaviour: • 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; } }
Constructor(1) • What is Constructor? • A constructor to construct 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; } }
Constructor(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()); } }
Accessing an 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 = 123in case id is not private.