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
Lecture java continued
Lecture java continued
 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.
Lecture java continued
Lecture java continued
Lecture java continued
Access Members of a Class
 We can use the name of objects along with
the . operator to access members of a class.
For example,
Lecture java continued
 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()
Lecture java continued
Lecture java continued
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.
Lecture java continued
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

More Related Content

PPTX
Constructor in java
PPT
Java lec class, objects and constructors
PPTX
OOPS features using Objective C
PDF
ITFT-Classes and object in java
PPTX
Object Oriented Programming_Lecture 2
PPTX
Basics of oops concept
PPTX
‫‫Chapter4 Polymorphism
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Constructor in java
Java lec class, objects and constructors
OOPS features using Objective C
ITFT-Classes and object in java
Object Oriented Programming_Lecture 2
Basics of oops concept
‫‫Chapter4 Polymorphism
Basic Concepts of OOPs (Object Oriented Programming in Java)

What's hot (20)

PDF
Object Oriented Programming Concepts
PPT
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
PPT
Java oops PPT
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPTX
‫Object Oriented Programming_Lecture 3
PDF
Python Class | Python Programming | Python Tutorial | Edureka
PPTX
‫Chapter3 inheritance
PPTX
Object Oriented Programming with C#
PDF
Class and Objects in Java
PDF
CLASS & OBJECT IN JAVA
PDF
Python - object oriented
PPT
Lect 1-class and object
PPTX
Java(Polymorphism)
PDF
Lect 1-java object-classes
PDF
Classes and objects in java
PPT
Oops in Java
PPT
Oops concept in c#
PPTX
04. Review OOP with Java
Object Oriented Programming Concepts
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
Java oops PPT
[OOP - Lec 09,10,11] Class Members & their Accessing
‫Object Oriented Programming_Lecture 3
Python Class | Python Programming | Python Tutorial | Edureka
‫Chapter3 inheritance
Object Oriented Programming with C#
Class and Objects in Java
CLASS & OBJECT IN JAVA
Python - object oriented
Lect 1-class and object
Java(Polymorphism)
Lect 1-java object-classes
Classes and objects in java
Oops in Java
Oops concept in c#
04. Review OOP with Java
Ad

Similar to Lecture java continued (20)

PPT
Lecture java continued 1
PPT
OOP_1_TEG
PDF
Class notes(week 3) on class objects and methods
DOCX
Class notes(week 3) on class objects and methods
PPT
Cso gaddis java_chapter6
PPTX
2 Object-oriented programghgrtrdwwe.pptx
PPT
Eo gaddis java_chapter_03_5e
PPT
Java class
PPTX
Pi j2.3 objects
PDF
Class and Objects in object-oriented programming with Java
PPT
Chapter 4 - Defining Your Own Classes - Part I
PPT
Java căn bản - Chapter4
PPT
Core Java unit no. 1 object and class ppt
PPTX
Classes, Inheritance ,Packages & Interfaces.pptx
PPT
Cso gaddis java_chapter6
PPTX
Pj01 x-classes and objects
PDF
PPT
9 cm604.14
PPTX
class as the basis.pptx
PPT
java
Lecture java continued 1
OOP_1_TEG
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
Cso gaddis java_chapter6
2 Object-oriented programghgrtrdwwe.pptx
Eo gaddis java_chapter_03_5e
Java class
Pi j2.3 objects
Class and Objects in object-oriented programming with Java
Chapter 4 - Defining Your Own Classes - Part I
Java căn bản - Chapter4
Core Java unit no. 1 object and class ppt
Classes, Inheritance ,Packages & Interfaces.pptx
Cso gaddis java_chapter6
Pj01 x-classes and objects
9 cm604.14
class as the basis.pptx
java
Ad

More from Kamran Zafar (11)

PPTX
DFA Assignment solution.pptx
PPTX
TOA_WEEK 14.pptx
PPTX
Drones in agri. by eskill india
PDF
exercise 3.3 solution /class 12th/ mathcity
PPTX
Lec 1 number systems converted
PDF
Lecture 5 binary_codes
PPT
Lecture 1 programming fundamentals (PF)
PPT
digital systems and information
PDF
02 computing magazine september 2012
PDF
Binary search tree operations
DOCX
Learn microsoft word from tayyab riaz
DFA Assignment solution.pptx
TOA_WEEK 14.pptx
Drones in agri. by eskill india
exercise 3.3 solution /class 12th/ mathcity
Lec 1 number systems converted
Lecture 5 binary_codes
Lecture 1 programming fundamentals (PF)
digital systems and information
02 computing magazine september 2012
Binary search tree operations
Learn microsoft word from tayyab riaz

Recently uploaded (20)

PDF
Complications of Minimal Access-Surgery.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
semiconductor packaging in vlsi design fab
PDF
Journal of Dental Science - UDMY (2021).pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
Virtual and Augmented Reality in Current Scenario
PPTX
Education and Perspectives of Education.pptx
PDF
Hazard Identification & Risk Assessment .pdf
Complications of Minimal Access-Surgery.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
A powerpoint presentation on the Revised K-10 Science Shaping Paper
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
semiconductor packaging in vlsi design fab
Journal of Dental Science - UDMY (2021).pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
AI-driven educational solutions for real-life interventions in the Philippine...
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
Core Concepts of Personalized Learning and Virtual Learning Environments
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Virtual and Augmented Reality in Current Scenario
Education and Perspectives of Education.pptx
Hazard Identification & Risk Assessment .pdf

Lecture java continued

  • 1. Object-Oriented Programming Dr. Riaz UlAmin Lecture : Introduction to OOP
  • 2. 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
  • 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 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.
  • 10. Access Members of a Class  We can use the name of objects along with the . operator to access members of a class. For example,
  • 12.  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()
  • 15. 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.
  • 17. 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: