Desktop Application Development
MOHAMMAD SALIM HAMDARD
KABUL UNIVERSITY Monday, July 29, 2024 1
Lecture 3
OBJECT AND CLASS
KABUL UNIVERSITY Monday, July 29, 2024 2
OUTCOMES
Should know about Objects in Java and General
Should Learn about Classes in Java and General
Could use Setter / Getter Methods
Know about Primitive vs Reference type
Initializing object with constructor
Static field and static method
Should do Practical class activity
Know about Graphical Boxes in Java
WHAT ARE OBJECTS
Any thing of real life about which we can stored data can be an object.
Almost any noun can be reasonably represented as a software object in
terms of:
◦ Attributes (e.g., name, color and size) and
◦ Behaviors (e.g., calculating, moving and communicating).
object-oriented programs are often easier to understand, correct and modify.
CLASSES
Class is nothing but a collection of object of similar type for example class of object
in which each student is an object.
You need to know how to handle and manage classes
The instance of a class is called object
Each class can have different instance variables and methods (Functions)
public class Car //class name
◦ {
◦ String color; //instance variable of the class
◦ }
THE CAR ANALOGY
A class can be defined as car’s blueprint design which contains the:
◦ The car attributes (color, model etc.)
◦ The methods (pedal , steering wheel etc.)
Instantiation:
◦ Building a car using the blueprint design
◦ Then we can make use of the behaviors etc.
SETTER AND GETTER METHODS
To assign a value to the instance variable we use setter methods
To get the value of the instance variable we use getter methods
Notice the camel case naming:
ACCOUNTTEST CLASS TO CREATE OBJECTS
Using test class we can create objects of class Account and to access the
methods of the class.
The class Account does not have main method, hence, it will not get
executed directly unless called by another class
AccountTest class orders the Account class of what to do!
CLASS ACCOUNTTEST
PRIMITIVE TYPES VS REFERENCE TYPES
int, String etc. are primitive datatypes
◦ Can hold only one variable
All non primitive types are reference types
◦ Account acc=new Account();
◦ acc is reference to the Account object
INITIALIZE OBJECTS WITH CONSTRUCTORS
The constructor is called each time you are creating object so this is the ideal
point to initialize an object’s instance variables.
Constructor should have the same name as the class! (important)
GRAPHICAL BOXES FOR INPUT
CODE
METHODS STRUCTURE
STATIC METHODS
Static methods are also called class methods
We can get access to the static methods just by calling class name,
There is no need for object creation
ClassName.methodName(arguments)
Math.sqrt(900)
Static variables
static int a=8;
CALLING METHODS
There are three ways to call a method:
1. Using a method name by itself:
◦ maximum(a,b,c);
2. Using a variable that contains a reference to an object,
◦ myObject.maximum(a,b,c);
3. Using the class name and a dot (.)
◦ myClass.maximum(a,b,c);
METHODS WITH RETURNING VALUES
If the method is providing an output, you should define a return type, while
defining the method.
Example:
public int add(int a, int b){
◦ return a+b;
◦ }
public String setName(String name){
◦ return name;
◦ }
JAVA API
Java contains many predefined classes that are grouped into categories of related
classes called packages.
Together, these are known as the Java Application Programming Interface (Java API),
java.util;
javax.swing;
java.io
Etc.
VARIABLE SCOPE
Scope is the reach out of the variable in a program.
In Java, as in any programming language, each variable has a scope
Scopes in java can be as follows:
◦ Class Scope
◦ Method Scope
◦ Loop Scope
CLASS SCOPE
Each variable declared inside of a class's brackets ( {} ) with private access modifier
but outside of any method, has class scope.
As a result, these variables can be used everywhere in the class, but not
outside of it:
public class ClassScopeExample {
private int amount = 0;
public void exampleMethod() {
amount++;
}
public void anotherExampleMethod() {
int anotherAmount = amount + 4;
}
}
METHOD SCOPE
When a variable is declared inside a method, it has method scope and it will only
be valid inside the same method:
public class MethodScopeExample {
public void methodA() {
int area = 2;
}
public void methodB() {
// compiler error, area cannot be resolved to a variable
area = area + 2;
}
}
BRACKET SCOPE
We can provide block of code in our program, in which the variables will be
accessible within the block only.
public class BracketScopeExample {
public void mathOperationExample() {
Integer sum = 0;
{
Integer number = 2;
sum = sum + number;
}
// compiler error, number cannot be solved as a variable
number++;
}
}
Questions?
KABUL UNIVERSITY Monday, July 29, 2024 23