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
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 

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
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

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.