Object-Oriented Programming
Dr. Riaz UlAmin
Lecture : Introduction to OOP
More about field modifiers (1)
 Access control modifiers
– private: private members are accessible only in the class
itself
– package: package members are accessible in classes in the
same package and the class itself
– protected: protected members are accessible in classes in
the same package, in subclasses of the class, and in the
class itself
– public: public members are accessible anywhere the class
is accessible
public class Pencil {
public String color = “red”;
public int length;
public float diameter;
private float price;
public static long nextID = 0;
public void setPrice (float newPrice) {
price = newPrice;
}
}
public class CreatePencil {
public static void main (String args[]){
Pencil p1 = new Pencil();
p1.price = 0.5f;
}
}
Pencil.java
CreatePencil.java
%> javac Pencil.java
%> javac CreatePencil.java
CreatePencil.java:4: price has private
access in Pencil
 An object is an instance of a class.
 A class is a template or blueprint from
which objects are created. So, an object is
the instance(result) of a class.
Access Members of a Class
 We can use the name of objects along with
the . operator to access members of a class.
For example,
 In the above example, we have created a class named
Bicycle. It includes a field named gear and a method
named braking(). Notice the statement,
 Bicycle sportsBicycle = new Bicycle(); Here, we have
created an object of Bicycle named sportsBicycle. We then
use the object to access the field and method of the class.
 sportsBicycle.gear - access the field gear
 sportsBicycle.braking() - access the method braking()
Constructors
 A constructor in Java is a special method
that is used to initialize objects. The
constructor is called when an object of a
class is created.
Constructor Parameters
 Constructors can also take parameters,
which is used to initialize attributes.
 The following example adds an int y
parameter to the constructor. Inside the
constructor we set x to y (x=y). When we
call the constructor, we pass a parameter to
the constructor (5), which will set the value
of x to 5:
Lecture java continued
Lecture java continued

Lecture java continued

  • 1.
    Object-Oriented Programming Dr. RiazUlAmin Lecture : Introduction to OOP
  • 2.
    More about fieldmodifiers (1)  Access control modifiers – private: private members are accessible only in the class itself – package: package members are accessible in classes in the same package and the class itself – protected: protected members are accessible in classes in the same package, in subclasses of the class, and in the class itself – public: public members are accessible anywhere the class is accessible
  • 3.
    public class Pencil{ public String color = “red”; public int length; public float diameter; private float price; public static long nextID = 0; public void setPrice (float newPrice) { price = newPrice; } } public class CreatePencil { public static void main (String args[]){ Pencil p1 = new Pencil(); p1.price = 0.5f; } } Pencil.java CreatePencil.java %> javac Pencil.java %> javac CreatePencil.java CreatePencil.java:4: price has private access in Pencil
  • 6.
     An objectis an instance of a class.  A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class.
  • 10.
    Access Members ofa Class  We can use the name of objects along with the . operator to access members of a class. For example,
  • 12.
     In theabove example, we have created a class named Bicycle. It includes a field named gear and a method named braking(). Notice the statement,  Bicycle sportsBicycle = new Bicycle(); Here, we have created an object of Bicycle named sportsBicycle. We then use the object to access the field and method of the class.  sportsBicycle.gear - access the field gear  sportsBicycle.braking() - access the method braking()
  • 15.
    Constructors  A constructorin Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.
  • 17.
    Constructor Parameters  Constructorscan also take parameters, which is used to initialize attributes.  The following example adds an int y parameter to the constructor. Inside the constructor we set x to y (x=y). When we call the constructor, we pass a parameter to the constructor (5), which will set the value of x to 5: