Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects and Classes Mostafa Abdallah

Similar presentations


Presentation on theme: "Objects and Classes Mostafa Abdallah"— Presentation transcript:

1 Objects and Classes Mostafa Abdallah Eng_mostafa_mufic@yahoo.com

2 Agenda Class and Object. Defining and using Class.

3 Class and Object 7-3

4 Objects in life An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and car. 7-4

5 Objects in life An object has a state, and behaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of methods. 7-5

6 Class A template or blueprint that describes the kinds of state and behavior that objects of its type support. Class Uses:  Variables to define data fields.  Methods to define behaviors. Objects and classes 7-6

7 An Object is an instance of a class That object will have :  Its own state.  Access all behaviors defined by its class. Objects and classes 7-7

8 Objects and classes Class Name: Circle Data field: radius Methods: getArea() Circle : Object1 Data field: radius : 10 Circle : Object2 Data field: radius : 25 Circle : Object3 Data field: radius : 125 A Class Template Three objects of Circle Class 7-8

9 Defining and using Classes 7-9

10 Defining Class 7-10

11 Data Fields A Java class uses variables to define data fields. Java assign default values for data fields. –0 for a numeric type. –False for a boolean type. –'\u0000' for a char type. –Null for reference type. 7-11

12 Data Fields However, Java assigns no default value to a local variable inside a method. public class Student { String name; // name has default value null int age; // age has default value 0 } public class Test { public static void main(String[] args) { Student student = new Student(); System.out.println("name? " + student.name); System.out.println("age? " + student.age); } 7-12

13 Constructors A special kind of methods that are invoked to construct objects. 7-13

14 Constructors, cont Must have the same name as the class itself. Do not have a return type—not even void. Are invoked using the new operator when an object is created. When has no parameters is referred to as a no-arg constructor. 7-14

15 Default Constructors A class may be declared without constructors.  In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor. 7-15

16 Example public class Circle { public static final double PI = 3.14159; // A constant public double r;// instance field holds circle’s radius // The constructor method: initialize the radius field public Circle(double rad) { r = rad; } // Constructor to use if no arguments public Circle() { r = 1.0; } // The instance methods: compute values based on radius public double circumference() { return 2 * PI * r; } public double area() { return PI * r*r; } } 7-16

17 Declaring Object To declare a reference variable, use the syntax: ClassName objectRefVar; Example: Circle myCircle; 7-17

18 Creating Object To reference an object, assign the object to a reference variable: new ClassName(); Example: new Circle(); new Circle(5.0); 7-18

19 Declaring/Creating Object ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle(); Create an object Assign object reference 7-19

20 Object Types in Memory 7-20

21 Copying Variables 7-21

22 Accessing Objects Referencing the object’s data: objectRefVar.data ex: myCircle.radius Invoking the object’s method: objectRefVar.methodName(arguments) ex: myCircle.getArea() Ex: Circle c=new Circle(); c.radius=5; c.getArea(); 7-22

23 Questions

24 Thanks


Download ppt "Objects and Classes Mostafa Abdallah"

Similar presentations


Ads by Google