ObjectOriented Programming Revision Noor ullah khan Lecturer Department
Object-Oriented Programming Revision Noor ullah khan, Lecturer, Department of computer Science. IT Manager UOS lahore. E-mail: noorkhan@uoslahore. edu. pk
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, Action Script 3. 0, Ruby etc. also use OOP. • 3 concepts of OOP: – Class Encapsulation – Class Inheritance – Polymorphism
Concept: Classes describe objects • Every object belongs to (is an instance of) a class • An object may have fields, or variables – The class describes those fields • An object may have methods – The class describes those methods • A class is like a template, or cookie cutter – You use the class’s constructor to make objects 4
Concept: Classes are like Abstract Data Types • An Abstract Data Type (ADT) bundles together: – some data, representing an object or "thing" – the operations on that data • The operations defined by the ADT are the only operations permitted on its data • Example: a Checking. Account, with operations deposit, withdraw, get. Balance, etc. • Classes enforce this bundling together – If all data values are private, a class can also enforce the rule that its defined operations are the only ones permitted on the data 5
Concept: Classes describe objects • Every object belongs to (is an instance of) a class • An object may have fields, or variables – The class describes those fields • An object may have methods – The class describes those methods • A class is like a template, or cookie cutter – You use the class’s constructor to make objects 6
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 input. ID, String input. Name){ this. id = input. ID; this. name = input. Name; } public String to. String(){ 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 input. ID, String input. Name){ this. id = input. ID; this. name = input. Name; } public String to. String(){ 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 Test. Student { public static void main(String[] args){ Student stu; //To construct an object stu = new Student(); System. out. println(stu. to. String()); } }
Access Object’s Data and Methods • To access method: public class Test. Student { public static void main(String[] args){ Student stu; //To construct an object stu = new Student(“ 123”, ”Veasna”); System. out. println(stu. to. String()); } } • 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
- Slides: 12