Chapter Three-I: Classes and Objects in Java
Chapter Three-I: Classes and Objects in Java
Chapter Three-I: Classes and Objects in Java
Programming (OOP)
CHAPTER THREE-I
02/10/22 3
Example –class song
02/10/22 4
Example-class student
• An object which recorded information about a University
of Gondar student might record a first name, a last name
and an identification number.
• We would represent this as follows in Java.
class Student {
public String firstName;
public String lastName;
public String idNumber;
}
02/10/22 5
Example-class student
02/10/22 7
class variable
• A class variable or static variable is defined in a class, but
there is only one copy regardless of how many objects are
created from that class.
• It's common to define static final variables (constants) that
can be used by all methods, or by other classes.
02/10/22 8
Definition and declaration of a class:
Syntax:
class ClassName{
//member attributes (instance or class variables)
//member Methods
}
02/10/22 9
Definition and declaration of a class:
• A class definition defines the class blueprint.
• The behaviors/operations of a class are implemented
methods. Also known as member functions
• The state of the class is stored in its data members. Also
known as fields, attributes, or instance variables
02/10/22 10
Class methods
• Objects are sent messages which in turn call methods.
• Methods may be passed arguments and may return something
as well.
• Methods are available to all instances of the class.
• Like data members, methods are also referenced using the dot
operator.
• A class definition typically includes one or more methods,
which carry out some action with the data.
• Methods resemble the functions and subroutines in other
languages.
• A method will be called, or invoked, by code in some other
method.
02/10/22 11
Class methods
• The structure of a method includes a method signature and a
code body:
13
Example
class Circle{
float r;
void showArea(){
float area = r*r*PI;
System.out.println(“The area is:” + area);
}
void showCircumference(){
float circum=2* PI*r;
System.out.println(“The circumference is:” +
circum);
}
...}
02/10/22 14
Objects
An object is an instance of a class. It can be uniquely identified by its
name and defined state, represented by the values of its attributes in a
particular time.
An object represents something with which we can interact in a
program.
A class represents a concept, and an object represents the
embodiment of a class. A class is a blueprint for an object.
• An Object has two primary components:
• state – properties of the object
• behavior – operations the object can perform
EXAMPLE
object state behavior
dog breed, isHungry eat, bark
grade book grades mean, median
02/10/22
light on/off switch 15
Constructing Objects
• We use the new keyword to construct a new instance of an Object
• We can assign this instance to a variable with the same type of the
Object
• Note: classes define new datatypes !
int id;//data member (also instance variable)
String name;//data member(also instance variable)
public static void main(String args[]){
Student s=new Student();//creating object of Student
System.out.println(s.id); //output=?
System.out.println(s.name); //output=?
}
}
02/10/22 19
We can create multiple objects by one type only as we
do in case of primitives.
i.e like int x,y,z; in primitves
We can say Student s1,s2,s3;
02/10/22 20
Class activity 1
Create a rectangle class that calculate
the area of rectangle.
Create an object from the rectangle
class to access class members.
Create an insert method to insert width
and length of a rectangle.
Create calculateArea method to
compute the area of a rectangle
respectively.
02/10/22 21
class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;
}
02/10/22 22
void calculateArea(){
System.out.println(length*width);
}
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea(); } }
02/10/22 23
Visibility Modifiers
The following are common visibility modifiers
Public
Default
Private
Protected
You can use the public visibility modifier for classes,
methods, and data fields to denote that they can be
accessed from any other classes(even outside package).
• If no visibility modifier is used, then by default the
classes, methods, and data fields are accessible by any
class in the same package.
• Also called default modifier.
02/10/22 24
• The private modifier makes methods and data fields accessible only
from within its own class.
• It indicates that instance variables are accessible only to methods of
the class; this is known as data hiding
• The get and set methods are used to read and modify private properties.
• The private modifier restricts access to its defining class
• The default modifier restricts access to a package,
• The public modifier enables unrestricted access.
• If a class is not defined public, it can be accessed only within the same
package.
02/10/22 25
• protected : Often it is desirable to allow subclasses to
access data fields or methods defined in the superclass,
but not allow non subclasses to access these data fields
and methods.
02/10/22 26
Summery on visibility modifiers
Accessible to: public protected Package private
(default)
Same Class Ye s Ye s Ye s Ye s
Class in package Ye s Ye s Ye s No
Subclass in Ye s Ye s No No
different package
Non-subclass Ye s No No No
different package
02/10/22 27
The static keyword
• The static keyword is used when a member variable of a class has to be
shared between all the instances of the class.
• All static variables and methods belong to the class and not to any
instance of the class
• When can we access static variable?
When a class is loaded by the virtual machine all the static variables
and methods are available for use.
Hence we don’t need to create any instance of the class for using the
static variables or methods.
Variables which don’t have static keyword in the definition are
implicitly non static.
02/10/22 28
Example
Class staticDemo{
public static int a = 100; // All instances of staticDemo have this variable as a common variable
public int b =2 ;
public static showA(){
System.out.println(“A is “+a); } }
public static void main(String args[]){
staticDemo.a = 35; // when we use the class name, the class is
loaded, direct access to a without any instance
staticDemo.b=22; // ERROR this is not valid for non static variable
staticDemo demo = new staticDemo();
demo.b = 200; // valid to set a value for a non static variable after
creating an instance.
staticDemo.showA(); //prints 35
}}
02/10/22 29
Static and Non-static
• We can access static variables without creating an instance of the
class
• As they are already available at class loading time, we can use them
in any of our non static methods.
• We cannot use non static methods and variables without creating an
instance of the class as they are bound to the instance of the class.
• They are initialized by the constructor when we create the object
using new operator.
02/10/22 30
final fields
• The keyword final means: once the value is set, it can never be
changed.
31
Any ?
02/10/22 32