SlideShare a Scribd company logo
1 of 21
B Y :
H U M A S A M I N
Object Oriented Programming
Concepts
Class and Object
 Class is a user defined datatype.
 The class definition provides a template or blueprint,
which describes
 the data (instance variables) contained within, and
 the behavior (methods) common to all objects of a class.
 Object is a variable of a class.
Syntax of class
class ClassName
{
//data or instance variables
//behavior or methods
}
Data or Instance Variables
 The data is contained in variables defined within the
class
 Often called instance variables, data members,
properties or attributes.
 The instance variables are variables of the primitive
types or they can be reference variables of objects of
other classes.
Example
class Student
{
//data members or instance variables
int rollno;
String name;
int semester;
int[] marks;
//behavior or methods
}
Behavior or Methods
 The behavior is controlled by methods defined
within the class.
 Often called member methods or member functions.
 Syntax:
returntype methodName(parameterlist)
{
//valid java statements
}
Example
class Student
{
//data members or instance variables
int rollno;
String name;
int semester;
//behavior or methods
void displayValues( )
{
System.out.println(rollno);
System.out.println(name);
System.out.println(semester);
}
}
Object
 Object is a variable of a class.
 Object is the implementation of class.
 It is a software bundle of variables and methods.
 It is also known as instance of a class.
 The members of the class both data members and
methods are accessed with the help of the object.
Declaration and Definition of Object
 Syntax:
 Declaration of object:
ClassName objectName;
 Definition:
ObjectName=new ClassName( );
 Shortcut:
 ClassName objectName=new ClassName( );
Example
 Objects are created in the main method or any other
class.
public class StudentDriver
{
public static void main(String args[])
{
Student s;
s=new Student( );
}
}
Accessing Members of the class
 The members of the class(both data members and
methods) are accessed with the help of the object of
the class.
 Syntax:
objectName.instanceVariable=value;
OR
objectName.methodName();
Access Modifiers or Access Specifiers
Access
Specifier
Class Package SubClass World
private Y N N N
package Y Y N N
protected Y Y Y N
public Y Y Y Y
Example
public class Student
{
//data members or instance variables
private int rollNo;
public int semester;
//behavior or methods are kept public
public void displayValues( )
{
System.out.println(rollno);
System.out.println(name);
System.out.println(semester);
}
}
Note: Data Members are kept private and methods are kept public
Example Continued..
public class StudentDriver
{
public static void main(String args[])
{
Student s;
s=new Student( );
s.semester=2;
s.rollNo=123; //Not Allowed as its private
s.displayValues( );
}
}
Accessing private members
 To access the private members of the class, we have
to provide getter and setter methods in the class.
 The getters and setters have public access specifier.
 If x & y are the instance variables then for setters,
word “set” is used before the instance variable name
like setX, setY.
 For getters, word “get” is used before the instance
variable name getX, getY.
Example
public class Student
{
//data members or instance variables
private int rollNo;
public int semester;
//behavior or methods are kept public
public void setrRollNo(int r)
{
rollNo=r;
}
public int getRollNo( )
{
return rollNo;
}
public void displayValues( )
{
System.out.println(rollNo);
System.out.println(semester);
}
}
Example cont..
public class StudentDriver
{
public static void main(String args[])
{
Student s;
s=new Student( );
s.semester=2;
System.out.println(s.semester);
s.setrollNo(123);
System.out.println(s.getrollNo( ));
int r;
r=s.getrollNo();
System.out.println(r);
s.displayValues( );
}
}
Constructors
 Constructor is a special kind of method of the class
having the same name as that of the class and has a
no return type.
 It is called when an object of the class is going to be
created.
 The main purpose of writing constructor is
initialization of instance variables.
Constructor Example
public class Student
{
//data members or instance variables
private int rollNo;
public int semester;
//behavior or methods are kept public
//Constructor
public Student( )
{
rollNo=10;
semester=2;
}
public setrollNo(int r)
{
rollNo=r;
}
public int getrollNo( )
{
return rollNo;
}
public void displayValues( )
{
System.out.println(rollNo);
System.out.println(semester);
}
}
Constructor
 If you don’t provide constructor for class, the JVM
