SlideShare a Scribd company logo
1 of 42
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

More Related Content

What's hot

14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining ClassesIntro C# Book
ย 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
ย 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaSanjeev Tripathi
ย 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsSvetlin Nakov
ย 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm ComplexityIntro C# Book
ย 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variablesIntro C# Book
ย 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism Intro C# Book
ย 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and ClassesSvetlin Nakov
ย 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOPSunil OS
ย 
09. Java Methods
09. Java Methods09. Java Methods
09. Java MethodsIntro C# Book
ย 
Basic c#
Basic c#Basic c#
Basic c#kishore4268
ย 
C++ oop
C++ oopC++ oop
C++ oopSunil OS
ย 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processingIntro C# Book
ย 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and MapsIntro C# Book
ย 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: MethodsSvetlin Nakov
ย 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
ย 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3mohamedsamyali
ย 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4mohamedsamyali
ย 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionSvetlin Nakov
ย 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
ย 

What's hot (20)

14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
ย 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
ย 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
ย 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, Loops
ย 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
ย 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
ย 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
ย 
Java Foundations: Objects and Classes
Java Foundations: Objects and ClassesJava Foundations: Objects and Classes
Java Foundations: Objects and Classes
ย 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
ย 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
ย 
Basic c#
Basic c#Basic c#
Basic c#
ย 
C++ oop
C++ oopC++ oop
C++ oop
ย 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
ย 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
ย 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
ย 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
ย 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
ย 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
ย 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
ย 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
ย 

Similar to 14. Java defining classes

Unit4_2.pdf
Unit4_2.pdfUnit4_2.pdf
Unit4_2.pdfElakkiyaE8
ย 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
ย 
Ch03
Ch03Ch03
Ch03ojac wdaj
ย 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_typesAbed Bukhari
ย 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2Aram Mohammed
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsKamal Acharya
ย 
Java cฤƒn bแบฃn - Chapter4
Java cฤƒn bแบฃn - Chapter4Java cฤƒn bแบฃn - Chapter4
Java cฤƒn bแบฃn - Chapter4Vince Vo
ย 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
ย 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
ย 
ADG Poznaล„ - Kotlin for Android developers
ADG Poznaล„ - Kotlin for Android developersADG Poznaล„ - Kotlin for Android developers
ADG Poznaล„ - Kotlin for Android developersBartosz Kosarzycki
ย 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
ย 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptSeok-joon Yun
ย 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder paramisoft
ย 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerSrikanth Shreenivas
ย 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptxarjun431527
ย 
C++ L10-Inheritance
C++ L10-InheritanceC++ L10-Inheritance
C++ L10-InheritanceMohammad Shaker
ย 
How do you create a programming language for the JVM?
How do you create a programming language for the JVM?How do you create a programming language for the JVM?
How do you create a programming language for the JVM?Federico Tomassetti
ย 

Similar to 14. Java defining classes (20)

Unit4_2.pdf
Unit4_2.pdfUnit4_2.pdf
Unit4_2.pdf
ย 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
ย 
Ch03
Ch03Ch03
Ch03
ย 
Ch03
Ch03Ch03
Ch03
ย 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
ย 
Java doc Pr ITM2
Java doc Pr ITM2Java doc Pr ITM2
Java doc Pr ITM2
ย 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
ย 
Java cฤƒn bแบฃn - Chapter4
Java cฤƒn bแบฃn - Chapter4Java cฤƒn bแบฃn - Chapter4
Java cฤƒn bแบฃn - Chapter4
ย 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
ย 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
ย 
ADG Poznaล„ - Kotlin for Android developers
ADG Poznaล„ - Kotlin for Android developersADG Poznaล„ - Kotlin for Android developers
ADG Poznaล„ - Kotlin for Android developers
ย 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
ย 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
ย 
OOP and C++Classes
OOP and C++ClassesOOP and C++Classes
OOP and C++Classes
ย 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
ย 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder
ย 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better Programmer
ย 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
ย 
C++ L10-Inheritance
C++ L10-InheritanceC++ L10-Inheritance
C++ L10-Inheritance
ย 
How do you create a programming language for the JVM?
How do you create a programming language for the JVM?How do you create a programming language for the JVM?
How do you create a programming language for the JVM?
ย 

More from Intro C# Book

17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversalIntro C# Book
ย 
Java Problem solving
Java Problem solving Java Problem solving
Java Problem solving Intro C# Book
ย 
21. Java High Quality Programming Code
21. Java High Quality Programming Code21. Java High Quality Programming Code
21. Java High Quality Programming CodeIntro C# Book
ย 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexityIntro C# Book
ย 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arraysIntro C# Book
ย 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queuesIntro C# Book
ย 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handlingIntro C# Book
ย 
01. Introduction to programming with java
01. Introduction to programming with java01. Introduction to programming with java
01. Introduction to programming with javaIntro C# Book
ย 
23. Methodology of Problem Solving
23. Methodology of Problem Solving23. Methodology of Problem Solving
23. Methodology of Problem SolvingIntro C# Book
ย 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQIntro C# Book
ย 
21. High-Quality Programming Code
21. High-Quality Programming Code21. High-Quality Programming Code
21. High-Quality Programming CodeIntro C# Book
ย 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and SetIntro C# Book
ย 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks QueuesIntro C# Book
ย 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like StructuresIntro C# Book
ย 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories Intro C# Book
ย 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text ProcessingIntro C# Book
ย 

