OO Programming Concepts ● Class O Programmer-defined type OInstance variable method o Obiect is the instance of the class o System analysis->Class->Object o This section discusses O How to create objects O How to use objects ● OBPVS.OOP Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 OO Programming Concepts ⚫ Class Programmer-defined type Instance variable + method ⚫ Object is the instance of the Class ⚫ System analysis-→Class->Object ⚫ This section discusses How to create objects How to use objects ⚫ OBP Vs. OOP
Objects data field 1 radius =5__Data field,State operties State data field m find Areao Method method 1 Behavior method n (A)A generic object (B)An example of circle object An object has a unique identity, 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 Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Objects data field 1 method n data field m method 1 (A) A generic object ... ... State (Properties) Behavior radius = 5 findArea() Data field, State Properties Method, Behavior (B) An example of circle object An object has a unique identity, 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
Classes o Every Java class must extend another class O If class does not explicitly extend another class, it extends java. lang Object o class implicitly extends Object(1l methods definded in it Class constructor ○ Same name as class O Initializes instance variables of a class object O Called when program instantiates an object of that class O Can take arguments, but cannot return data types(void can’ t either) O A constructor with no parameters is referred to as a no-arg constructor. O Class can have several constructors, through overloading Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Classes ⚫ Every Java class must extend another class If class does not explicitly extend another class ,it extends java.lang.Object ⚫class implicitly extends Object(11 methods definded in it) ⚫ Class constructor Same name as class Initializes instance variables of a class object Called when program instantiates an object of that class Can take arguments, but cannot return data types (“void” can’t either) A constructor with no parameters is referred to as a no-arg constructor. Class can have several constructors, through overloading
Without maino class circl 大大 The radius of this circ1e*/ double radius =1.0; Data field /* Construct a circle object * Circle( Constructors /* Construct a circle object * Circle(double newRadius) radius newradiusi /大大 Return the area of this ciro1e*/ double findAreao[ Method return radius radius *3.14159; Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Classes class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double findArea() { return radius * radius * 3.14159; } } Data field Method Constructors Without main()
instructors What if there is void Constructors are a special Circle i kind of methods that are invoked to construct objects Circle(double newRadius)( radius newRadius Demo Circle java Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Constructors Circle() { } Circle(double newRadius) { radius = newRadius; } Constructors are a special kind of methods that are invoked to construct objects. What if there is void Demo Circle.java