OOP concepts

237 views

Published on

oops concept chapter

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
237
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

OOP concepts

  1. 1. B Y : H U M A S A M I N Object Oriented Programming Concepts
  2. 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. 3. Syntax of class class ClassName { //data or instance variables //behavior or methods }
  4. 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. 5. Example class Student { //data members or instance variables int rollno; String name; int semester; int[] marks; //behavior or methods }
  6. 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. 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. 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. 9. Declaration and Definition of Object  Syntax:  Declaration of object: ClassName objectName;  Definition: ObjectName=new ClassName( );  Shortcut:  ClassName objectName=new ClassName( );
  10. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.

×