SlideShare a Scribd company logo
1 of 47
Lecture-4
Instructor Name:
Object Oriented Programming
Today’s Lecture
 What is a class?
 Class Composition
 Components of Class
 Fields
 Methods
 Main() Method, Setter Method, Getter Method, immutator.
Calling Method in same class Main method
Calling Method in other class.
 Creating Objects from class
2
Class
What is a class?
 Assume you want to model a traffic simulation. One kind of entity that you
have to deal with is cars.
 What is a car in our context : Is it a class or object?
 A few questions helps us to make decision
 What color is a car? How fast it go? Where is it right now?
 If I ask question about “My car parked in University Parking”
 Now I am talking about Object and before it was a class.
3
Class
What is a class?
 In OOP we create general sketch for each kind of object and then we create
different instances using this sketch we call this sketch a class.
 All objects of same kind exhibit identical characteristics (information
structure and behavior) however they have data of their own.
 A class is a blueprint from which individual objects are created.
 A class is a concept
Class in OOP
 In object-oriented programming, a class is an extensible program-code-
template for creating objects, providing initial values for state (member
variables) and implementations of behavior (member functions or
methods).
4
Class
What is a class?
5
Class
What is a class?
6
Class
Class Representation?
We can represent a class using rectangle as follows
7
(Class Name)
(attributes)
(operations)
(Class Name)
Normal Form
Suppressed Form
Class
Class Representation Examples?
8
Circle
center
radius
draw
computeArea
Normal Form
Suppressed Form
Circle
Person
name
age
gender
eat
walk
Person
Class
Classes & Object
 A Java program consists of one or more classes
 A class is an abstract description of objects
 Here is example of class
public class Dog {
Inner part of class will go here
} braces define the scope of class
 Here are some objects of class
9
Reserved words
must be in lower
case
User defined name for
the class. conventionally
starts with upper letter
Class
Classes Header
 The text of classes can be broken down into two parts: a small outer wrapping
that simply names the class and the inner part that does all the work
(previous slide).
 the outer wrapping of different classes all look pretty much the same.
 The outer wrapping contains the class header, whose main purpose is to
provide a name for the class.
Class Activity
 Write out what you think the outer wrappers of the Student and LabClass
classes might look like; do not worry about the inner part.
10
Class
Components of a class?
 A class has following three components
1. Fields
2. Constructor
3. Methods
 Order is insignificant. We can write them in any order.
11
Class with blanks
public class Test
{
}
====================
public class Test()
{
int marks;
public setmarks(int i)
{
Marks=I;
}
}
12
13
public class Welcome
{
public static void main(String[] args)
{
System.out.prinln("Welcome to GCU");
}
}
Writing First Program
Java Virtual Machine (JVM)
What is JVM
 A Java virtual machine (JVM) is an abstract computing machine that enables
a computer to run a Java program.
 Java is interpreter
14
Java Virtual Machine (JVM)
Compile Once Run every where. Platform Independent 15
Java Virtual Machine (JVM)
16
Class
What is a Field?
 Also known as attributes. Another name is instance variable or data
 For each instance of the class (object), values of attributes can vary, hence
instance variable
 Consider the Example Person Class
Attributes: name, address, phone
 Consider the Objects of Person Class
 Imran Object
Name is Imran, address is Lahore, phone is 111222333
 Ali Object
Name is Ali, address is Karachi, phone is 111222333
 As the value of name, address, and phone belongs to objects that’s why it is
also called instance variable.
 This is what we also called as state of object
26
Class
Field Example
 Classes describes the data (fields) held by each of its object
public class Dog {
private String name;
private int age;
rest of class
}
 Usually fields are defined as private.
 The word private and public will be explained in the coming lectures.
27
Data Types
Instance variable /
Field name
Class
What is a Constructor?
 Constructor in java is a special type of method that is used to initialize the
object.
 Java constructor is invoked at the time of object creation.
 It constructs the values i.e. provides data for the object that is why it is
known as constructor.
 The constructors are responsible for ensuring that an object is set up properly
when it is first created.
 The construction process is also known as initialization.
 If a constructor is not defined in the class, Java will create a default
