Escolar Documentos
Profissional Documentos
Cultura Documentos
Object have attributes. These are the characteristic that describe the object . For ex a
student object has the attributes like name, address, branch, semester etc. A book has the
attributes like the title, Id, author, price etc.
Object have operations or behavior. For ex student object has operations like applying
for library account, writing exams etc. Theses all operations need the basic information
like the student name, branch and semester.
Therefore now a class can be defined as a generalized description of same of object
having the same type of attributes and operations.
An object is the physical representation of a class. Objects can be created from a class
blueprint.
Creating a class: As we have seen earlier, a class can be created with the given syntax.
1)Class followed by class name followed by open and closed curly braces which define
the start and end of a class.
class SimpleClass{
public static void main(String[] args){
System.out.println(“ Dummy class “);
}
}
2) main is the function (called method in java ) from which the program execution starts.
3) void is the return type of the class. Since the main method doesn’t return any data type.
4) static keyword will covered in later chapters.
5) public is the access specifier, which make main method accessible from the external
source.
6) System is a predefined class .
7) println method which print to the connected output device
8) out is the outputstream object which is the console.
Declaring Objects:
We can create the object to this class as follows.
SimpleClass obj1,obj2,obj3;
Here obj1,obj2,obj3 are all the SimpleClass type variables which are not yet allocated
memory. An object cannot be used to access the members of a class until it is allocated
memory.
class Test{
int a =10;
public static void main(String args[]){
Test obj1 =new Test();
System.out.println(“Value of a = “+obj1.a); //prints 10
Obj1.a = 20;
Here obj2 is pointing to the memory location of obj1. This is called object variable
referencing.
Introducing methods: Methods are the function which operate on the class data. Ex
main is the method which does the operation of starting the program execution. Similarly
the user can defines his own methods for implementing the objects functionality.
Constructors: Constructor is a method which has the same name as the class name and
doesn’t have the return type.
Constructors are used to initialize the objects of a class.
Ex:
class ReactangleArea{
double width=height=0.0;
public ReactangleArea() { // Constructor
width = 1;
height = 1 ;
}
Parameterized constructors:
Constructor with parameters are called parameterized constructors.
class ReactangleArea{
public ReactangleArea (double width, double height){
double areaOfRectangle = width * height;
System.out.println(“Area of the rectangle = “+areaOfRectangle);
}
public static void main(String[] args){
double w = 20.5;
double h = 15.2;
ReactangleArea obj = new ReactangleArea (w,h); // creating object
}
}
Usage of static keyword: Both data and methods can be declared as static.
If a data or a method is defined as static in a class, all the objects of that class share the
same memory location, i.e. point to the same memory location.
Every object created for a class point to different memory locations. So the data of can
be different in different object for the same data variable say width.
But static data or methods cannot have their memory located in the objects, i.e. the static
fields are created and available even if there are no objects declared for a class.
Note: Static methods can access only static data.
class ReactangleArea{
public static double width,height;
class Area{
static void area(){
double area = ReactangleArea.width * ReactangleArea;
}
}
Usage of final with data: The final keyword can be used with data, methods &
classes.
The value of a final data of the class cannot be modified after it is once initialized. The
final data members are like constants in java.
Ex: public final double pi = 3.45;
The operation defined by the final method of a class cannot be modified by its subclasses.
Ex:
public final double rectangleArea(double width , double height){
double area = width * height;
return area;
}
If a class is declared as final cannot be modified or extended by subclasses. If a class is
final then all its members are by default final.
Access controls: All the data and the methods declared in a class are accessible in that
class. If we want the members of a class to be accessible from another class we have to
use the access specifiers.
Theses are list of access specifiers used in java.
public : Accessible from anywhere
private : Accessible within the class only.
protected : Accessible within same class and all the subclasses within the same
and other packages
default: : Members declared with no access specifier have the default access
also called package access. These members are accessible to all the
members in the same class and same package.
this Keyword: this reference refer to the current class object in which it is used.
1) If the name of the data member is the same as the name of the method
parameter, then this reference can used to identify the current class member.
2) A class can add itself to list of various objects.
Ex:
class Student{
String name;
Garbage collection: Java performs garbage collection for your programs, and thus
eliminates the need to free objects explicitly. When no references exists to an object, and
thus, the object becomes unreachable. Java can reclaim the memory allocated to that
object, without you doing anything about it.
Thus, java has the new operator, but there is no corresponding delete operator as in c++.
When you no longer want to reference an object, you can explicitly set its reference to
null.
Student s1 = new Student();
s1.setName(“Ravi”);
…
s1= null;
OverLoading Methods: In java, a class can have two or more methods with the
same name but with different signatures. The signature of a method consist of the name
together with the number and type of its parameters.
This feature of having multiple methods with the same name but different signatures is
known as overloading.
The compiler identifies the method based on the number and type of parameters.
Class FigureArea{
public void area(double width, double height){
System.out.println(“Area of rectangle = “+ width* height);
}
Overloading constructors: Writing more than one constructor but with different
signatures is the overloading of constructors.. Constructor doesn’t have the return type.
Ex:
Class FigureArea{
public FigureArea (double width, double height){
System.out.println(“Area of rectangle = “+ width* height);
}
}
}
class PassByValue{
public static void main(String args[]){
int a=5;
int b=6;
System.out.println(“Before sorting”);
System.out.println(“A is “ + a);
System.out.println((“B is “+b);
sort(a,b);
System.out.println(“After sorting”);
System.out.println(“A is “ + a);
System.out.println((“B is “+b);
}
Nested Classes: A class can be nested inside another class. This means defining a
class as a member of another class. This is done only when the nested class relies on the
functionality of the outer class.
Class LinkedList{
private Item top = null;
private Item bottom = null;
Nesting of classes beyond one level becomes difficult to read and understand.
Inner Class: Inner class is one type of nested class. An non-static nested class is called
an inner class.
Exploring String Class: Strings in java are not primitive data types.
a)Java has a predefined String class. So strings are objects are in java
Creation of a String:
1) String s1 = new String(”Hello”);
The string object s1 now points to memory location which holds the string literal “hello”;
Here sa1 and sa2 are same text string but are pointing two different memory locations.
But since sb1 literal is created and memory is allocated to it, the sb2 will not create the
new memory but will point to location where sb1 is pointing to.
b)Strings are immutable.(Once string object is created its contents cannot be altered).
String s1= new String(“Hello ”);
s1 = s1+”World”; // this is called concatenation of strings.
(or)
s1+= “World”;
Concatenating a new character or a string to existing string object , will first allocate new
memory with new size(say here 11) and the string(here “Hello world”) is stored in it.
So every time a concatenation happens the new memory location is created, which is a
costly process. So java introduced StringBuffer class
The StringBuffer class will have by default 16 characters of extra space allocated to it.
so the concatenation process is not so costly.