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

Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
Deepak Singh
 

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 Concepts
thinkphp
 

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

9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
Terry Yoast
 

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

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 

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.