Chapter 2
Chapter 2
METHODS [24]
Class
User-defined data type with a template that
serves to define its properties
A class is the blueprint of an object
The class uses methods to define the
behaviors of the object
The class that contains the main method of a
Java program represents the entire program
Multiple objects can be created from the
same class
Syntax:
class classname [extends superclass]
{
fields declaration;
methods declaration;
}
Objects
Is a block of memory that contains space to
store all instance variables
An object has:
state - descriptive characteristics
behaviors - what it can do
Methods
Represents behavior of objects
Consists of four parts:
Constructors [4M]
Special type of method hat enables an object
to initialize itself when it is created.
Same name as classname
Do not specify return type bcoz always return
instance of class
It is immediately called as soon as object is
created before new operator completes.
Types:
Default
parameterized and
copy constructor.
Copy constructor
class Student{
int id; String name;
Student(int i, String n){
id = i; name = n;
}
Student(Student s){
id = s.id; name =s.name;
}
void display(){
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student s1 = new Student(111,abc");
Student s2 = new Student(s1);
s1.display(); s2.display();
}
}
Nesting of Methods
If a method in java calls a method in the same class
Dot operator is not needed
class Demo{
int a=10,b=20;
void display() {
S.o.p(Numbers are:+a+ and +b);
}
void add(){
display();
S.o.p(Addition is:+(a+b));
}
}
class DemoMain{
public static void main(String args[]){
Demo d1 = new Demo();
d1.add();
}}
this keyword[2M]
this is a keyword in Java which can be used inside the
Method or constructor of Class.
It(this) works as a reference to the current Object
whose Method or constructor is being invoked.
Example:
Class Demo {
int variable = 5;
public static void main(String args[]) {
Demo obj = new Demo();
obj.method(20);
}
void method(int variable) {
variable = 10;
S.o.p("Value of Instance var :" + this.variable);
S.o.p("Value of Local var :" + variable);
}
}
Object class
The java.lang.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.
Constructor : Object()
All objects, including arrays, implement the
methods of this class.
Package
A java package is a group of similar types of
classes, interfaces and sub-packages.
Package in java can be categorized in two
form, built-in package and user-defined
package.
There are many built-in packages such as
java, lang, awt, swing, net, io, util, sql etc.
Advantage of Java Package
easily maintained.
provides access protection.
Visibility Control
Java provides a number of access modifiers
to set access levels for classes, variables,
methods and constructors.
The four access levels are:
Visible
Visible
Visible
Visible
Access
Modifier
to
to
to
to
the
the
the
the
package (default).
class only (private).
world (public).
package and all subclasses (protected).
within
class
within
package
outside package
by subclass only
outside
package
Private
Default
Protected
Public
1. Default
Default access modifier means we do not
explicitly declare an access modifier for a
class, field, method, etc.
A variable or method declared without any
access control modifier is available to any
other class in the same package.
Example:
String version = "1.5.1";
boolean processOrder() {
return true;
}
package pack;
class A{
void msg(){System.out.println("Hello");}
}
1. Default (Example)
package pack;
class A{
void msg(){
System.out.println("Hello");
}
}
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
}
}
2. Private
Private access modifier is the most restrictive
access level.
Methods, Variables and Constructors that are
declared private can only be accessed within
the declared class itself.
Class and interfaces cannot be private.
Using the private modifier is the main way
that an object encapsulates itself and hide
data from the outside world.
2. Private (Example)
class A{
private int data=40;
private void msg(){
System.out.println("Hello java");
}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
S.o.p(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
3. Public
A class, method, constructor, interface,
fields, etc. declared public can be accessed
from any other class belonging to the Java
Universe.
However if the public class we are trying to
access is in a different package, then the
public class needs to be imported.
Because of inheritance, all public methods
and variables of a class are inherited by its
subclasses.
3. Public (Example)
package pack;
public class A{
public void msg(){
System.out.println("Hello");
}
}
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
4. Protected
Variables, methods and constructors which
are declared protected in a superclass can be
accessed only by the subclasses in other
package or any class within the package.
The protected access modifier cannot be
applied to class and interfaces.
4. Protected (Example)
package pack;
public class A{
protected void msg(){
System.out.println("Hello");
}
}
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}