constructor that will invoke automatically when object is create. 28
Class
What is a Constructor?
 Constructor is called at the time of object creation.
 Once object has been created, the constructor plays no further role in that
object life and can not be called on it.
 One of the distinguished feature of constructor is that it has the same name as
the class in which they are defined.
 In the constructor the fields are initialized with default values or external
information passed into object at the time of creation.
29
Class
Constructor Rules and Example
 Constructor name must be same as class name.
 Constructor must have no explicit return type.
public class Dog {
private String name;
private int age;
public Dog(){
name=NULL;
age=0;
}
rest of class
}
30
Class
Types of Constructor
 There are two types of Constructor
1. Default Constructor (No arguments required)
A constructor that have no parameter (as in previous slide)
2. Parameterized Constructor
A constructor that have parameter. Example
public Dog(String dogname, int dogage){
name=dogname;
age=dogage;
}
Class Activity
 Can you guess what types some of the Book class’s fields might be, from the
parameters in its constructor? Can you assume anything about the names of
its fields? 31
Class
Exercise
 To what class the following constructor belong?
Public Student (String name)
 Suppose that the class Pet has a field called name that is of type String. Write
an assignment statement in the body of the following constructor so that the
name field will be initialized with the value of the constructor’s parameter.
public Pet(String petsName){
}
 Write the constructor header for the following constructor call.
Date(“September”,08,2015)
Try to give meaningful name to the parameters
32
Class
What is a Method?
 Method defines the behaviour or basic functionality of the class.
 Methods have two parts: a header and a body.
 Methods belong to a class
– Defined inside the class
 Heading
– Return type (e.g. int, float, void)
– Name (e.g. nextInt, println)
– Parameters (e.g. println(…) )
– More…
 Body
– enclosed in braces {}.
– Declarations and/or statements.
33
Class
Method Examples
public class Dog {
private String name;
private int age;
public Dog(){
name=NULL;
age=0;
}
public setName(String dogName){
name=dogName;
}
public String getName(){
return name;
}
}
34
Program Structure
35
• A program consists of
one or more classes
• Typically, each class is
in a separate .java file
File File
File
File
Class
Variables
Constructors
Methods
Variables
Variables
Statements
Statements
Program
Class
Accessor & Mutator Methods
 Accessor methods are those which returns information to the caller about the
state of the object; they provide access to information about the state of object
 Accessor usually contains the return statement in order to pass back that
information.
getName()
 Mutator method is one that change the state of an object.
 The most basic form of object is one that takes a single parameter whose
value is used to directly overwrite what is stored in the object’s field.
setName()
36
Class & Methods
Accessor Methods
37
Class & Methods
Mutator Methods
38
Class & Objects
Creating Objects from Class
 There are different ways to create an object of a class.
 Using Non Parameterized or Default constructor
Dog fido=new Dog();
 Using Parameterized Constructor
Dog fido=new Dog(“Fido”,5);
39
Writing & Running a Program
40
 When you write a program, you are writing classes and all the things
that go into classes
 Your program typically contains commands to create objects (that is,
“calls” to constructors)
– Analogy: A class is like a cookie cutter, objects are like cookies.
 When you run a program, it creates objects, and those objects interact
with one another and do whatever they do to cause something to
happen
– Analogy: Writing a program is like writing the rules to a game;
running a program is like actually playing the game
 You never know how well the rules are going to work until you try
them out
Writing & Running a Program
41
Getting Started
 Question: Where do objects come from?
– Answer: They are created by other objects.
 Question: Where does the first object come from?
– Answer: Programs have a special main method, not part of any object,
that is executed in order to get things started
public static void main(String[ ] args) {
Dog fido=new Dog(“Fido”,5); // creates a Dog
}
 The special keyword static says that the main method belongs to the class
itself, not to objects of the class
– Hence, the main method can be “called” before any objects are created
– Usually, the main method gets things started by creating one or more
objects and telling them what to do
Writing & Running a Program
42
A Complete Program
class Dog {
String name;
int age;
Dog(String n, int age) {
name = n;
this.age = age;
}
void bark() {
System.out.println("Woof!");
}
void wakeTheNeighbors( ) {
int i = 50;
while (i > 0) {
bark( );
i = i – 1;
}
}
public static void main(String[ ] args) {
Dog fido = new Dog("Fido", 5);
fido.wakeTheNeighbors();
}
} // ends the class
Home Assignment/ Self Review Exericse
43
1. Write out a class Person. Write out a constructor that should take two
parameters.
 The first is of type String and is called myName.
 The second is of type int and is called myAge.
 The first parameter should be used to set the value of a field called
