Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 6 Introduction to classes and objects
What is Object-Oriented Programming ? What classes, objects, methods ? How to declare a class ? How to use class to create an object? How to declare methods in a class ? Emank X Mezank 2 Presented & Prepared by: Mahmoud R. Alfarra
It is a whole new way of thinking about programming! It is a way of modeling software that maps your code to the real world. Now please, Look in this room … 3 Presented & Prepared by: Mahmoud R. Alfarra What is Object-Oriented Programming ? Chair Door Man Window. No. of legs Material Color Size Purpose. Abstraction Encapsulation
4 Presented & Prepared by: Mahmoud R. Alfarra What is Object-Oriented Programming ? Any Thing Attributes Behavior Each one represented by variable in class Each one represented by method in class
The terms class and object are often confused, and it is important to understand the distinction. It may help you to visualize these terms using the earlier racecar analogy. Think of a class as the template for the car, or perhaps the plans used to build the car. The car itself is an instance of those plans, so it could be referred to as an object. 5 Presented & Prepared by: Mahmoud R. Alfarra What are classes, objects ?
6 Presented & Prepared by: Mahmoud R. Alfarra Classes, Objects ? Student Student Student Student Student car Car-AB-201 Car-AB-323 Car-BM-291 Objects classes
The behavior of the class will be represented as a method in class. The method can be declared as: 7 Presented & Prepared by: Mahmoud R. Alfarra What are methods ? Access_modifiers return_type method_name (arguments) { // instructions return value; }
8 Presented & Prepared by: Mahmoud R. Alfarra What are methods ? (Example) Public void add (int x, int y) { int sum = x + y; } Public int even (int x) { if (x%2 ==0) return x; else { JOptionPane.showMessageDialog(null, "The value is not even "); Return -1; } }
9 Presented & Prepared by: Mahmoud R. Alfarra How to declare Class? Access_modifiers class class_name{ //variables declaration // constructor // methods } Access_modifiers class class_name{ //variables declaration // constructor // methods } Public class Car{ public String model; public String ID; public int price; public Car (){ model = “Cevic”; ID = “ ”; price= 12,000; // methods } Public class Car{ public String model; public String ID; public int price; public Car (){ model = “Cevic”; ID = “ ”; price= 12,000; // methods }
10 Presented & Prepared by: Mahmoud R. Alfarra How to use class to create an object? class_name obj_name = new constructor_name (); Car c1 = new Car (); Car c2 = new Car (); c2.model = “Honda”; c2.ID = “987435”; c2.price = 4000; Car c2 = new Car (); c2.model = “Honda”; c2.ID = “987435”; c2.price = 4000;
قال الله تعالى: (يوم تكون الجبال كالعهن المنفوش) 11 Presented & Prepared by: Mahmoud R. Alfarra
Equality and Relational Operators 12 Presented & Prepared by: Mahmoud R. Alfarra