will provide a default (zero argument) constructor
and initialize the instance variables to default values.
Lab Work
 Write a program to create a class named Circle
having radius as a data member. The class should
contain two methods to calculate the area and
circumference of the circle.
 You have to create two objects of the Circle class and
display their area and circumference.

More Related Content

What's hot

ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#Adeel Rasheed
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaAdil Mehmoood
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objectsDeepak Singh
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with javaSujit Kumar
 
It 405 materi 4 objek dan kelas ii
It 405 materi 4   objek dan kelas iiIt 405 materi 4   objek dan kelas ii
It 405 materi 4 objek dan kelas iiAyi Purbasari
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 

What's hot (20)

ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
 
It 405 materi 4 objek dan kelas ii
It 405 materi 4   objek dan kelas iiIt 405 materi 4   objek dan kelas ii
It 405 materi 4 objek dan kelas ii
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java session4
Java session4Java session4
Java session4
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Class and objects
Class and objectsClass and objects
Class and objects
 

Viewers also liked

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming ConceptsMohamed Emam
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principlesmaznabili
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming ConceptsAbhigyan Singh Yadav
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS abhishek kumar
 
Advance Javascript for Coders
Advance Javascript for CodersAdvance Javascript for Coders
Advance Javascript for CodersPaddy Lock
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts246paa
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented ParadigmHüseyin Ergin
 
OOPS features using Objective C
OOPS features using Objective COOPS features using Objective C
OOPS features using Objective CTiyasi Acharya
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01Adil Kakakhel
 
Principles of object oriented programming
Principles of object oriented programmingPrinciples of object oriented programming
Principles of object oriented programmingAmogh Kalyanshetti
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 

Viewers also liked (16)

Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principles
 
Oops
OopsOops
Oops
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
concept of oops
concept of oopsconcept of oops
concept of oops
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Advance Javascript for Coders
Advance Javascript for CodersAdvance Javascript for Coders
Advance Javascript for Coders
 
Java Object Oriented Programming
Java Object Oriented Programming Java Object Oriented Programming
Java Object Oriented Programming
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented Paradigm
 
OOPS features using Objective C
OOPS features using Objective COOPS features using Objective C
OOPS features using Objective C
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
 
Principles of object oriented programming
Principles of object oriented programmingPrinciples of object oriented programming
Principles of object oriented programming
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 

Similar to OOP concepts

03_Objects and Classes in java.pdf
03_Objects and Classes in java.pdf03_Objects and Classes in java.pdf
03_Objects and Classes in java.pdfParameshwar Maddela
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptxVishwanathanS5
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Mahmoud Alfarra
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsHelen SagayaRaj
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesSakkaravarthiS1
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08Terry Yoast
 
3.Classes&Objects.pptx
3.Classes&Objects.pptx3.Classes&Objects.pptx
3.Classes&Objects.pptxPRABHUSOLOMON1
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 

Similar to OOP concepts (20)

03_Objects and Classes in java.pdf
03_Objects and Classes in java.pdf03_Objects and Classes in java.pdf
03_Objects and Classes in java.pdf
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Classes-and-Object.pptx
Classes-and-Object.pptxClasses-and-Object.pptx
Classes-and-Object.pptx
 
Object and class
Object and classObject and class
Object and class
 
Chap11
Chap11Chap11
Chap11
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
C++ classes
C++ classesC++ classes
C++ classes
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
3.Classes&Objects.pptx
3.Classes&Objects.pptx3.Classes&Objects.pptx
3.Classes&Objects.pptx
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 

