Classes, Fields, Constructors, Methods
Defining Classes
Software University
http://softuni.bg
SoftUni Team
Technical Trainers
1. Defining Simple Classes
2. Fields
3. Methods
4. Constructors, Keyword this
5. Static Members
Table of Contents
2
sli.do
#java-advanced
Have a Question?
Defining Classes
 Specification of a given type of objects
from the real-world
 Classes provide structure for describing
and creating objects
Defining Simple Classes
5
class Car {
…
}
Class name
Class body
Keyword
Naming Classes
 Use PascalCase naming
 Use descriptive nouns
 Avoid ambiguous names
6
class Dice { … }
class BankAccount { … }
class IntegerCalculator { … }
class TPMF { … }
class bankaccount { … }
class numcalc { … }
Class Members
7
 Class is made up of state and behavior
 Fields store state
 Methods describe behaviour
class Car {
String make;
String model;
void start(){ … }
}
Fields
Method
class Dog {
int age;
String type;
void bark(){ … }
}
Fields
Method
Creating an Object
8
 A class can have many instances (objects)
class Program {
public static void main()
{
Car firstCar = new Car();
Car secondCar = new Car();
}
}
Variable stores a
reference
Use the new keyword
 Declaring a variable creates a reference in the stack
 The new keyword allocates memory on the heap
Object Reference
9
Car car = new Car();
HEAPSTACK
diceD6
(1540e19d)
obj
type = null
sides = 0
Classes vs. Objects
10
 Classes provide structure for describing and creating objects
 An object is a single instance of a class
Dice (Class)
D6 Dice
(Object)
Class Data
Storing Data Inside a Class
Fields
12
public class Car {
private String make;
private int year;
public Person owner;
…
}
Fields can be of any
type
 Class fields have access modifiers, type and name
type
access modifier
name
 Create a class Car
 Ensure proper naming!
Problem: Define Car Class
13
Car
+make:String
+model:String
+horsePower:int
(no actions)
Class name
Class fields
Class methods
Solution: Define Car Class
14
public class Car {
String make;
String model;
int horsePower;
}
 Classes and class members have modifiers
 Modifiers define visibility
Access Modifiers
15
public class Car {
private String make;
private String model;
}
Class modifier
Member modifier
Fields should always
be private!
Methods
Defining a Class Behaviour
 Store executable code (algorithm) that manipulate state
Methods
17
class Car {
private int horsePower;
public void increaseHP(int value) {
horsePower += value;
}
}
 Used to create accessors and mutators (getters and setters)
Getters and Setters
18
class Car {
private int horsePower;
public int getHorsePower() {
return this.horsePower;
}
public void setHorsePower(int horsePower) {
this.horsePower = horsePower;
}
}
Field is hidden
Getter provides
access to field
Setter provide field change
this points to the
current instance
 Keyword this
 Prevent field hiding
 Refers to a current object
Getters and Setters
19
private int horsePower;
public void setSides(int horsePower) {
this.horsePower = horsePower;
}
public void setSidesNotWorking(int horsePower) {
horsePower = horsePower;
}
 Create a class Car
Problem: Car Info
20
Car
-make:String
…
+setMake():void
+getMake():String
…
+carInfo():String
+ == public
- == private
return type
Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
Solution: Car Info
21
public class Car {
private String make;
private String model;
private int horsePower;
public void setMake(String make) { this.make = make; }
public String getMake() { return this.make; }
public String carInfo() {
return String.format("The car is: %s %s - %d HP.",
this.make, this.model, this.horsePower);
}
//TODO: Create the other Getters and Setters
}
//TODO: Test the program
Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
Constructors
Object Initialization
 Special methods, executed during object creation
 The only one way to call a constructor in Java is
through the keyword new
Constructors
23
public class Car {
private String make;
public Car() {
this.make = "BMW";
}
}
Overloading default
constructor
 Special methods, executed during object creation
