Chapter 6 Objects and Classes Prerequisites for Part II apter 5 Arrays She calls out to the man on the street Sir can you help me Oh think twice hapter 6 Objects and Classes It's another day for you and me In paradise hapter 7 Strin You can cover GUI after Chapter 8 Chapter 8 Inheritance and Polymorphism Chapter 11 Getting Started with GUI Programming Chapter 9 Abstract Classes and Interfaces > Chapter 12 Event-Driven programming Chapter 10 Object-Oriented Modeling hapter 15 Exceptions and Assertions You can cover Exceptions and l/o after Chapter 8 Chapter 16 Simple Input and Output Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 6 Objects and Classes Prerequisites for Part II Chapter 6 Objects and Classes Chapter 7 Strings Chapter 8 Inheritance and Polymorphism Chapter 5 Arrays Chapter 9 Abstract Classes and Interfaces Chapter 10 Object-Oriented Modeling Chapter 11 Getting Started with GUI Programming Chapter 12 Event-Driven Programming Chapter 15 Exceptions and Assertions Chapter 16 Simple Input and Output You can cover Exceptions and I/O after Chapter 8 You can cover GUI after Chapter 8 She calls out to the man on the street Sir can you help me … Oh think twice It's another day for you and me In paradise …
Objectives To understand objects and classes and use classes to model objects(s 6.2) o To learn how to declare a class and how to create an object of a class($ 6.3) To understand the roles of constructors and use constructors to create objects (§6.3) To use UML graphical notations to describe classes and objects ($6.3) To distinguish between object reference variables and primitive data type variables (§64) To use classes in the Java library (86.5) To declare private data fields with appropriate get and set methods to make class easy to maintain($6.6-6.8) To develop methods with object arguments($ 6.9) To understand the difference between instance and static variables and methods (§6.10 To determine the scope of variables in the context of a class($6.11) To use the keyword this as the reference to the current object that invokes the instance method($ 6. 12) To store and process objects in arrays($6. 13) To apply class abstraction to develop software($6.14) o To declare inner classes.gqry. to Java Programming, rewsed by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To understand objects and classes and use classes to model objects (§6.2). ⚫ To learn how to declare a class and how to create an object of a class (§6.3). ⚫ To understand the roles of constructors and use constructors to create objects (§6.3). ⚫ To use UML graphical notations to describe classes and objects (§6.3). ⚫ To distinguish between object reference variables and primitive data type variables (§6.4). ⚫ To use classes in the Java library (§6.5). ⚫ To declare private data fields with appropriate get and set methods to make class easy to maintain (§6.6-6.8). ⚫ To develop methods with object arguments (§6.9). ⚫ To understand the difference between instance and static variables and methods (§6.10). ⚫ To determine the scope of variables in the context of a class (§6.11). ⚫ To use the keyword this as the reference to the current object that invokes the instance method (§6.12). ⚫ To store and process objects in arrays (§6.13). ⚫ To apply class abstraction to develop software (§6.14). ⚫ To declare inner classes (§6.17)
OO Programming Concepts Object Oriented Programming(OOP) O Encapsulates data (attributes) and methods (behaviors) o Objects: An object represents an entity in the real world that can be distinctly identified O Allows objects to communicate oWell-defined interfaces Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 OO Programming Concepts ⚫Object Oriented Programming (OOP) Encapsulates data (attributes) and methods (behaviors) ⚫Objects: An object represents an entity in the real world that can be distinctly identified Allows objects to communicate ⚫Well-defined interfaces
OO Programming Concepts o Procedural programming language OC is an example OAction-oriented O Functions are units ofprogramming o Object-oriented programming language O Java is an example OObject-orientec O Classes are units ofprogramming o Functions, or methods, are encapsulated in classes Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 OO Programming Concepts ⚫Procedural programming language C is an example Action-oriented Functions are units of programming ⚫Object-oriented programming language Java is an example Object-oriented Classes are units of programming ⚫Functions, or methods, are encapsulated in classes
OO Programming Concepts o Procedural programming language algrithom data structure o Object-oriented programming language Object t messaging object message Liang, Introduction to Java Programming, rewsed by Dai- kary
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 OO Programming Concepts ⚫Procedural programming language ⚫Object-oriented programming language Object + messaging algrithom + data structure
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