name, and the second should set a field called age. You don’t have to
include the definitions for the fields, just the text of the constructor.
2. Write an accessor method called getName that returns the value of a field
called name, whose type is String.
3. Write a mutator method called setAge that takes a single parameter of type
int and sets the value of a field called age.
4. Write a method called printDetails for a class that has a field of type
String called name. The printDetails method should print out a string of
the form “The name of this person is”, followed by the value of the name
field. For instance, if the value of the name field is “Ali”, then printDetails
would print:
The name of this person is Ali
44
45
Writing First Program
public class Welcome
{
public static void main(String[] args)
{
System.out.prinln("Welcome to GCU");
}
}
46
Compiling and execution o your First Java Application
 Save the program with java extenstion
 Welcome.java
 Class name and file name must be same
 Compile the program with
 Javac Welcome.java
 Execute the program
 Java Welcome
 Java is case sensitive
 Comment allowed in JAVA with
 // or /* */
47
Compiling and execution o your First Java Application
 Open the command prompt window
 Change the directory where the program is strored
 To compile javac Welcome.java
 Execute the program with java Welcome

More Related Content

What's hot

Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
Object oriented architecture in erp
Object  oriented architecture in erpObject  oriented architecture in erp
Object oriented architecture in erpPreyanshu Saini
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan collegeahmed hmed
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Uzair Salman
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 augshashank12march
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
Class & Object - Intro
Class & Object - IntroClass & Object - Intro
Class & Object - IntroPRN USM
 

What's hot (20)

Object and class
Object and classObject and class
Object and class
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Object oriented architecture in erp
Object  oriented architecture in erpObject  oriented architecture in erp
Object oriented architecture in erp
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Oop java
Oop javaOop java
Oop java
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Pj01 x-classes and objects
Pj01 x-classes and objectsPj01 x-classes and objects
Pj01 x-classes and objects
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Class & Object - Intro
Class & Object - IntroClass & Object - Intro
Class & Object - Intro
 

Viewers also liked

Viewers also liked (19)

Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Introduction
IntroductionIntroduction
Introduction
 
Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptx
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 20-21
Lecture 20-21Lecture 20-21
Lecture 20-21
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
Impact of Fast Food
Impact of Fast FoodImpact of Fast Food
Impact of Fast Food
 
Why fast foods are bad for you?
Why fast foods are bad for you?Why fast foods are bad for you?
Why fast foods are bad for you?
 
Fast food
Fast foodFast food
Fast food
 

Similar to Lecture 4

03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.pptMikeAdva
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfArpitaJana28
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 

Similar to Lecture 4 (20)

Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Oops
OopsOops
Oops
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.ppt
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Unit - 3.pptx
Unit - 3.pptxUnit - 3.pptx
Unit - 3.pptx
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 

Recently uploaded

Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
thanksgiving dinner and more information
thanksgiving dinner and more informationthanksgiving dinner and more information
thanksgiving dinner and more informationlialiaskou00
 
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...Call Girls in Nagpur High Profile
 
Pesticide Calculation Review 2013 post.pptx
Pesticide Calculation Review 2013 post.pptxPesticide Calculation Review 2013 post.pptx
Pesticide Calculation Review 2013 post.pptxalfordglenn
 
NO1 Trending kala jadu karne wale ka contact number kala jadu karne wale baba...
NO1 Trending kala jadu karne wale ka contact number kala jadu karne wale baba...NO1 Trending kala jadu karne wale ka contact number kala jadu karne wale baba...
NO1 Trending kala jadu karne wale ka contact number kala jadu karne wale baba...Amil baba
 
VIP Russian Call Girls in Cuttack Deepika 8250192130 Independent Escort Servi...
VIP Russian Call Girls in Cuttack Deepika 8250192130 Independent Escort Servi...VIP Russian Call Girls in Cuttack Deepika 8250192130 Independent Escort Servi...
VIP Russian Call Girls in Cuttack Deepika 8250192130 Independent Escort Servi...Suhani Kapoor
 
Grade Eight Quarter 4_Week 6_Cookery.pptx
Grade Eight Quarter 4_Week 6_Cookery.pptxGrade Eight Quarter 4_Week 6_Cookery.pptx
Grade Eight Quarter 4_Week 6_Cookery.pptxKurtGardy
 
Week 5 Dessert Accompaniments (Cookery 9)
Week 5 Dessert Accompaniments (Cookery 9)Week 5 Dessert Accompaniments (Cookery 9)
Week 5 Dessert Accompaniments (Cookery 9)MAARLENEVIDENA
 
Ho Sexy Call Girl in Mira Road Bhayandar | ₹,7500 With Free Delivery, Kashimi...
Ho Sexy Call Girl in Mira Road Bhayandar | ₹,7500 With Free Delivery, Kashimi...Ho Sexy Call Girl in Mira Road Bhayandar | ₹,7500 With Free Delivery, Kashimi...
Ho Sexy Call Girl in Mira Road Bhayandar | ₹,7500 With Free Delivery, Kashimi...Pooja Nehwal
 
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCyLet Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCystephieert
 
High Class Call Girls Nashik Priya 7001305949 Independent Escort Service Nashik
High Class Call Girls Nashik Priya 7001305949 Independent Escort Service NashikHigh Class Call Girls Nashik Priya 7001305949 Independent Escort Service Nashik
High Class Call Girls Nashik Priya 7001305949 Independent Escort Service Nashikranjana rawat
 
Low Rate Call Girls Nagpur Esha Call 7001035870 Meet With Nagpur Escorts
Low Rate Call Girls Nagpur Esha Call 7001035870 Meet With Nagpur EscortsLow Rate Call Girls Nagpur Esha Call 7001035870 Meet With Nagpur Escorts
Low Rate Call Girls Nagpur Esha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Dubai Call Girls Drilled O525547819 Call Girls Dubai (Raphie)
Dubai Call Girls Drilled O525547819 Call Girls Dubai (Raphie)Dubai Call Girls Drilled O525547819 Call Girls Dubai (Raphie)
Dubai Call Girls Drilled O525547819 Call Girls Dubai (Raphie)kojalkojal131
 
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...Suhani Kapoor
 
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...Suhani Kapoor
 
Food & Nutrition Strategy Baseline (FNS.pdf)
Food & Nutrition Strategy Baseline (FNS.pdf)Food & Nutrition Strategy Baseline (FNS.pdf)
Food & Nutrition Strategy Baseline (FNS.pdf)Mohamed Miyir
 
(PRIYANKA) Katraj Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(PRIYANKA) Katraj Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...(PRIYANKA) Katraj Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(PRIYANKA) Katraj Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...ranjana rawat
 
(ASHA) Sb Road Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(ASHA) Sb Road Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(ASHA) Sb Road Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(ASHA) Sb Road Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 

Recently uploaded (20)

Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Ghitorni Delhi 💯Call Us 🔝8264348440🔝
 
thanksgiving dinner and more information
thanksgiving dinner and more informationthanksgiving dinner and more information
thanksgiving dinner and more information
 
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
VVIP Pune Call Girls Sinhagad Road (7001035870) Pune Escorts Nearby with Comp...
 
Pesticide Calculation Review 2013 post.pptx
Pesticide Calculation Review 2013 post.pptxPesticide Calculation Review 2013 post.pptx
Pesticide Calculation Review 2013 post.pptx
 
NO1 Trending kala jadu karne wale ka contact number kala jadu karne wale baba...
NO1 Trending kala jadu karne wale ka contact number kala jadu karne wale baba...NO1 Trending kala jadu karne wale ka contact number kala jadu karne wale baba...
NO1 Trending kala jadu karne wale ka contact number kala jadu karne wale baba...
 