Recently uploaded

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Recently uploaded (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

OOP concepts

  • 1. B Y : H U M A S A M I N Object Oriented Programming Concepts
  • 2. Class and Object  Class is a user defined datatype.  The class definition provides a template or blueprint, which describes  the data (instance variables) contained within, and  the behavior (methods) common to all objects of a class.  Object is a variable of a class.
  • 3. Syntax of class class ClassName { //data or instance variables //behavior or methods }
  • 4. Data or Instance Variables  The data is contained in variables defined within the class  Often called instance variables, data members, properties or attributes.  The instance variables are variables of the primitive types or they can be reference variables of objects of other classes.
  • 5. Example class Student { //data members or instance variables int rollno; String name; int semester; int[] marks; //behavior or methods }
  • 6. Behavior or Methods  The behavior is controlled by methods defined within the class.  Often called member methods or member functions.  Syntax: returntype methodName(parameterlist) { //valid java statements }
  • 7. Example class Student { //data members or instance variables int rollno; String name; int semester; //behavior or methods void displayValues( ) { System.out.println(rollno); System.out.println(name); System.out.println(semester); } }
  • 8. Object  Object is a variable of a class.  Object is the implementation of class.  It is a software bundle of variables and methods.  It is also known as instance of a class.  The members of the class both data members and methods are accessed with the help of the object.
  • 9. Declaration and Definition of Object  Syntax:  Declaration of object: ClassName objectName;  Definition: ObjectName=new ClassName( );  Shortcut:  ClassName objectName=new ClassName( );
  • 10. Example  Objects are created in the main method or any other class. public class StudentDriver { public static void main(String args[]) { Student s; s=new Student( ); } }
  • 11. Accessing Members of the class  The members of the class(both data members and methods) are accessed with the help of the object of the class.  Syntax: objectName.instanceVariable=value; OR objectName.methodName();
  • 12. Access Modifiers or Access Specifiers Access Specifier Class Package SubClass World private Y N N N package Y Y N N protected Y Y Y N public Y Y Y Y
  • 13. Example public class Student { //data members or instance variables private int rollNo; public int semester; //behavior or methods are kept public public void displayValues( ) { System.out.println(rollno); System.out.println(name); System.out.println(semester); } } Note: Data Members are kept private and methods are kept public
  • 14. Example Continued.. public class StudentDriver { public static void main(String args[]) { Student s; s=new Student( ); s.semester=2; s.rollNo=123; //Not Allowed as its private s.displayValues( ); } }
  • 15. Accessing private members  To access the private members of the class, we have to provide getter and setter methods in the class.  The getters and setters have public access specifier.  If x & y are the instance variables then for setters, word “set” is used before the instance variable name like setX, setY.  For getters, word “get” is used before the instance variable name getX, getY.
  • 16. Example public class Student { //data members or instance variables private int rollNo; public int semester; //behavior or methods are kept public public void setrRollNo(int r) { rollNo=r; } public int getRollNo( ) { return rollNo; } public void displayValues( ) { System.out.println(rollNo); System.out.println(semester); } }
  • 17. Example cont.. public class StudentDriver { public static void main(String args[]) { Student s; s=new Student( ); s.semester=2; System.out.println(s.semester); s.setrollNo(123); System.out.println(s.getrollNo( )); int r; r=s.getrollNo(); System.out.println(r); s.displayValues( ); } }
  • 18. Constructors  Constructor is a special kind of method of the class having the same name as that of the class and has a no return type.  It is called when an object of the class is going to be created.  The main purpose of writing constructor is initialization of instance variables.
  • 19. Constructor Example public class Student { //data members or instance variables private int rollNo; public int semester; //behavior or methods are kept public //Constructor public Student( ) { rollNo=10; semester=2; } public setrollNo(int r) { rollNo=r; } public int getrollNo( ) { return rollNo; } public void displayValues( ) { System.out.println(rollNo); System.out.println(semester); } }
  • 20. Constructor  If you don’t provide constructor for class, the JVM will provide a default (zero argument) constructor and initialize the instance variables to default values.
  • 21. Lab Work  Write a program to create a class named Circle having radius as a data member. The class should contain two methods to calculate the area and circumference of the circle.  You have to create two objects of the Circle class and display their area and circumference.