0% found this document useful (0 votes)
8 views15 pages

Intro To Class and Object

The document discusses the differences between structured and object-oriented programming approaches, highlighting the advantages of encapsulation and data hiding in object-oriented programming. It explains how Java objects are stored in memory, emphasizing the need for dynamic memory allocation using the 'new' operator to instantiate classes. Additionally, it illustrates object creation and manipulation through examples of class and instance variables.

Uploaded by

pyvbvaraprasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

Intro To Class and Object

The document discusses the differences between structured and object-oriented programming approaches, highlighting the advantages of encapsulation and data hiding in object-oriented programming. It explains how Java objects are stored in memory, emphasizing the need for dynamic memory allocation using the 'new' operator to instantiate classes. Additionally, it illustrates object creation and manipulation through examples of class and instance variables.

Uploaded by

pyvbvaraprasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Structured Approach

disadvantages: No constructs for encapsulation, chances of code repetition, No


strong data hiding concept, difficult to debug
Object-oriented Approach
Class
Collection of objects is called class. It is a logical entity.

A class is a template for an object, and an object is an instance of a class

Collectively, the methods and variables defined within a class are called members of the class

Variables defined within a class are called instance variables because each instance
of the class contains its own copy of these variables
How are Java objects stored in memory?
• In Java, when we only declare a variable of a class type, this variable does not define an object. Only a
reference is created .
• Instead, it is simply a variable that can refer to an object.
• memory is not allocated for the object.
Class Box
{
void show()
{
System.out.println("Test::show() called");
}
} A variable in this state,
which currently references
public class Main { no object
public static void main(String[] args)
{
Box mybox;
mybox.show();

// Error here because mybox is not initialzed


}
}
Obtaining objects of a class is a two-step process
▪ Firstly, it is required to declare a variable of the class type
❖This variable doesn't define an object
❖It is simply a variable that can refer to an object.
▪ Secondly, it is required to acquire an actual, physical copy of
the object and assign it to that variable.
❖We can do this using the new operator.
❖The new operator dynamically allocates memory for
an object and returns a reference to it.

• The new operator instantiates a class by dynamically allocating(i.e, allocation at run


time) memory for a new object and returning a reference to that memory.
• This reference is then stored in the variable. Thus, in Java, all class objects must
be dynamically allocated.
• The new operator is also followed by a call to a class constructor, which initializes
the new object.
class Test { class Test {

void show() void show()


{ {
System.out.println("Test::show() called"); System.out.println("Test::show() called");
} }
} }

public class Main { public class Main {

public static void main(String[] args) public static void main(String[] args)
{ {
Test t;
Test t = new Test();
t.show(); t.show(); // No error
} }
} }
Important points :

 The above two statements can be rewritten as one statement.


Box mybox = new Box();
 The reference returned by the new operator does not have to be assigned to a class
variable. It can also be used directly in an expression. For example:
double height = new Box().height;
 Since arrays are object in java, hence while instantiating arrays, we use new operator. For
example:
int arr[] = new int[5];
 At this point, you might be wondering why you do not need to use new operator for
primitives data types. The answer is that Java’s primitive types are not implemented as
objects. Rather, they are implemented as “normal” variables.

 The phrase “instantiating a class” means the same thing as “creating an object.” When
you create an object, you are creating an “instance” of a class, therefore “instantiating” a
class.
class Student{
int a;
int b;
}
class Ex1{
public static void main(String args[]){
Student s1=new Student();
s1.a=10;
s1.b=15;
System.out.println(s1.a+" "+s1.b);
10 15
Student s2; 0 5

s2=s1;
s2.a+=-10;
s2.b+=-10;
System.out.println(s1.a+" "+s1.b);
}
}

You might also like