VIP Russian Call Girls in Cuttack Deepika 8250192130 Independent Escort Servi...
VIP Russian Call Girls in Cuttack Deepika 8250192130 Independent Escort Servi...VIP Russian Call Girls in Cuttack Deepika 8250192130 Independent Escort Servi...
VIP Russian Call Girls in Cuttack Deepika 8250192130 Independent Escort Servi...
 
Grade Eight Quarter 4_Week 6_Cookery.pptx
Grade Eight Quarter 4_Week 6_Cookery.pptxGrade Eight Quarter 4_Week 6_Cookery.pptx
Grade Eight Quarter 4_Week 6_Cookery.pptx
 
Week 5 Dessert Accompaniments (Cookery 9)
Week 5 Dessert Accompaniments (Cookery 9)Week 5 Dessert Accompaniments (Cookery 9)
Week 5 Dessert Accompaniments (Cookery 9)
 
Ho Sexy Call Girl in Mira Road Bhayandar | ₹,7500 With Free Delivery, Kashimi...
Ho Sexy Call Girl in Mira Road Bhayandar | ₹,7500 With Free Delivery, Kashimi...Ho Sexy Call Girl in Mira Road Bhayandar | ₹,7500 With Free Delivery, Kashimi...
Ho Sexy Call Girl in Mira Road Bhayandar | ₹,7500 With Free Delivery, Kashimi...
 
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCyLet Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
Let Me Relax Dubai Russian Call girls O56338O268 Dubai Call girls AgenCy
 
High Class Call Girls Nashik Priya 7001305949 Independent Escort Service Nashik
High Class Call Girls Nashik Priya 7001305949 Independent Escort Service NashikHigh Class Call Girls Nashik Priya 7001305949 Independent Escort Service Nashik
High Class Call Girls Nashik Priya 7001305949 Independent Escort Service Nashik
 
Low Rate Call Girls Nagpur Esha Call 7001035870 Meet With Nagpur Escorts
Low Rate Call Girls Nagpur Esha Call 7001035870 Meet With Nagpur EscortsLow Rate Call Girls Nagpur Esha Call 7001035870 Meet With Nagpur Escorts
Low Rate Call Girls Nagpur Esha Call 7001035870 Meet With Nagpur Escorts
 
Dubai Call Girls Drilled O525547819 Call Girls Dubai (Raphie)
Dubai Call Girls Drilled O525547819 Call Girls Dubai (Raphie)Dubai Call Girls Drilled O525547819 Call Girls Dubai (Raphie)
Dubai Call Girls Drilled O525547819 Call Girls Dubai (Raphie)
 
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort serviceyoung Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
young Whatsapp Call Girls in Jamuna Vihar 🔝 9953056974 🔝 escort service
 
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
(PRIYA) Call Girls Budhwar Peth ( 7001035870 ) HI-Fi Pune Escorts Service
 
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
VIP Russian Call Girls Gorakhpur Chhaya 8250192130 Independent Escort Service...
 
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
VIP Russian Call Girls in Noida Deepika 8250192130 Independent Escort Service...
 
Food & Nutrition Strategy Baseline (FNS.pdf)
Food & Nutrition Strategy Baseline (FNS.pdf)Food & Nutrition Strategy Baseline (FNS.pdf)
Food & Nutrition Strategy Baseline (FNS.pdf)
 
(PRIYANKA) Katraj Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(PRIYANKA) Katraj Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...(PRIYANKA) Katraj Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
(PRIYANKA) Katraj Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune E...
 
(ASHA) Sb Road Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(ASHA) Sb Road Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(ASHA) Sb Road Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(ASHA) Sb Road Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 

