• Like
OOP concepts
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

OOP concepts

  • 33 views
Published

oops concept chapter

oops concept chapter

Published in Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
33
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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.