Unit-2 - Classes, Object & Methods
Unit-2 - Classes, Object & Methods
OBJECTS, CLASSES
AND METHODS
Class
Class can be defined as a template or blueprint that
describe the behaviors of a particular entity.
Once defined this new type can be used to create object of that
type.
Height
Soapbox Length
Width
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
Access Modifiers in java
There are two types of modifiers in java: access modifiers and
non-access modifiers.
The access modifiers in java specifies accessibility (scope) of a
data member, method, constructor or class.
Syntax:
Package mypackage;
Public class myclass
{
------------------------
------------------------
------------------------
}
Accessing Own Packages:
Syntax:
Import pack1[.pack2][.pack3].classname;
Example:
Import mypackage.*;
Import mypackage.myclass;
Example of default access modifier
In this example,
we have created two packages pack and mypack. We are
accessing the A class from outside its package, since A class is
not public, so it cannot be accessed from outside the package.
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
In the above example, the scope of class A and its method msg() is
default so it cannot be accessed from outside the package.
3.protected Modifier:
The protected access modifier is accessible within package
and outside the package but through inheritance only.
The protected access modifier can be applied on the data
member, method and constructor. It can't be applied on the class.
//save by B.java
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Output:Hello
4.public access modifier
The public access modifier is accessible everywhere. It has the widest
scope among all other modifiers.
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
//Output:Hello
Array of Object:
Arrays are capable of storing objects also. For example, we can create an
array of Strings which is a reference type variable. However, using a
String as a reference type to illustrate the concept of array of objects isn't
too appropriate due to the immutability of String objects.
Example:
class Student
{
int marks;
}
Rule:
If there is no constructor in a class, compiler
automatically creates a default constructor.
class Rectangle
{
double width;
double height;
Rectangle()
{
width = 10;
height = 10;
}
double area()
{
return width * height;
}
}
public class defaultc
{
public static void main(String args[])
{
Rectangle mybox1 = new Rectangle();
double area;
area = mybox1.area();
System.out.println("Area is " + area);
}
}
Java parameterized constructor
A constructor that have parameters is known as parameterized
constructor.
Parameterized constructor is used to provide different values to
the distinct objects.
class student
{
int rno;
String name;
student(int i,String n)
{
rno = i;
name = n;
}
void display()
{
System.out.println("Rno: "+rno+"\tName: "+name+"\n");
}
}
class pconstruct
{
public static void main(String args[])
{
student s1 = new student(5,"Karan");
student s2 = new student(2,"Aryan");
s1.display();
s2.display();
}
}
Constructor Overloading in Java
class staticblk
{
static
{
System.out.println("static block is invoked");
}
public static void main(String args[])
{
System.out.println("Hello main");
}
}
Java static method
•A static method can be invoked without the need for creating an instance
of a class.
•static method can access static data member and can change the value of
it.
static Variable:
•The static variable can be used to refer the common property of all
objects
e.g. company name of employees,college name of students etc.
•The static variable gets memory only once in class area at the time of
class loading.
class staticstud
{
int rno;
String name;
static String college = "PCMCS";
s1.display();
s2.display();
s3.display();
}
}
The finalize() method :
Clean-up activity means closing the resources associated with that object
like Database Connection, Network Connection or we can say resource
de-allocation.
Syntax:
protected void finalize() throws Throwable{}
Nested Class:
static nested class : Nested classes that are declared static are called
static nested classes.
class NestedClass
{ ...
}
}
Method Overloading in java
Syntax:
returntype methodname()
{
//code to be executed
methodname();//calling same method
}
Anonymous Inner Class
It is an inner class without a name and for which only a single object is
created.
•The Object class is the parent class of all the classes in java by
default.
• In other words, it is the topmost class of java.
•The Object class is beneficial if you want to refer any object whose
type you don't know. Notice that parent class reference variable can
refer the child class object, know as upcasting.
• For example:
•Object obj=getObject();//we don't know what object will be returne
d from this method