Lecture 4

  • 2. Today’s Lecture  What is a class?  Class Composition  Components of Class  Fields  Methods  Main() Method, Setter Method, Getter Method, immutator. Calling Method in same class Main method Calling Method in other class.  Creating Objects from class 2
  • 3. Class What is a class?  Assume you want to model a traffic simulation. One kind of entity that you have to deal with is cars.  What is a car in our context : Is it a class or object?  A few questions helps us to make decision  What color is a car? How fast it go? Where is it right now?  If I ask question about “My car parked in University Parking”  Now I am talking about Object and before it was a class. 3
  • 4. Class What is a class?  In OOP we create general sketch for each kind of object and then we create different instances using this sketch we call this sketch a class.  All objects of same kind exhibit identical characteristics (information structure and behavior) however they have data of their own.  A class is a blueprint from which individual objects are created.  A class is a concept Class in OOP  In object-oriented programming, a class is an extensible program-code- template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). 4
  • 5. Class What is a class? 5
  • 6. Class What is a class? 6
  • 7. Class Class Representation? We can represent a class using rectangle as follows 7 (Class Name) (attributes) (operations) (Class Name) Normal Form Suppressed Form
  • 8. Class Class Representation Examples? 8 Circle center radius draw computeArea Normal Form Suppressed Form Circle Person name age gender eat walk Person
  • 9. Class Classes & Object  A Java program consists of one or more classes  A class is an abstract description of objects  Here is example of class public class Dog { Inner part of class will go here } braces define the scope of class  Here are some objects of class 9 Reserved words must be in lower case User defined name for the class. conventionally starts with upper letter
  • 10. Class Classes Header  The text of classes can be broken down into two parts: a small outer wrapping that simply names the class and the inner part that does all the work (previous slide).  the outer wrapping of different classes all look pretty much the same.  The outer wrapping contains the class header, whose main purpose is to provide a name for the class. Class Activity  Write out what you think the outer wrappers of the Student and LabClass classes might look like; do not worry about the inner part. 10
  • 11. Class Components of a class?  A class has following three components 1. Fields 2. Constructor 3. Methods  Order is insignificant. We can write them in any order. 11
  • 12. Class with blanks public class Test { } ==================== public class Test() { int marks; public setmarks(int i) { Marks=I; } } 12
  • 13. 13 public class Welcome { public static void main(String[] args) { System.out.prinln("Welcome to GCU"); } } Writing First Program
  • 14. Java Virtual Machine (JVM) What is JVM  A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program.  Java is interpreter 14
  • 15. Java Virtual Machine (JVM) Compile Once Run every where. Platform Independent 15
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. Class What is a Field?  Also known as attributes. Another name is instance variable or data  For each instance of the class (object), values of attributes can vary, hence instance variable  Consider the Example Person Class Attributes: name, address, phone  Consider the Objects of Person Class  Imran Object Name is Imran, address is Lahore, phone is 111222333  Ali Object Name is Ali, address is Karachi, phone is 111222333  As the value of name, address, and phone belongs to objects that’s why it is also called instance variable.  This is what we also called as state of object 26
  • 27. Class Field Example  Classes describes the data (fields) held by each of its object public class Dog { private String name; private int age; rest of class }  Usually fields are defined as private.  The word private and public will be explained in the coming lectures. 27 Data Types Instance variable / Field name
  • 28. Class What is a Constructor?  Constructor in java is a special type of method that is used to initialize the object.  Java constructor is invoked at the time of object creation.  It constructs the values i.e. provides data for the object that is why it is known as constructor.  The constructors are responsible for ensuring that an object is set up properly when it is first created.  The construction process is also known as initialization.  If a constructor is not defined in the class, Java will create a default constructor that will invoke automatically when object is create. 28
  • 29. Class What is a Constructor?  Constructor is called at the time of object creation.  Once object has been created, the constructor plays no further role in that object life and can not be called on it.  One of the distinguished feature of constructor is that it has the same name as the class in which they are defined.  In the constructor the fields are initialized with default values or external information passed into object at the time of creation. 29
  • 30. Class Constructor Rules and Example  Constructor name must be same as class name.  Constructor must have no explicit return type. public class Dog { private String name; private int age; public Dog(){ name=NULL; age=0; } rest of class } 30
  • 31. Class Types of Constructor  There are two types of Constructor 1. Default Constructor (No arguments required) A constructor that have no parameter (as in previous slide) 2. Parameterized Constructor A constructor that have parameter. Example public Dog(String dogname, int dogage){ name=dogname; age=dogage; } Class Activity  Can you guess what types some of the Book class’s fields might be, from the parameters in its constructor? Can you assume anything about the names of its fields? 31
  • 32. Class Exercise  To what class the following constructor belong? Public Student (String name)  Suppose that the class Pet has a field called name that is of type String. Write an assignment statement in the body of the following constructor so that the name field will be initialized with the value of the constructor’s parameter. public Pet(String petsName){ }  Write the constructor header for the following constructor call. Date(“September”,08,2015) Try to give meaningful name to the parameters 32
  • 33. Class What is a Method?  Method defines the behaviour or basic functionality of the class.  Methods have two parts: a header and a body.  Methods belong to a class – Defined inside the class  Heading – Return type (e.g. int, float, void) – Name (e.g. nextInt, println) – Parameters (e.g. println(…) ) – More…  Body – enclosed in braces {}. – Declarations and/or statements. 33
  • 34. Class Method Examples public class Dog { private String name; private int age; public Dog(){ name=NULL; age=0; } public setName(String dogName){ name=dogName; } public String getName(){ return name; } } 34
  • 35. Program Structure 35 • A program consists of one or more classes • Typically, each class is in a separate .java file File File File File Class Variables Constructors Methods Variables Variables Statements Statements Program
  • 36. Class Accessor & Mutator Methods  Accessor methods are those which returns information to the caller about the state of the object; they provide access to information about the state of object  Accessor usually contains the return statement in order to pass back that information. getName()  Mutator method is one that change the state of an object.  The most basic form of object is one that takes a single parameter whose value is used to directly overwrite what is stored in the object’s field. setName() 36
  • 39. Class & Objects Creating Objects from Class  There are different ways to create an object of a class.  Using Non Parameterized or Default constructor Dog fido=new Dog();  Using Parameterized Constructor Dog fido=new Dog(“Fido”,5); 39
  • 40. Writing & Running a Program 40  When you write a program, you are writing classes and all the things that go into classes  Your program typically contains commands to create objects (that is, “calls” to constructors) – Analogy: A class is like a cookie cutter, objects are like cookies.  When you run a program, it creates objects, and those objects interact with one another and do whatever they do to cause something to happen – Analogy: Writing a program is like writing the rules to a game; running a program is like actually playing the game  You never know how well the rules are going to work until you try them out
  • 41. Writing & Running a Program 41 Getting Started  Question: Where do objects come from? – Answer: They are created by other objects.  Question: Where does the first object come from? – Answer: Programs have a special main method, not part of any object, that is executed in order to get things started public static void main(String[ ] args) { Dog fido=new Dog(“Fido”,5); // creates a Dog }  The special keyword static says that the main method belongs to the class itself, not to objects of the class – Hence, the main method can be “called” before any objects are created – Usually, the main method gets things started by creating one or more objects and telling them what to do
  • 42. Writing & Running a Program 42 A Complete Program class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } void bark() { System.out.println("Woof!"); } void wakeTheNeighbors( ) { int i = 50; while (i > 0) { bark( ); i = i – 1; } } public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); fido.wakeTheNeighbors(); } } // ends the class
  • 43. Home Assignment/ Self Review Exericse 43 1. Write out a class Person. Write out a constructor that should take two parameters.  The first is of type String and is called myName.  The second is of type int and is called myAge.  The first parameter should be used to set the value of a field called name, and the second should set a field called age. You don’t have to include the definitions for the fields, just the text of the constructor. 2. Write an accessor method called getName that returns the value of a field called name, whose type is String. 3. Write a mutator method called setAge that takes a single parameter of type int and sets the value of a field called age. 4. Write a method called printDetails for a class that has a field of type String called name. The printDetails method should print out a string of the form “The name of this person is”, followed by the value of the name field. For instance, if the value of the name field is “Ali”, then printDetails would print: The name of this person is Ali
  • 44. 44
  • 45. 45 Writing First Program public class Welcome { public static void main(String[] args) { System.out.prinln("Welcome to GCU"); } }
  • 46. 46 Compiling and execution o your First Java Application  Save the program with java extenstion  Welcome.java  Class name and file name must be same  Compile the program with  Javac Welcome.java  Execute the program  Java Welcome  Java is case sensitive  Comment allowed in JAVA with  // or /* */
  • 47. 47 Compiling and execution o your First Java Application  Open the command prompt window  Change the directory where the program is strored  To compile javac Welcome.java  Execute the program with java Welcome

Editor's Notes

  1. No linking is done . Class file contain byte codoe