TOPICS Class basics: Defining and using a class Access identifiers Set/get methods Data initialization What is a constructor? Defining constructors Overloading constructor Usingthisreference Dr. MFH, 2022 2 IN JAVA Everything must be in a class. There are no global functions or global data. Even Main ............ is a Class Dr. MFH, 2022 3 What Is A Class? Let’s start by an example: • Properties: – Name – Age – Breed – Favorite Food • Behaviors: – Make meow sound – Play – Wash – Eat – Hunt Dr. MFH, 2022 4 Copyright 2017, Oracle and/or its affiliates. All rights reserved. What Is A Class? The combination of properties and behaviors is ... – Called a class – A blueprint or recipe for an object – Usedtocreateobjectinstances Object instances Cat class -Properties -Behaviors Dr. MFH, 2022 5 Copyright 2017, Oracle and/or its affiliates. All rights reserved. Creating New Instances From A Blueprint → Objects Cat class Properties • name • age • favoriteFood Behaviors • meow • play • eat cat1 • "Opal" • 11 • "Mice" cat2 • "Misty" • 5 • "Chicken" cat3 • "Tiger" • 15 • "BlueJ" Dr. MFH, 2022 6 Copyright 2017, Oracle and/or its affiliates. All rights reserved. So............Object-Oriented Programming • Interaction of objects • No arranged sequence Dr. MFH, 2022 7 Copyright 2017, Oracle and/or its affiliates. All rights reserved. So............A Class : – Is a blueprint or recipe for an object – Describes an object’s properties (data types) and behaviors (operations) – Is used to create Object instances Class -Properties -Behaviors Object instances Dr. MFH, 2022 8 Copyright 2017, Oracle and/or its affiliates. All rights reserved. To better understand: Let’s consider an online shopping cart scenario. Imagine an online store called Duke’s Choice. (1) One shopper is Duke’s mother, Mrs. Duke. As she shops, she places items in a shopping cart, she places shirts. (2) After she fills the cart, she checks out. (3) The checkout process applies the purchase to a credit card, which is verified, (4) and then Mrs. Duke receives an order number so that she can track her order or return it. As a software developer, when you are presented with this scenario such as Duke’s Choice for an application that you need to develop, you can analyze the scenario by breaking it into steps and defining the objects of the scenario. Dr. MFH, 2022 9 Copyright 2017, Oracle and/or its affiliates. All rights reserved. To better understand: Dr. MFH, 2022 10 Copyright 2017, Oracle and/or its affiliates. All rights reserved. To better understand: To validate objects in such a problem domain you identify the properties of all objects: • Objects are physical or conceptual. Physical: Shirt Conceptual: Online Account Or Credit Card Dr. MFH, 2022 11 Copyright 2017, Oracle and/or its affiliates. All rights reserved. To better understand: • Objects have properties that represent the state of the object: – Size – Price – Color Color property value is red. Copyright 2017, Oracle and/or its affiliates. All rights reserved. To better understand: • Objects have behaviors (things they can do) : – Shop – Put item in cart –Pay