230 likes | 244 Views
Understand the concept of classes and objects in Java, including fields, constructors, methods, data encapsulation, and more. Learn to create, access, and manipulate objects in Java programming.
E N D
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Spring 2018
Chapter 6 Presentation Outline • The General Idea about Class and Object • Instance Fields and Methods • Constructors • Overloading Methods and Constructors • Scope of Instance Fields • Data Encapsulation • Packages and Import Statements • Java API classes
The concepts of class and object • Java is an Object-Oriented Programming language • Also known as OOP • Tries to mimic the way we see the world • Makes it easier to develop, debug, and maintain a code A program based on objects? How?
Similar houses in my neighborhood How is that possible?
The Class and Object Concepts • Class Concept: • A Class is like a blueprint of something, like the blue print of a house • A Class is NOT the house itself, but it is the “instruction manual” that tells how to build a house (one or more of them) • Object Concept: • The object is the actual thing (e.g., the house) • The object is constructed based on the blueprint (the Class) • An object is created by using the word “new” • HousepaulosHouse = new House(); • House marysHouse = new House();
The main idea • You create a class: • Put the functionality you want • Put some data/parameters you want • You create objects based on your class: • Home majorHouse = new Home(); • majorHouse.getAddress();
Class Major Components • Fields: • Internal variables or objects Example: Class House • Address • Zip Code • Price • Methods: • Functions that uses the internal variables/objects to perform some type of task • getAddress() • changeAskingPrice() • getNumberOfBedrooms() • All methods can see the class fields (global scope)
Example public class House { // class fields private String address; private double price; public intnumberOfBedrooms; private booleanhasSwimmingPool; // class methods public double getPrice() { return price; } public void setPrice(double updatedValue) { price = updatedValue; } }
Questions and more questions How do I call the class methods? How do I create one or more houses from this class? How do I access the class fields? Any difference in the way we access public and private fields?
Using your class public class MyProgram { // creating a house House myHouse = new House(); myHouse.setPrice(150000); House yourHouse = new House(); yourHouse.setPrice(300000); System.out.println(“My house worths: $” + myHouse.getPrice()); System.out.println(“Your house worths: $” + yourHouse.getPrice()); myHouse.setZipCode(15432); // since numberOfBedrooms has public access…. myHouse.numberOfBedrooms = 3; // it can be accessed directly! System.out.println(“Number of bedrooms: “ + myHouse.numberOfBedrooms); myHouse.numberOfBedrooms = 100; // even though it is possible, it is bad! }
How can we avoid a class field to be set to an invalid value??? (e.g., numberOfBedrooms = 1000) Solution: Have “something” to validate the request before taking the action of changing a field value Dealer: “Sorry, we have only in black, silver, and white” Car: “what is he thinking???? Customer: “I want a green car”
Data Encapsulation Done Private Fields Change the number of bedrooms to 1000 Public fields (and methods) bedrooms Change the price to $50.00 setZip() Zip price setPrice() getPrice() address getZip() setAddr() Not a chance! Valid prices are $150K to $200K
Data Encapsulation • Data encapsulation is achieve by: • Declaring the class fields as “private” • Using setters to validate the data change request before changing it • Using getters to get the current values of encapsulated fields • It is a great way to keep invalid data away
public class House { // class fields private String address; private double price; private intnumberOfBedrooms; private booleanhasSwimmingPool; // class methods public double getPrice() { return price; } public void setPrice(double updatedValue) { if(updatedValue < 150000 || updatedValue > 200000) { System.out.println(“Invalid price value, please try again); } else { price = updatedValue; } } public void setPrice(double updatedValue) { price = updatedValue; }
Who builds your house (the object) from the blueprint (class)? Default order Special order A Class can have multiple constructors: a default one (that takes no input arguments) and others more specialized ones
Class, Objects, Fields, Methods, Encapsulation, Getters, Setters, Constructors… GOT IT! Can we have a full example, please????
Bike Retailer Fields (attributes): Color (red, blue, white) Size (kids, adult) Model (Cruizer, …) Methods for changing: color size model Red text means that it is the default value used to display the bike on the web browser. The user can change it though later on