Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture5 Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Advanced Programming 1

CLASSES, OBJECTS AND METHODS


CLASS:
Class is a user defined data type that consists of members and methods. A class represents a group
name. Class is a model for creating the objects.

Syn: class class-name { Ex: class Person {


Members; int age;
Methods; String name;
} }
Ex 1: class Person {
int age;
String name;
}

Ex 2: public class Dog{


String breed;
int age;
String color;
void barking( ) {
}
void hungry( ) {
}
void sleeping( ) {
}
}

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;

Prepared by Krishna Kumari Ganga Page 1


Advanced Programming 1

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.

INSTANCE VARIABLES AND METHODS:


Variables with in the user-defined class are known as instance variables. Methods with in the user-
defined class are known as instance methods. Instance methods act upon instance variables. The instance
variables and methods are only access by the objects.

INITIALIZING THE INSTANCE VARIABLES:


Type1: we can initialize the instance variables of one class in another class with the help of object.
p.name = “salem”;
p.age = 24;
Example:
class Person {
String name="salem";
int age=24;
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.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;

Prepared by Krishna Kumari Ganga Page 2


Advanced Programming 1

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;
} }
} }

Prepared by Krishna Kumari Ganga Page 3


Advanced Programming 1

 A constructor does not return any value even void.


 A constructor is automatically called and executed at the time of creating an object.
 While creating an object, if nothing is passed to the object, then the default constructor is called and
executed. If some values are passed to object, then parameterized constructor will be called and
executed.
Person raju=new Person( ); (// default constructor is called & executed)
Person raju=new Person (“raju”,23); (// parameterized constructor is called & executed)
 A constructor called and executed only once per object. That means, when we create an object, the
constructor is called and executed. When we create second object, then again the constructor is called
second time and executed.
Q. What is the difference between default constructor and parameterized constructor?
Default constructor Parameterized constructor
1. Default constructor is used to initialize all 1. Parameterized constructor is used to initialize
objects with same data values. each object with different data values.
2. Default constructor does not have any 2. Parameterized constructor will have one or more
parameters. parameters.
3. When data is not passed at the time of 3. When data is passed at the time of creating an
creating an object, the default constructor is object, the parameterized constructor is called and
called and executed. executed.

Q. What is the difference between constructors and methods?


Constructor Method
1. Constructor is used to initialize the instance 1. A method is used for any general purpose
variables of a class. processing or calculations.
2. A constructor name and class name should 2. A method name and class name are not same
be same.
3. A constructor is called at the time of creating 3. A method can be called after creating the object.
the object.
4. A constructor is called once per an object. 4. A method can be called several times on the
object.
5. A constructor is called and executed 5. A method is called and executed only when we
automatically call it.

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;
}
}

Prepared by Krishna Kumari Ganga Page 4


Advanced Programming 1

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.

Prepared by Krishna Kumari Ganga Page 5


Advanced Programming 1

A constructor name and class name should be same.

ACCESSING CLASS MEMBERS AND METHODS:


We can not access the instance variables and methods directly. We must use the concerned object and
the dot operator as shown below.
SYN: Object – name.variable – name;
Object – name.method – name(parameter list);
Note: We can create any number of objects to the class. Each object maintains it’s own set of data.

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:

Prepared by Krishna Kumari Ganga Page 6


Advanced Programming 1

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:

Prepared by Krishna Kumari Ganga Page 7


Advanced Programming 1

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.

Prepared by Krishna Kumari Ganga Page 8

You might also like