0% found this document useful (0 votes)
45 views

Object Oriented Programming 2

The document discusses object-oriented programming concepts in Java, including: - Classes define new data types through templates called objects that encapsulate members like data members (instance variables) and member functions. - Objects are created from classes using the new keyword and can access members through dot operators. Each object has its own copy of instance variables. - Examples demonstrate creating multiple objects of a class, accessing objects through reference variables, and using constructor functions that initialize upon object creation. Constructor overloading allows different constructor signatures.

Uploaded by

Pirzada Swati
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Object Oriented Programming 2

The document discusses object-oriented programming concepts in Java, including: - Classes define new data types through templates called objects that encapsulate members like data members (instance variables) and member functions. - Objects are created from classes using the new keyword and can access members through dot operators. Each object has its own copy of instance variables. - Examples demonstrate creating multiple objects of a class, accessing objects through reference variables, and using constructor functions that initialize upon object creation. Constructor overloading allows different constructor signatures.

Uploaded by

Pirzada Swati
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Object Oriented Programming

in Java
Problems in Structured
Programming

 No Restriction on global data


 No clear relationship between global
data and functions.
Principles of OOP
 Encapsulation
 Inheritance
 polymorphism
Class & Object
 Class is logical structure.
 Object is a physical entity.
 A class defines a new data type.
 A class is the template of its objects.
 A class encapsulates the members of the object.
 A class can have two types of members.
 Data Members
 Member functions
 Global variables declared inside the class are called
data members.
Class & Object
 The functions defined in side the class
are called member functions.
 All the members of a class are
accessible to the object of the class.
 An object can access any of it members
through dot(.) operator.
Class & Object
 Data members are also called instance
variable.
 When ever an object of the class is created a
separate copy of the data members is
created.
 Each object has a unique copy of data
members.
 Member functions are created once in
memory.
Example
1. class Example 1. class demo
2. { 2. {
3. int x,y; // Data Members 3. Public static void main(String as[])
4. void set(int a, int b) //Member 4. {
function 5. //creating object using new key word
5. { 6. Example Ob;
6. x=a; 7. Ob=new Example();
7. y=b; 8. //Example Ob=new Example();
9. // access member function
8. }
10. Ob.set(10,20);
9. void get() //Member function
11. Ob.get();
10. { 12. //setting the values of data members
11. System.out.println(“x=“+x+”y=“+y) directly.
; 13. Ob.x=5;
12. } 14. Ob.y=8;
13. } 15. Ob.get();
16. }
17. }
Example
1. class Example 1. class demo
2. { 2. {
3. int x,y; // Data Members 3. Public static void main(String as[])
4. void set(int a, int b) //Member 4. {
function 5. //creating object using new key
5. { word
6. x=a; 6. Example Ob=new Example();
7. y=b; 7. // access member function
8. } 8. Ob.set(10,20);
9. void get() //Member function 9. Ob.get();
10. { 10. //setting the values of data
11. System.out.println(“x=“+x+”y=“+y) members directly.
; 11. Ob.x=5;
12. } 12. Ob.y=8;
13. } 13. Ob.get();
14. }
15. }
Multiple objects Example
1. class Example 1. class demo
2. { 2. {
3. int x,y; // Data Members 3. Public static void main(String as[])
4. void set(int a, int b) //Member 4. {
function 5. Example Ob1=new Example();
5. { 6. Example Ob2=new Example();
6. x=a;
7. y=b; 7. // access member function
8. } 8. Ob1.set(10,20);
9. void get() //Member function 9. Ob2.set(30,40);
10. { 10. Ob1.get();
11. System.out.println(“x=“+x+”y=“+y) 11. Ob2.get();
;
12. }
12. }
13. }
13. }
Multiple Objects of a Class.
Accessing object through Multiple

Reference Variables.
Constructor Function

Constructor Overloading.
Multiple Objects of a Class.
 Multiple objects can be created for a class.
 Each object will be completely distinct
from other objects.
 A separate copy of the data members is
created for each object.
 An object cannot access the data
members of another object.
Class demo
Class Example {
{ Public static void main
int x; (String as[ ])
{
Void set(int a) Example ob1=new
{ Example();
Example ob2=new
x=a; Example();
} Ob1.set(10);
Ob2.set(30);
Void get() Ob1.get();
{ Ob2.get);
System.out.println(“ }
x=“ + x); }
Output:
} 10
} 30
Accessing object through
Multiple
 Reference variables are the variable of
class data type.
 A reference variable of a class can hold
the reference to the object of that class.
 We can access an object through
multiple reference variables.
Class demo Ob=ob1; // assign the
{ reference of object 1
Public static void Ob.get() // ob will access
main(String as[]) the members of ob1
{ Ob=ob2; // assign the
Example ob1=new reference of object 2
Example(); Ob.get(); // assign the
Example ob2=new reference of object 2
Example();
}}
Example ob;
Output:
Ob1.set(10);
10
Ob2.set(30);
Ob1.get(); 30
Ob2.get); 10
  30
Constructor Function

 Constructor function is a member function of a


class having:
 Same name as class name
 No return type
 Is called automatically at the time of creation of an object.
 Cannot be called like other member functions
 Is called only once in the life time of an object i.e. at the
time of creation.
 Is normally used to initialize the data members of an
object.
Constructor Function
Class demo
Class Example {
{ Public static void main(String as[])
int x; {
// the following function is Example ob1=new Example(); //
constructor function constructor function called for
Example() ob1
{ Example ob2=new Example();
X=0; constructor function called for 2
} Ob1.get();
Void set(int a) Ob2.get();
{ Ob1.set(10);
X=a; Ob2.set(30);
} Ob1.get();
Void get() Ob2.get);
{ }
System.out.println(“x=“ + x); }
} Output:
} 0
0
10
30
Constructor Overloading.

 Two or more than two constructor


functions having different number of
arguments or same number of
arguments with different data types is
called constructor overloading
Class Example
{
int x; Void get()
Float y; {
// the following function is no argument System.out.println(“x=“ + x + “y=“ + y);
constructor function }
Example() }
{ Class demo
X=0; {
} Public static void main (String as[])
//the following function is one argument {
constructor functionwith int argument Example ob1=new Example(); // no argumnet
Example(int a) constructor function called for ob1
{
X=a; Example ob2=new Example(20); one argument
Y=a; constructor function with int data type called
}
//the following function is one argument Example ob3=new Example(2.9); one argument
constructor functionwith one float argument constructor function withfloat data type
called
Example(float a)
{
Example ob4=new Example(20,30); two
X=a; argument constructor function called
Y=a;
}
//the following function is two argument Ob1.get(); X=0 y=0
constructor functionwith one int and one float
argument Ob2.get(); X=20
y=20.0000
Example(int a, float b) Ob3.get(); X=2 y=2.9
{
X=a; Ob4.get); X=20 y=30
Y=b; }
} }
Instance Variable Hiding
 The data members and local variable of a class
may have the same name.
 This phenomena creates a conflict for accessing
the exact data variable.
 The local variable of the function gets
precedence.
 It can be said the local variable of a function
hides the data members of the class
 This key word is used to resolve conflict.
1. class demo
1. class Example{ 2. {
2. int x; // data member named 3. Public static void main (String as[ ])
x 4. {
3. Void set(int x) // the local 5. Example Ob1=new Example();
varaibles of the function 6. Ob1.set(30);
named x 7. Ob1.get();
4. { 8. }
5. //x=x in correct assignment 9. }
but valid. 10. Output:
6. this.x=x; // this key word is 11. 30
used to refer current object
7. }
8. Void get(){
9. // no need to use this key
word here
10. System.out.println(“x=“ + x);
11. }}
Garbage Collection.
 A mechanism that periodically checks
un referenced objects in memory.
 It de allocates the memory allocated to
objects.

You might also like