INTRODUCTION TO
CLASSES
Objects, Methods and Strings
Ligjerues: Naim Sulejmani
PREDIFINED CLASS - USED
• System, System.out classes with its methods (print, println, printf, etc)
• Scanner with methods [nextLine(), nextInt(), nextDouble(),etc.]
Create your own datatype:
• Class -> its your own defined datatypes
• Create variables
• Create objects
OOP (OBJECT-ORIENTED
PROGRAMMING)
Object means a real-world entity such as a pen, chair, table, computer, watch, me, you,
monitor, laptop, window, door, projector, etc.
Object-Oriented Programming is a methodology or paradigm to design a program using
classes and objects. It simplifies the software development and maintenance by providing
some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
OBJECT ORIENTED
PROGRAMMING
Object
Object
Object
OOP LANGUAGES
Objective-C C# Visual Basic
Python
C++
Ruby
Java
JavaScript
Scala
PHP
Perl R
Perl
JAVA VS REAL LIFE OOP
PROBLEMS
Map
Road
Car
Java Real Life
PES – 2019 PLAYERS
POKEMON GAME
POKEMON
Name: Pikachu
Type: Electric
Fields
Health: 70
attack()
Methods dodge()
evolve()
POKEMON CLASS
POKEMON OBJECTS
VARIABLE TYPES
int age; Primitive
double score; variables
Pokemon pikachu; Object
Item incubator; variables
int Pokemon
pikachu, electric,
70
age 10
WHY PEOPLE CHOOSE THOSE
PRIMITIVE TYPES !?
Objects is made up from primitive types
Object
1. name: pikachu
Primitive 2. type: Electric
variables 3. health: 70
Sorry primitive types, you do matter!!!
WHY USE OBJECTS
An object is an identifiable entity with some characteristics, state and behavior.
An object is simply a real-world entity.
For example, we can say ‘Orange’ is an object. Its characteristics are: spherical
in shape and color is orange. Its behavior is: juicy and tastes sweet-sour.
Objects combine variables together to make your code meaningful
Code becomes organized and easier to understand
Maintaining your code becomes much simple
JAVA won’t work without objects!
Even main() methods should be inside a class
CLASS & OBJECTS
A class is a group of objects that share common properties and behavior.
A class can also be defined as a blueprint from which you can create an individual object.
Class doesn't consume any space.
Vehicle
String color
int power
int seats
For example, we can consider a car as a class that has characteristics like steering wheels, seats,
brakes, etc. And its behavior is mobility. But we can say Honda City having a reg.number 4654 is
an ‘object’ that belongs to the class ‘car’.
CLASS & OBJECTS – CONT.
CLASS & OBJECTS – CONT.
JAVA CLASS
The core element of Object orientation in Java is the class.
A class is often defined as the blueprint or template for an object.
We can create multiple objects from a class.
It is a logical entity that does not occupy any space/memory.
Memory is allocated when we create the objects of a class type.
A class contains properties and methods to define the state and behavior of its object.
It defines the data and the methods that act upon that data.
A class defines the shared characteristics like –
The set of attributes/properties
The set of behavior or methods or actions
How to construct an object
JAVA CLASS & OBJECTS
JAVA CLASS & OBJECTS
JAVA CLASS & OBJECTS
JAVA CLASS & OBJECTS
CLASS & OBJECTS
COMPARISON
A class can also be defined as a blueprint from which you can create an individual object.
Class doesn't consume any space.
Objects are to classes, what variables are to Data Types
Class Object
What A Data Type A Variable
Where Has its own file Scattered around the project
Why Defines the structure Used to implement to logic
Used as data (information)
Naming CamelCase (starts with an camelCase (start with a lower case)
conventions upper case)
Examples Country kosovo
Book pse
Pokemon pikachu
Student resulSelmani
Professor naimSulejmani
EVERYTHING IS AN OBJECT IN
JAVA
Class Primitive type
Integer int
Long long
Double double
Character char
String char[]
String is made up of an array of char[] as its fields, but as an object means that is also offers
some powerful methods like “length(), equals(), etc.”
Each class is inherited from Object class.
OBJECT
Any entity that has state and behavior is known as an object.
An Object can be defined as an instance of a class.
An object contains an address and takes up some space in memory. Objects can communicate
without knowing the details of each other's data or code. The only necessary thing is the type
of message accepted and the type of response returned by the objects.
Example: A dog is an object because it has states
like color, name, breed, etc. as well as behaviors
like wagging the tail, barking, eating, etc.
Some other examples
CLASS
1. <access-modifier> class <ClassName>
Class declaration 2. {
public class ClassName 3. //Class body containing variables and methods
Constructor: public ClassName
4. }
Instance variable
private String name;
Access Modifiers public (+) & private (-)
Methods for set & get
ACCOUNT CLASS EXAMPLE
ACCOUNT CLASS EXAMPLE
HOW TO CREATE OBJECT
FROM CLASS
With keyword new.
Example creating an object from Scanner?
More to follow next lecture
EXAMPLES
1. Account Class -> naimi’s account – llogaria ne facebook. Llogaria ne twitter,
2. Futboll Player Class -> ronaldo, messi
3. Trainer Class -> zidane, guardiola
4. Pokemon Class -> Pikachu ose ndonje filmi vizatimore
5. Car/Vehicle Class -> Mercedes benz, bmw, vw, etc.
6. Student Class -> arber, elona
7. Professor Class -> naim, rrezarta
1. For each of these class add the variables(instance variables), constructors, get/set methods, and
initialize object, populate and show in console/terminal
X. ACCOUNT CLASS – BOOK
EXAMPLE (BASIC)
1. Instance variable – name, balance
2. Constructors = default, with parameters
3. Properties (methods) – getters / setters
4. Methods – deposit(),
1. SIMULATE BANK CARD
1. Instance variables Account Number, Card Number, Holder Number, Valid Throug, PIN,
Security Code, Card type, Bank Name
2. Constructors:
3. Methods: deposit, withdraw, checkBalance, print