Download presentation
Presentation is loading. Please wait.
1
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Spring 2018
2
Chapter 6 A First Look at Classes
3
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
4
The Concept of Class and Object
5
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?
6
The General Concept of a Class
7
Similar houses in my neighborhood
How is that possible?
8
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” House paulosHouse = new House(); House marysHouse = new House();
9
You create objects based on your class:
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();
10
Major Components of a Class
11
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)
12
Example public class House { // class fields private String address;
private double price; public int numberOfBedrooms; private boolean hasSwimmingPool; // class methods public double getPrice() { return price; } public void setPrice(double updatedValue) { price = updatedValue;
13
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?
14
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! }
15
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”
16
Data Encapsulation Done Change the number of bedrooms to 1000
Private Fields Change the number of bedrooms to 1000 Public fields (and methods) bedrooms Change the price to $50.00 setZip() Zip price getPrice() setPrice() address getZip() setAddr() Not a chance! Valid prices are $150K to $200K
17
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
18
public class House { // class fields private String address; private double price; private int numberOfBedrooms; private boolean hasSwimmingPool; // class methods public double getPrice() { return price; } public void setPrice(double updatedValue) { if(updatedValue < || updatedValue > ) { System.out.println(“Invalid price value, please try again); } else { price = updatedValue; public void setPrice(double updatedValue) { price = updatedValue; }
19
Class Constructors
20
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
21
Can we have a full example, please????
Class, Objects, Fields, Methods, Encapsulation, Getters, Setters, Constructors… GOT IT! Can we have a full example, please????
22
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
23
Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.