Constructors (1)
24
class Car {
private String make;
…
public Car() {
this.make = "unknown";
…
}
}
Overloading default
constructor
 You can have multiple constructors in the same class
Constructors (2)
25
public class Car {
private int horsePower; private String make;
public Car(String make) {
this.make = make;
}
public Car(String make, int horsePower) {
this.make = make;
this.horsePower = horsePower;
}
}
Constructor with all
parameters
Constructor with one
parameter
 Constructors set object's initial state
Object Initial State
26
public class Car {
String make;
List<Part> parts;
public Car(String make) {
this.make = make;
this.parts = new ArrayList<>();
}
}
Always ensure
correct state
 Constructors can call each other
Constructor Chaining
27
class Car {
private String make;
private int horsePower;
public Car(String make, int horsePower) {
this(make);
this.horsePower = horsePower;
}
public Car(String make) {
this.make = make;
}
}
 Create a class Car
Problem: Constructors
28
Car
-make:String
-model:String
-horsePower:int
+Car(String make)
+Car(String make, String model,
int horsePower)
+carInfo():String
Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
Solution: Constructors
29
public Car(String make) {
this.make = make;
this.model = "unknown";
this.horsePower = -1;
}
public Car(String make, String model, int horsePower) {
this(make);
this.model = model;
this.horsePower = horsePower;
}
Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
Static Members
Members Common for the Class
 Access static members through the class name
 Static members are shared class-wide
 You don't need an instance
Static Members
31
class Program {
public static void main(String[] args) {
BankAccount.setInterestRate(2.2);
}
}
Sets the rate for all
bank accounts
Static Members
32
class BankAccount {
private static int accountsCount;
private static double interestRate;
public BankAccount() {
accountsCount++;
}
public static void setInterestRate(double rate) {
interestRate = rate;
}
}
 Create a class BankAccount
 Support commands:
 Create
 Deposit {ID} {Amount}
 SetInterest {Interest}
 GetInterest {ID} {Years}
 End
Problem: Bank Account
33
BankAccount
-id:int (starts from 1)
-balance:double
-interestRate:double (default: 0.02)
+setInterest(double interest):void
+getId():int
+getInterest(int years):double
+deposit(double amount):void
Create
Deposit 1 20
GetInterest 1 10
End
Account ID1 Created
Deposited 20 to ID1
4.00
(20 * 0.02) * 10
underline ==
static
Solution: Bank Account
34
public class BankAccount {
private final static double DEFAULT_INTEREST = 0.02;
private static double rate = DEFAULT_INTEREST;
private static int bankAccountsCount;
private int id;
private double balance;
// continue…
Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
Solution: Bank Account (2)
35
public BankAccount() {
this.id = ++bankAccountsCount;
}
public static void setInterest(double interest) {
rate = interest;
}
// TODO: int getId()
// TODO: double getInterest(int years)
// TODO: void deposit(double amount)
// TODO: override toString()
}
Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
Solution: Bank Account (2)
36
HashMap<Integer, BankAccount> bankAccounts = new HashMap<>();
while (!command.equals("End")) {
//TODO: Get command args
switch (cmdType) {
case "Create": // TODO
case "Deposit": // TODO
case "SetInterest": // TODO
case "GetInterest": // TODO
}
//TODO: Read command
}
Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
 …
 …
 …
Summary
37
 Classes define specific structure for objects
 Objects are particular instances of a class
 Classes define fields, methods, constructors
and other members
 Constructors are invoked when creating new
class instances
 Constructors initialize the object's
initial state
 https://softuni.bg/modules/59/java-advanced
SoftUni Diamond Partners
SoftUni Organizational Partners
 Software University – High-Quality Education and
Employment Opportunities
 softuni.bg
 Software University Foundation
 http://softuni.foundation/
 Software University @ Facebook
 facebook.com/SoftwareUniversity
 Software University Forums
 forum.softuni.bg
