Lec 9
Lec 9
class Class_Name
{
datatype variable 1;
datatype variable 2;
.
.
datatype variable n;
class Employee
{
int id;
String name;
int salary;
float tax;
• As we already know class does not exist physically, only objects exist physically. So by
defining Employee class we only create a template and that does not allocate any
memory until we create objects of that class.
• Object of a class can be created as follows -
• Class_Name object_name = new Class_Name();
• So for employee class we can create object as follows
-
• Employee emp1 = new Employee();
• Objects in java are created using the new keyword, It
dynamically allocate memory for object and return a
reference to that object. The reference is nothing but
the address of the object in memory.
• Employee emp1; // declaring object
• emp1 = new Employee();
• // allocate memory and return address of object, that is received in emp1.
• For example like medicine capsule, the medicine within capsule wrapper is
not accessible until capsule is dipped in some liquid. I.e. to access medicine
within capsule wrapper we have to follow certain procedure (rules and
regulations).
• In the same way Data Encapsulation means to put data (variables) in some
wrapper so that it is not directly accessible to outsiders. The wrapper here
will be the private access specifiers i.e. declare variables as private so that
they are not accessible outside the class. This process of hiding data from
outsiders is known as Data Hiding. And if any user outside the class wants to
access encapsulated data then he has to follow certain rules and regulations
and these rules and regulations are defined by public methods (getters and
setters methods).
• Example
class Employee
{
private int id;
private String name;
private int salary;
emp1.setName("Tom");
emp1.setSalary(20000);
In the above program we declare all data (id, name, salary) of the
Employee class as private so that anyone outside the class should not be
able to access it directly. This process of wrapping the data in private
access specifier is known as Data Encapsulation.
Right now, you do not need to be worried about what Exception is,You
will learn it later.For now just consider that it will stop program execution
and show the message whatever you entered in double quotes.).
• Benefit of Data Encapsulation -
• So now the question arises why we encapsulate
data, and what is the benefit of doing this?
By making data encapsulated, we ensure that
anyone outside the class should not be able to
access the data, if he passes some anonymous
values for data for example we know that
Employee id can never be 0 or negative similarly
salary can also never be zero or negative. So if
user ever passes some negative values or zero for
these data then he will not be able to access them
and a Runtime Exception will be thrown.
Constructor
• Each object we created must be initialized to some value. We can do this in two
ways -
emp1.id = 1;
emp1.name="Tom";
• 2 - Secondly we can initialize objects by using methods -
emp1.setId(1);
emp1.setName("Tom");
• But the initialization of objects is so common that Java enables objects to
initialize themselves when they are created.
class Employee
{
public void a()
{
System.out.println("AAA");
}
public void b()
{
System.out.println("BBB");
}
public Employee() // Constructor
{
System.out.println("You are in Constructor");
}
}
emp1.a();
emp1.b();
}
}
Output -
You are in Constructor
AAA
BBB
• Program Description -
• Note that constructor :
public Employee()
{
System.out.println("Constructor");
}
• Have the same name as class name and also do not have any return type.
• Also note that constructor runs only once for any object when we create
that object, we cannot call them multiple times like java methods.
• Types of Java Constructor -
• Constructor is of two types -
• 1- Constructor without parameter (Default Constructor) - If a constructor does not have any
parameter than it is called Default Constructor, as constructor created in last example.
• If you do not define any constructor explicitly, a default constructor is automatically built by
java run time system.
• For example -
class Employee
{
}
• Even though we have not define anything within the class but compiler will automatically
generate a default constructor within the class like this -
public Employee()
{
}
• Note - A default constructor initializes all data members (variables) of the class to their
default values.
• 2 - Constructor with parameter
(Parameterized Constructor) - If a constructor
has one or more parameters then it is called
parameterized constructor.
• For example -
public Employee(int i, String n)
{
//Statements;
}
Example -
class Employee
{
int id;
String name;
public void input()
{
id = Integer.parseInt(System.console().readLine("Enter ID"));
name = System.console().readLine("Enter name");
}
public void disp()
{
System.out.println(id+ " " + name);
}
class Employee
{
int id;
String name;
public void input()
{
id = Integer.parseInt(System.console().readLine("Enter ID"));
name = System.console().readLine("Enter name");
}
public void disp()
{
System.out.println(id+ " " + name);
}
• When the code Employee emp1 = new Employee(); gets executed, it will
cause the compiler to look for a constructor that is not having any
parameter, so Constructor 1 will gets executed and initialize data fields of
emp1 to their default value (i.e. 0 for int and null for String)
• Note the default value of object type is null.
class Employee
{
int id;
String name;
Output -
1 Tom
1 Tom
• When the code Employee emp1 = new
Employee(1, "Tom"); gets executed, it will call
the constructor and set emp1 id to 1 and
name to 'Tom'.
class Employee
{
int id;
String name;
• When the code Employee emp1 = new Employee("John", 2); gets executed , It will call to
constructor 4 and the statement this(i,n) will call to a constructor that have first
parameter of int type and second parameter of String type, So it will call to constructor
3.
• Constructor 3 contain the 1st statement this(i) , it will call to a constructor that contain
only 1 parameter of int type so constructor 1 will be executed and set the id of emp1 to
1.
• Then it will come back to constructor 3 that will set the name of emp1 to 'John' and
finally it come back to constructor 4.
• The next line emp1.disp(); will display id and name of emp1.
• 'this' keyword -
• this means 'object of current class'. It refers to the current object.
'this' keyword is normally used to differentiate between local and
class variables within a method. When local variables within method
have the same name as class variables then local variables hides the
class variables. In that case this keyword is used to refer class
variables.
For example - consider the Constructor 4 of previous example -
public Employee(int id, String name)
{
this.id = id;
this.name = name;
}
Here this.id refers to object's id and id refers to method's id. Similarly
this.name refers to object name and name refers to method name.
Method Overloading (Function
Overloading) -
• In java it is possible to have multiple methods with the same
name in a class as long as their parameter declarations are
different i.e. either the number of parameters or data types of
parameters are different. In this case methods are said to be
overloaded and process is referred as method overloading.
This is one of the ways Java supports the concept of
Polymorphism.
• When an overloaded method is called, Compiler checks for
the number and data types of parameters to determine which
overloaded method to call.
Note - Always remember that there is no method overloading
on the basis of return type of methods.
class Test
{
public int sum(int a) // version 1
{
return (a+a);
}
public int sum(int a, int b) // version 2
{
return (a+b);
}
public double sum(double a) // version 3
{
return (a+a);
}
public double sum(double a, double b) // version 4
{
return (a+b);
}
}
class Test
{
public int sum(int a, int b) // version 1
{
return (a+b);
}
public double sum(double a) // version 2
{
return (a+a);
}
}