More from Intro C# Book (16)

17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal17. Java data structures trees representation and traversal
17. Java data structures trees representation and traversal
ย 
Java Problem solving
Java Problem solving Java Problem solving
Java Problem solving
ย 
21. Java High Quality Programming Code
21. Java High Quality Programming Code21. Java High Quality Programming Code
21. Java High Quality Programming Code
ย 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
ย 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
ย 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
ย 
12. Java Exceptions and error handling
12. Java Exceptions and error handling12. Java Exceptions and error handling
12. Java Exceptions and error handling
ย 
01. Introduction to programming with java
01. Introduction to programming with java01. Introduction to programming with java
01. Introduction to programming with java
ย 
23. Methodology of Problem Solving
23. Methodology of Problem Solving23. Methodology of Problem Solving
23. Methodology of Problem Solving
ย 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
ย 
21. High-Quality Programming Code
21. High-Quality Programming Code21. High-Quality Programming Code
21. High-Quality Programming Code
ย 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
ย 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
ย 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
ย 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
ย 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
ย 

Recently uploaded

Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...
Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...
Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...Delhi Call girls
ย 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...tanu pandey
ย 
Microsoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftMicrosoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftAanSulistiyo
ย 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC
ย 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdfMatthew Sinclair
ย 
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...SUHANI PANDEY
ย 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdfMatthew Sinclair
ย 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...SUHANI PANDEY
ย 
Busty DesiโšกCall Girls in Vasundhara Ghaziabad >เผ’8448380779 Escort Service
Busty DesiโšกCall Girls in Vasundhara Ghaziabad >เผ’8448380779 Escort ServiceBusty DesiโšกCall Girls in Vasundhara Ghaziabad >เผ’8448380779 Escort Service
Busty DesiโšกCall Girls in Vasundhara Ghaziabad >เผ’8448380779 Escort ServiceDelhi Call girls
ย 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...tanu pandey
ย 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
ย 
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...nilamkumrai
ย 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfJOHNBEBONYAP1
ย 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubaikojalkojal131
ย 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...SUHANI PANDEY
ย 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...SUHANI PANDEY
ย 
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹nirzagarg
ย 

Recently uploaded (20)

Thalassery Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call G...
Thalassery Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call G...Thalassery Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call G...
Thalassery Escorts Service โ˜Ž๏ธ 6378878445 ( Sakshi Sinha ) High Profile Call G...
ย 
Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...
Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...
Hireโ† Young Call Girls in Tilak nagar (Delhi) โ˜Ž๏ธ 9205541914 โ˜Ž๏ธ Independent Esc...
ย 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
ย 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
ย 
Microsoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftMicrosoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck Microsoft
ย 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
ย 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
ย 
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
ย 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
ย 
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
ย 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
ย 
Busty DesiโšกCall Girls in Vasundhara Ghaziabad >เผ’8448380779 Escort Service
Busty DesiโšกCall Girls in Vasundhara Ghaziabad >เผ’8448380779 Escort ServiceBusty DesiโšกCall Girls in Vasundhara Ghaziabad >เผ’8448380779 Escort Service
Busty DesiโšกCall Girls in Vasundhara Ghaziabad >เผ’8448380779 Escort Service
ย 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
ย 
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
ย 
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls ๐ŸŽ—๏ธ 9352988975 Sizzling | Escorts | Girls Are Re...
ย 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
ย 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
ย 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
ย 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
ย 
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
๐Ÿ’š๐Ÿ˜‹ Salem Escort Service Call Girls, 9352852248 โ‚น5000 To 25K With AC๐Ÿ’š๐Ÿ˜‹
ย 

14. Java defining classes

  • 1. Classes, Fields, Constructors, Methods Defining Classes Software University http://softuni.bg SoftUni Team Technical Trainers
  • 2. 1. Defining Simple Classes 2. Fields 3. Methods 4. Constructors, Keyword this 5. Static Members Table of Contents 2
  • 5. ๏‚ง 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
  • 6. Naming Classes ๏‚ง Use PascalCase naming ๏‚ง Use descriptive nouns ๏‚ง Avoid ambiguous names 6 class Dice { โ€ฆ } class BankAccount { โ€ฆ } class IntegerCalculator { โ€ฆ } class TPMF { โ€ฆ } class bankaccount { โ€ฆ } class numcalc { โ€ฆ }
  • 7. 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
  • 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 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
  • 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. Class Data Storing Data Inside a Class
  • 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 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
  • 14. Solution: Define Car Class 14 public class Car { String make; String model; int horsePower; }
  • 15. ๏‚ง 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!
  • 17. ๏‚ง Store executable code (algorithm) that manipulate state Methods 17 class Car { private int horsePower; public void increaseHP(int value) { horsePower += value; } }
  • 18. ๏‚ง 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
  • 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 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
  • 21. 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
  • 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 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
  • 26. ๏‚ง 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
  • 27. ๏‚ง 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; } }
  • 28. ๏‚ง 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
  • 29. 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
  • 31. ๏‚ง 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
  • 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 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
  • 34. 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
  • 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
  • 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