Lecture5 Java
Lecture5 Java
Lecture5 Java
OBJECT:
Object is a run time entity. The variable of class is called as object. It also contains members and
methods that are defined in the class.
Syn: Person p; (// declare)
p = new Person( ); (// instantiate)
We can combine both above two steps as follows.
Person p = new Person( );
ACCESS SPECIFIERS:
An access specifier is a keyword. It specifies the accessibility of members and methods of a class or a
class itself. We can use access specifiers before a class and its members & methods. There are five access
specifiers.
1. Private
2. Public
3. Protected
4. Private protected
5. Default
Private: Private members of a class are not accessible to anywhere out side the class. They are accessible
only with in the class by the methods of that class.
Ex: private int sex;
Public: Public members of a class are accessible everywhere out side the class. So any other program can
read them and use them.
Ex: public int age;
Protected: Protected members of a class are accessible out side the class with in the same package. But
these members are not accessible to the other packages.
Ex: protected String name;
Private protected: A member can be declared with two keywords private and protected together like
Ex: private protected int codeNumber;
The private protected members are not accessible by other classes in the package. They are accessible for
the sub classes with in the same package.
Default: If the programmer does not write any access specifier, then the java compiler uses a default
access specifier. Default members are accessible out side the class, but with in the same directory.
class PersonEx1 {
public static void main(String args[]) {
Person p1=new Person();
Person p2=new Person();
p1.display();
p2.display();
}
}
Disadvantage: In type1, if the instance variables are declared as private then they are not available to other
class.
Type2: We can initialize the instance variables at the time of their declaration with in the class.
Disadvantage: In type2, every object is initialized with the same value.
Example:
class Person {
String name;
int age;
void display() {
System.out.println("NAME="+name);
System.out.println("AGE="+age);
}
}
class PersonEx1 {
public static void main(String args[]) {
Person p1=new Person();
Person p2=new Person();
p1.name=”salem”;
p1.age=24;
p2.name=”Abdulla”;
p2.age=32;
p1.display();
p2.display();
}
}
Disadvantage: In type2, if the instance variables are declared as private then they are not available
to other class.
Type3: We can initialize the instance variables by using constructor. A constructor is a method that is used to
initialize the instance variables.
CONSTRUCTORS:
A constructor is nothing but a method that is used to initialize the instance variables. A constructor
follows the following characteristics
The constructors name and class name should be same.
A constructor name should be ends with a pair of simple parentheses.
For example, in Person class, we can write a constructor as
class Person {
// Constructor
Person( ) {
Body;
}
}
A constructor may or may not have parameters. Parameters are used to receive data from outside into
the constructor.
A constructor with out any parameter is called default constructor.
A constructor with one or more parameters is called parameterized constructor. We can write default
constructor and parameterized constructor as follows.
class Person{ class Person {
// Default Constructor //parameterized Constructor
Person( ) { Person (String name, int age) {
Body; Body;
} }
} }
CONSTRUCTOR OVERLOADING:
Writing two or more constructors with the same name but difference in parameters list is called
constructor overloading. Such constructors are useful to perform different tasks.
While creating an object, java matches up the class name first and then the number and type of
parameters to decide which one of the constructor definitions to execute. This process is known as
polymorphism.
EX:
class Person {
Person(String name, int age) {
Body;
}
Person(String name, char sex, int age) {
Body;
}
}
Example:
class Bsc{
int fees;
Bsc() {
fees=7000;
}
Bsc(int f) {
fees=f;
}
void display() {
System.out.println("YOUR FEES="+fees);
}
}
class ConstructorOverloading {
public static void main(String args[]) {
Bsc B1=new Bsc();
Bsc B2=new Bsc(5000);
Bsc B3=new Bsc(4000);
Bsc B4=new Bsc();
B1.display();
B2.display();
B3.display();
B4.display();
}
}
METHODS:
A method represents a group of statements that performs a particular task. A method contains two
parts. They are
1. Method prototype (or) method header
This part contains method name, method parameter and method return type. Method
parameters are variables to receive data from out side in to the method.
Syn: Return-type method – name (parameter1, parameter2, ….);
Ex: void sum( );
double sum(double a, double b);
long fact(int num);
2. Method body:
Method body contains a group of statements that represent the logic to perform the task.
Syn: Return – type Method - name( parameter1, parameter2, . . . . ) {
Statements;
}
Note:
If a method returns some value then we should write return statement in a body.
A method can return only one value.
A method is executed only, when it is called.
METHOD OVERLOADING:
Writing two or more methods with the same name but difference in parameters list is called method
overloading. Method overloading is used when objects are required to perform similar tasks but using
different input parameters.
When we call a method in an object, java matches up the method name first and then the number and
type of parameters to decide which one of the method definitions to execute. This process is known as
polymorphism.
Ex1: class Person{
void sum(int num1,int num2) {
Body;
}
int sum(int num1,int num2,int num2) {
Body;
}
}
Ex2:
class Addition {
void sum(int a,int b) {
int result;
result=a+b;
System.out.println("Addition of 2 numbers="+result);
}
void sum(int a,int b,int c) {
int result;
result=a+b+c;
System.out.println("Addition of 3 numbers="+result);
}
}
class MethodOverloading {
public static void main(String args[]) {
Addition a1=new Addition();
a1.sum(10,20);
a1.sum(4,7,9);
}
}
NESTING OF METHODS:
Unto now, we called a method of a class only by an object of that class and dot operator. There is
another way to call the methods in java. That is called nesting of methods.
Definition:A method can be called by using only its name by another method of the same class. This
is known as nesting of methods.
Example:
class Addition {
void sum(int a,int b) {
int result;
result=a+b;
System.out.println("Addition of 2 numbers="+result);
}
void sum(int a,int b,int c) {
sum(10,20);
int result;
result=a+b+c;
System.out.println("Addition of 3 numbers="+result);
}
}
class NestingMethod {
public static void main(String args[]) {
Addition a1=new Addition();
a1.sum(4,7,9);
}
}
STATIC VARIABLES:
static is a keyword. If we place the static keyword before a variable declaration then that variable is
called as static variable. Static variables are also called as class variables. A single copy of static
variables will be shared by all objects of the class. We can declare the static variables as shown
below.
SYN: static Data – type var1, var2…..varN;
Ex: class Person {
static int age;
static String name;
}
No need to create object for a class to access the static variables. We can access the static variables
by using its class name and dot operator as shown below.
Syn: Class – name.variable – name;
EX: Person.name=”siva;
Person.age=23;
Note: we can also access the static variables by using the object name and dot operator(like instance
variables). But it is not recommended.
EX: P1.name=”siva;
P1.age=23;
STATIC METHODS:
Static is a keyword. If we place static keyword before the method head, then that method is called as
static method. Static methods only acts on static variables. We can declare the static variables as
shown below.
Syn: static return – type Method - name( parameter1, parameter2, . . . . ) {
Statements;
}
Ex: class Person {
static void display( ) {
Body;
}
}
No need to create object for a class to access the static methods. We can access the static methods by
using its class name and dot operator as shown below.
Syn: Class – name.method – name( );
Ex: Person.display( );
Class – name.variable – name;
Note:
We can also access the static methods by using the object name and dot operator (like
instance variables). But it is not recommended.
EX: P1.display( );
Static methods cannot access instance variables.