Trainings @ Software University (SoftUni)
 This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-NonCom
mercial-ShareAlike 4.0 International" license
License
42

14. Java defining classes

  • 1.
    Classes, Fields, Constructors,Methods Defining Classes Software University http://softuni.bg SoftUni Team Technical Trainers
  • 2.
    1. Defining SimpleClasses 2. Fields 3. Methods 4. Constructors, Keyword this 5. Static Members Table of Contents 2
  • 3.
  • 4.
  • 5.
     Specification ofa given type of objects from the real-world  Classes provide structure for describing and creating objects Defining Simple Classes 5 class Car { … } Class name Class body Keyword
  • 6.
    Naming Classes  UsePascalCase naming  Use descriptive nouns  Avoid ambiguous names 6 class Dice { … } class BankAccount { … } class IntegerCalculator { … } class TPMF { … } class bankaccount { … } class numcalc { … }
  • 7.
    Class Members 7  Classis made up of state and behavior  Fields store state  Methods describe behaviour class Car { String make; String model; void start(){ … } } Fields Method class Dog { int age; String type; void bark(){ … } } Fields Method
  • 8.
    Creating an Object 8 A class can have many instances (objects) class Program { public static void main() { Car firstCar = new Car(); Car secondCar = new Car(); } } Variable stores a reference Use the new keyword
  • 9.
     Declaring avariable creates a reference in the stack  The new keyword allocates memory on the heap Object Reference 9 Car car = new Car(); HEAPSTACK diceD6 (1540e19d) obj type = null sides = 0
  • 10.
    Classes vs. Objects 10 Classes provide structure for describing and creating objects  An object is a single instance of a class Dice (Class) D6 Dice (Object)
  • 11.
  • 12.
    Fields 12 public class Car{ private String make; private int year; public Person owner; … } Fields can be of any type  Class fields have access modifiers, type and name type access modifier name
  • 13.
     Create aclass Car  Ensure proper naming! Problem: Define Car Class 13 Car +make:String +model:String +horsePower:int (no actions) Class name Class fields Class methods
  • 14.
    Solution: Define CarClass 14 public class Car { String make; String model; int horsePower; }
  • 15.
     Classes andclass members have modifiers  Modifiers define visibility Access Modifiers 15 public class Car { private String make; private String model; } Class modifier Member modifier Fields should always be private!
  • 16.
  • 17.
     Store executablecode (algorithm) that manipulate state Methods 17 class Car { private int horsePower; public void increaseHP(int value) { horsePower += value; } }
  • 18.
     Used tocreate accessors and mutators (getters and setters) Getters and Setters 18 class Car { private int horsePower; public int getHorsePower() { return this.horsePower; } public void setHorsePower(int horsePower) { this.horsePower = horsePower; } } Field is hidden Getter provides access to field Setter provide field change this points to the current instance
  • 19.
     Keyword this Prevent field hiding  Refers to a current object Getters and Setters 19 private int horsePower; public void setSides(int horsePower) { this.horsePower = horsePower; } public void setSidesNotWorking(int horsePower) { horsePower = horsePower; }
  • 20.
     Create aclass Car Problem: Car Info 20 Car -make:String … +setMake():void +getMake():String … +carInfo():String + == public - == private return type Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
  • 21.
    Solution: Car Info 21 publicclass Car { private String make; private String model; private int horsePower; public void setMake(String make) { this.make = make; } public String getMake() { return this.make; } public String carInfo() { return String.format("The car is: %s %s - %d HP.", this.make, this.model, this.horsePower); } //TODO: Create the other Getters and Setters } //TODO: Test the program Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
  • 22.
  • 23.
     Special methods,executed during object creation  The only one way to call a constructor in Java is through the keyword new Constructors 23 public class Car { private String make; public Car() { this.make = "BMW"; } } Overloading default constructor
  • 24.
     Special methods,executed during object creation Constructors (1) 24 class Car { private String make; … public Car() { this.make = "unknown"; … } } Overloading default constructor
  • 25.
     You canhave multiple constructors in the same class Constructors (2) 25 public class Car { private int horsePower; private String make; public Car(String make) { this.make = make; } public Car(String make, int horsePower) { this.make = make; this.horsePower = horsePower; } } Constructor with all parameters Constructor with one parameter
  • 26.
     Constructors setobject's initial state Object Initial State 26 public class Car { String make; List<Part> parts; public Car(String make) { this.make = make; this.parts = new ArrayList<>(); } } Always ensure correct state
  • 27.
     Constructors cancall each other Constructor Chaining 27 class Car { private String make; private int horsePower; public Car(String make, int horsePower) { this(make); this.horsePower = horsePower; } public Car(String make) { this.make = make; } }
  • 28.
     Create aclass Car Problem: Constructors 28 Car -make:String -model:String -horsePower:int +Car(String make) +Car(String make, String model, int horsePower) +carInfo():String Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
  • 29.
    Solution: Constructors 29 public Car(Stringmake) { this.make = make; this.model = "unknown"; this.horsePower = -1; } public Car(String make, String model, int horsePower) { this(make); this.model = model; this.horsePower = horsePower; } Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
  • 30.
  • 31.
     Access staticmembers through the class name  Static members are shared class-wide  You don't need an instance Static Members 31 class Program { public static void main(String[] args) { BankAccount.setInterestRate(2.2); } } Sets the rate for all bank accounts
  • 32.
    Static Members 32 class BankAccount{ private static int accountsCount; private static double interestRate; public BankAccount() { accountsCount++; } public static void setInterestRate(double rate) { interestRate = rate; } }
  • 33.
     Create aclass BankAccount  Support commands:  Create  Deposit {ID} {Amount}  SetInterest {Interest}  GetInterest {ID} {Years}  End Problem: Bank Account 33 BankAccount -id:int (starts from 1) -balance:double -interestRate:double (default: 0.02) +setInterest(double interest):void +getId():int +getInterest(int years):double +deposit(double amount):void Create Deposit 1 20 GetInterest 1 10 End Account ID1 Created Deposited 20 to ID1 4.00 (20 * 0.02) * 10 underline == static
  • 34.
    Solution: Bank Account 34 publicclass BankAccount { private final static double DEFAULT_INTEREST = 0.02; private static double rate = DEFAULT_INTEREST; private static int bankAccountsCount; private int id; private double balance; // continue… Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
  • 35.
    Solution: Bank Account(2) 35 public BankAccount() { this.id = ++bankAccountsCount; } public static void setInterest(double interest) { rate = interest; } // TODO: int getId() // TODO: double getInterest(int years) // TODO: void deposit(double amount) // TODO: override toString() } Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
  • 36.
    Solution: Bank Account(2) 36 HashMap<Integer, BankAccount> bankAccounts = new HashMap<>(); while (!command.equals("End")) { //TODO: Get command args switch (cmdType) { case "Create": // TODO case "Deposit": // TODO case "SetInterest": // TODO case "GetInterest": // TODO } //TODO: Read command } Check your solution here: https://judge.softuni.bg/Contests/1517/Defining-Classes-Lab
  • 37.
     …  … … Summary 37  Classes define specific structure for objects  Objects are particular instances of a class  Classes define fields, methods, constructors and other members  Constructors are invoked when creating new class instances  Constructors initialize the object's initial state
  • 38.
  • 39.
  • 40.
  • 41.
     Software University– High-Quality Education and Employment Opportunities  softuni.bg  Software University Foundation  http://softuni.foundation/  Software University @ Facebook  facebook.com/SoftwareUniversity  Software University Forums  forum.softuni.bg Trainings @ Software University (SoftUni)
  • 42.
     This course(slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution-NonCom mercial-ShareAlike 4.0 International" license License 42