0% found this document useful (0 votes)
8 views108 pages

Encapsulation

Encapsulation in Java is a programming technique that binds data and methods together within a class, preventing outside access and ensuring data security. It is implemented by declaring class fields as private and providing public getter and setter methods for access. The advantages of encapsulation include enhanced flexibility, security, and maintainability, while its main disadvantage is increased code length and potential performance impact.

Uploaded by

Supriya Nevewani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views108 pages

Encapsulation

Encapsulation in Java is a programming technique that binds data and methods together within a class, preventing outside access and ensuring data security. It is implemented by declaring class fields as private and providing public getter and setter methods for access. The advantages of encapsulation include enhanced flexibility, security, and maintainability, while its main disadvantage is increased code length and potential performance impact.

Uploaded by

Supriya Nevewani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 108

Encapsulation in Java | Realtime

Example, Advantage
The process of binding data and
corresponding methods (behavior) together
into a single unit is called encapsulation
in Java.
In other words, encapsulation is a
programming technique that binds the
class members (variables and methods)
together and prevents them from being
accessed by other classes.
Thereby, we can keep variables and
methods safes from outside interference
and misuse.
Every Java class is an example of
encapsulation because we write everything
within the class only that binds variables
and methods together and hides their
complexity from other classes.
Another example of encapsulation is a
capsule. Basically, capsule encapsulates
several combinations of medicine.
If combinations of medicine are variables
and methods then the capsule will act as a
class and the whole process is called
Encapsulation as shown in the below
figure.

In the encapsulation technique, we declare


fields as private in the class to prevent
other classes from accessing them directly.
The required encapsulated data can be
accessed by using public Java getter and
setter method.
If the field is declared private in the class
then it cannot be accessed by anyone from
outside the class and hides field within the
class. Therefore, it is also called data
hiding.
Let’s understand Encapsulation in java
better by taking realtime examples.
Realtime Example of Encapsulation in
Java

Realtime Example 1:
School bag is one of the most real
examples of Encapsulation. School bag
can keep our books, pens, etc.
Realtime Example 2:
When you log into your email accounts
such as Gmail, Yahoo Mail, or Rediff
mail, there is a lot of internal processes
taking place in the backend and you have
no control over it.
When you enter the password for logging,
they are retrieved in an encrypted form and
verified, and then you are given access to
your account.
You do not have control over it that how
the password has been verified. Thus, it
keeps our account safe from being
misused.
Realtime Example 3:
Suppose you have an account in the bank.
If your balance variable is declared as a
public variable in the bank software, your
account balance will be known as public,
In this case, anyone can know your
account balance. So, would you like it?
Obviously No.
So, they declare balance variable as private
for making your account safe, so that
anyone cannot see your account balance.
The person who has to see his account
balance, will have to access only private
members through methods defined inside
that class and this method will ask your
account holder name or user Id, and
password for authentication.
Thus, we can achieve security by utilizing
the concept of data hiding. This is called
Encapsulation in Java.
How to achieve or implement
Encapsulation in Java

There are two important points whereby


we can achieve or implement
encapsulation in Java program.
1. Declaring the instance variable of the
class as private so that it cannot be
accessed directly by anyone from outside
the class.
2. Provide the public setter and getter
methods in the class to set/modify the
values of the variable/fields.
Advantage of Encapsulation in Java
There are the following advantages of
encapsulation in Java. They are as follows:
1. The encapsulated code is more flexible
and easy to change with new requirements.
2. It prevents the other classes from
accessing the private fields.
3. Encapsulation allows modifying the
implemented code without breaking other
code that has implemented the code.
4. It keeps the data and codes safe from
external inheritance. Thus, encapsulation
helps to achieve security.
5. It improves the maintainability of the
application.
6. If you don’t define the setter method in
the class, then the fields can be made read-
only.
7. If you don’t define the getter method in
the class, then the fields can be made
write-only.
8. If you define both getter and setter
methods in the class, then the fields can be
made both read-write.
Disadvantage of Encapsulation in Java

The major disadvantage of encapsulation


in Java is it increases the length of the code
and slows shutdown execution.
Data Hiding in Java

Data hiding in Java is an important


principle of object-oriented programming
system (OOPs). It prevents to access data
members (variables) directly from outside
the class so that we can achieve security on
data. This oops feature is called data
hiding in Java.

An outside person could not access our


internal data directly or our internal data
should not go out directly. After validation
or authentication, the outside person can
access our internal data.
For example, after providing proper
username and password, you can able to
access your Gmail inbox information.
How to achieve Data hiding
programmatically?

By declaring data members (variables) as


private, we can achieve or implement data
hiding. If the variables are declared as
private in the class, nobody can access
them from outside the class.
The biggest advantage of data hiding is we
can achieve security.
Key points:
1. We highly recommended it to declare
data members as private in the class.
2. A combination of data hiding and
abstraction is nothing but encapsulation.
Encapsulation = Data
Hiding + Abstraction
If any component follows data hiding and
abstraction, it is called an encapsulated
component.
Tightly Encapsulated Class in Java

If each variable is declared as private in


the class, it is called tightly encapsulated
class in Java. For a tightly encapsulated
class, we are not required to check whether
the class contains getter and setter method
or not and whether these methods are
declared as public or not.
Q. Which of the following classes are
tightly encapsulated?
class A
{
private int x = 20;
}
class B extends A
{
int y = 50;
}
class C extends A
{
private int z = 10;
}
class P
{
int a = 10;
}
class Q extends P
{
private int b = 20;
}
class R extends Q
{
private int z = 30;
Ans: None of these is tightly encapsulated
class because class Q is the child class of
P. Non-private data members of class P by
default are available in subclass Q.

Program code 1:
package encapPrograms;
public class Student
{
private String name;
public String getName()
{
return name;
}
public setName(void String studentName)
{
name = studentName;
}
}
class EncapsulatedTest
{
public static void main(String[] arg)
{
Student obj = new Student();
obj.name = "Amit"; // Compilation
error. Since name is private.
String studentName = obj.name; //same
as above.
}
}
To remove the compilation error problem
from the above code, we need to call the
getter, getName(), and the setter setName()
to read and update the value of variable.
class EncapsulationTest
{
public static void main(String[] args)
{
Student obj = new Student();// Creating
object of Student class by using new
keyword.
obj.setName("Amit"); // setting the
value of variable.
String studentName = obj.getName(); //
reading the value of variable.
System.out.println(studentName);
}
}
Output:
Amit
Program code 2:
package encapPrograms;
public class Student
{
// Step 1: Declare variables as private in
the class.
private String stdName; // private field.
private int stdRollNo; // private field
private int stdId;

// Step 2: Apply the public getter method


for each private variable.
public String getStdName()
{
// Private fields can be accessed only
inside the public method.
return stdName;
}
public int getStdRollNo()
{
return stdRollNo;
}
public int getStdId()
{
return stdId:
}
// Step 3: Apply the public setter method
for each private variable.
public void setStdName(String name)
{
stdName = name;
}
public void setStdRollNo(int rollNo)
{
stdRollNo = rollNo;
}
public void setId(int id)
{
stdId = id;
}
}
public class EncapsulationTest {
public static void main(String[][] args)
{
// Step 4: Create the object of class Student
by using the new keyword.
// obj is the reference variable of class
student and pointing to the object of the
student class.
Student obj = new Student();
// Step 5: Call setter methods and set the
values of variables.
obj.setStdName("Kiran");
obj.setStdRollNo(4);
obj.setStdId(12345);

// Step 6: Call the getter method to Read


the value of variables and print it.
String name1= obj.getStdName();
System.out.println("Student's Name: " +
name1);
Int i= obj.getStdRollNo()
System.out.println("Student's Roll no.: "
+i);
System.out.println("Student's Id: "
+obj.getStdId());
}
}
Output:
Student Name: Kiran
Student Roll no: 4
Student Id: 12345

This Keyword in Java | Use, Example


this keyword in Java is a reference
variable that refers to the current class
object. In other words, it holds the
reference to the current class object or the
same class object.
The current class object can be referred by
using this reference anywhere in the class.
The keyword “this” in Java can be applied
to instance variables, constructors, and
methods.
It is used only inside the member
functions of the class. In other words, it
can only be used within the non-static
method of a class. This reference cannot
be used outside the class.
The syntax to use ‘this’ keyword in Java is
as follows:
Syntax:
this.a; // It calls current class instance
variable where a is an instance variable.
this.msg(); // It calls current class instance
method where msg() is an instance
method.
this(int a); // To call parameterized
constructor of current class object.
Note: this and super keyword cannot be
used in a static method and static
initialization block. This will give
compile-time error.
Program source code 1:
package thisKeyword;
public class Hi
{ void msg()
{
System.out.println(this); // It will print
the same reference ID.
}
public static void main(String[] args)
{
Hi h = new Hi();
System.out.println(h); // It will print the
reference ID.
h.msg();
}
}
Output:
thisKeyword.Hi@1db9742
thisKeyword.Hi@1db9742
There are six usages of Java this keyword.
They are as follows:
1. this reference can be used to refer to the
current class instance variable.
2. this keyword is used to call the non-
static method of the current class.
3. this() can be used to invoke the current
class constructor.
4. this keyword can be used as a parameter
in the method call.
5. The keyword “this” can be used as a
parameter in the constructor call.
6. It can also be used to return the object of
the current class from the method.
Program source code 2:
package thisKeyword;
public class Student
{
// Declare instance variables.
String name;
int rollno;

// Declare two parameterized constructors


such as name and rollno. Here, name and
rollno are local variables.
Student(String name, int rollno)
{
// Here, the parameter's identifier is the
same as that of the instance variables
name.
name = name;
rollno = rollno;
}
void display()
{
System.out.println(name+ " "+rollno);
}
public static void main(String[] args)
{
// Create an object of class Student and call
the display() method using reference
variable s.
Student s = new Student("DEEPAK",
123);
s.display();
}
}
Output:
null 0
In the above example program, the
parameter’s identifier (formal arguments)
is the same as that of the instance variable.
It is permissible to do this in java.
But, there occurs the problem of ambiguity
between parameters and instance variables.
So, we can resolve this problem by using
this keyword in java programming.
Solution to solve the above problem using
this keyword
Program source code 3:
public class Student
{
// Declare instance variables.
String name;
int rollno;
// Declare a parameterized constructor with
two parameters such as name and rollno.
Here, name and rollno are local variables.
Student(String name, int rollno)
{
// Here, the parameter's identifier is same
as that of the instance variables name. So,
we will use this keyword to differentiate
between instance variables and parameters.
this.name=name; // Reference to current
object.
this.rollno=rollno; // Reference to
current object.
}
void display()
{
System.out.println(name+ " "+rollno);
}
public static void main(String[] args)
{
// Create an object of class Student and call
the display() method using reference
variable s.
Student s = new Student("DEEPAK",
123);
s.display();
}
}
Output:
DEEPAK 123
Explanation:
In the main method, the object reference
variable ‘s’ is the current object in context.
So, this reference stores the reference of
object reference variable s.

In the parameterized constructor of class


Student, this.name and this.rollno refer to
the data field of current class object s.
Here, the current class object means same
class object. They can also be written
without using this keyword.
But as the parameters (local variables) of
instance method are having the same name
as instance variables of class, we have to
use the keyword this.
Note: If local variables (formal arguments)
and instance variables are different, there
is no need to use keyword ‘this’.
Calling current class method using this
keyword

The keyword ‘this’ can be used to call


methods of the current class. If we do not
write ‘this’ keyword, the compiler
automatically adds ‘this’ keyword while
invoking the method.
The following example in the figure shows
how to add ‘this’ keyword automatically
by the compiler while calling method.
Let’s create a program where we will call
the current class method using this
keyword. JVM will automatically put
“this” keyword while calling method.
Program source code 4:
package thisKeyword;
public class Hello
{
/** * this keyword is used to call
current/same class method. */
void m()
{
System.out.println("Hello Java");
}
void n()
{
m(); // method m() is called. m() is same
as this.m(). JVM automatically put this
here.
System.out.println("Welcome you");
}
public static void main(String[] args)
{
Hello obj = new Hello();
Hello obj2=new Hello();
obj.n();
}
}
Output:
Hello Java
Welcome you
Returning this reference to Method in Java

Let’s take an example program in which


we will return this keyword to the method.
Program source code 5:
package thisKeyword;
public class AddTest
{
// Declare an instance variable num with
data type int.
int num;
// Declare one parameter constructor with
parameter num. The parameter num is a
local variable.
AddTest(int num)
{
this.num = num; // Reference to
current object.
}
// Declare an instance method.
void show()
{
System.out.println("Number: "
+this.num);
}
// Declare an instance method incr() with
type class 'AddTest'.
AddTest incr()
{
num++; // Increment by 1.
// Returns this reference to the current
object.
return this;
}
public static void main(String[] args)
{
// Create an object of the class and pass
argument 20 to one parameter constructor.
AddTest add = new AddTest(20);
// Call incr() method using reference
variable add and increment two times.
add.incr().incr(); // Call show() method
using reference variable add.
add.show();
}
}
Output:
Number: 22
Explanation:
The method incr() returns a reference to
the current object. Inside the main method,
when add.incr is called using reference
variable add, num is incremented to 21 and
the reference of the current object is
returned which again calls the incr()
method. This is done two times.
So, number becomes 22. In the end, the
show method is called using the reference
variable which displays the number: 22.
Calling current class constructor using this
keyword in Java

this() keyword in Java can also be used to


call another current class constructor from
within a constructor. This is called an
explicit invocation constructor. It is used
for constructor chaining.
Key Points:
1. The invocation of another constructor
using ‘this’ keyword must be the first line
of constructor only. That means we cannot
add this() keyword anywhere other than
the first line of the constructor.
2. JVM never put automatically this
keyword like super.
3. Recursion using ‘this’ call to the
constructor is not allowed in Java.
Let’s take some example programs based
on this concept.
Calling default constructor from
parameterized constructor
Program source code: 6
package thisKeyword;
public class ABC
{
// Declare a default constructor.
ABC()
{
System.out.println("HELLO JOHN");
}
// Declare a parameterized constructor with
parameter x. x is a local variable.
ABC(int x)
{
this(); // Must be the first line in the
constructor. // Calling default constructor
from parameterized constructor.
System.out.println("WELCOME
INDIA");
}
}
public class ABCTest
{
public static void main(String[] args)
{
// Create an object of class ABC and pass
the value to parameterized constructor.
ABC obj = new ABC(12);
}
}
Output:
HELLO JOHN
WELCOME INDIA
Explanation:
In the main method of class ABCTest,
when an object is created for class ABC
with passing an argument 12, it calls
parameterized constructor of class ABC.
The keyword this in the first line of the
constructor will call default constructor of
class ABC and prints the output “HELLO
JOHN” on the console.
After printing the first output, the control
of execution again comes to the
parameterized constructor and prints the
output “WELCOME INDIA”.
Calling parameterized constructor from
default constructor
Program source code 7:
package thisKeyword;
public class CD
{
CD()
{
this(5); // Calling parameterized
constructor from default constructor.
System.out.println("Hello John");
}
CD(int x)
{
System.out.println(x);
System.out.println("You are so sweet");
}
}
public class CDclasstest
{
public static void main(String[] args)
{
CD cd = new CD();
}
}
Output:
5
You are so sweet
Hello John
Passing this keyword as Argument to
Method call

The keyword ‘this’ in Java can be used as


an argument while calling the method. It is
mainly used in event handling. Let’s take
an example program to understand the
concept.
Program source code 8:
package thisKeyword;
public class Test
{
// Declare an instance method whose a
parameter is t with class type Test.
void m1(Test t)
{
System.out.println("m1 method is
called");
}
void m2()
{
m1(this); // Passing this as an argument
in the m1 method. this keyword will pass
the reference of current class object to the
m1 method.
}
public static void main(String[] args)
{
Test t = new Test();
t.m2(); // m2 method is called.
}
}
Output:
m1 method is called
Passing this keyword as Argument in
Constructor call

We can also pass this keyword in the


constructor call. This concept is useful
when we have to use one object in multiple
classes. Let’s take an example program to
understand it better.
Program source code 9:
package thisKeyword;
public class A
{
B obj;
// Declare a parameterized constructor
whose parameter is obj with class type B.
A(B obj)
{
this.obj = obj;
}
void show()
{
System.out.println("Show method is
called");
System.out.println("Value of b: " +obj.b);
}
}
public class B
{
// Declare an instance variable with
initialization the value equal to 30.
int b = 30; // Declare a default
constructor.
B()
{
// Create an object of class A and pass
this as an argument to call the constructor
of class A. this keyword will pass the
reference of the current class object. Here,
B is the current class object.
A a = new A(this);
a.show(); // show() method of class A
is called.
}
public static void main(String[] args)
{
B obj = new B();
}
}
Output:
Show method is called
Value of b: 30
Return current class object using this
keyword in Java

The keyword ‘this’ can be returned as a


statement from the method. In such a case,
the return type of method must be class
type (non-primitive).
The following syntax can be used as a
returned statement:
Syntax:
return_type method_name()
{
return this;
}
Program source code 10:
package thisKeyword;
public class Animal
{
// Declare an instance method show with
return type Animal (class type).
Animal show()
{
return this;
}
void msg()
{
System.out.println("Lion is an animal");
}
public static void main(String[] args)
{
new Animal().show().msg();
}
}
Output:
Lion is an animal
SN ‘this’ keyword ‘super’ keyword
“super” is a reference
“this” is a reference variable that contains
1. variable that contains immediate
current class objects.
super class objects.
Any member of the If the method overrides
current class object from one of its super class’s
within an instance method, the overridden
2.
method or a constructor method can be called
can be referred by using through the use of super
this keyword. keyword.
‘super’ keyword is used
‘this’ keyword is used to
to call the super class’s
call another constructor
3. constructor from within a
from within a constructor
constructor of the
in the same class.
subclass.
By default JVM
JVM never put
automatically put the
automatically this()
4. super() keyword at first
keyword like super() in
line inside the
Java.
constructor.
Upcasting and Downcasting in Java with
Example
What is Upcasting (Widening) in Java?

When the reference variable of super class


refers to the object of subclass, it is known
as widening or upcasting in java.
In other words, when the subclass object
type is converted into superclass type, it is
called widening or upcasting.
Look at the below figure, where superclass
reference is pointing to the subclass object.
Let’s understand it with the help of a
suitable example. Suppose class One and
class Two are related classes through
inheritance. class One is a superclass and
class Two is subclass of One.
1. Super class reference is used to refer to
superclass object.
One o = new One();
In the above statement, class One’s
reference o is pointing or referring to
One’s object.
2. Similarly, sub class reference is used to
point to sub class object.
Two t = new Two();
In this statement, class Two’s reference t is
pointing to Two’s object.
In the above two statements, we do not
need casting. This is because on the left
side and at the right side of assignment
operator (=), we have the same data type.
For example, in the first statement, o is a
reference of class One. So, data type of o is
One.
At right side of assignment operator, we
have class One’s object. Data type of the
class object is also One. Therefore, casting
is not required in this case.
Till here, there is no need for casting. But
when a reference of class refers to different
class’s object, we need casting.
For example:
One o = new Two(); // class One's
reference o is pointing to class Two's
object.
In this statement, on the left side, the data
type of reference o is One but on the right
side, we got object whose data type is
Two. So, in this case, we will need casting.
We will convert object’s data type into
class O using cast operator. It can be done
like this:
One o = (One) new Two(); // Converting
class Two's type into class One.
Here, sub class object type has converted
into super class type. This kind of
conversion is called upcasting or widening
in java.
If we do not use the cast operator in the
above case, even we will not get any error
message. Java compiler will do the
implicit casting.
Example Based on Upcasting (Widening)

Let’s take an example program to see the


effect of upcasting where super class
reference refers to sub class object.
Program code 1:
package classCasting;
public class One
{
void m1()
{
System.out.println("m1 method in class
One");
}
}
public class Two extends One
{
void m2()
{
System.out.println("m2 method in class
Two");
}
}
public class Test
{
public static void main(String[] args)
{
One o = (One)new Two(); // Upcasting.
Here, super class reference o refers to sub
class object.
o.m1();
// o.m2(); // Compile-time error
message.
}
}
Output:
m1 method in class One
Program code 2:
package classCasting;
public class One
{
void m1()
{
System.out.println("m1 method in class
One");
}
}
public class Two extends One
{
void m1()
{
System.out.println("m1 method in class
Two");
}
}
public class Test
{
public static void main(String[] args)
{
One o = (One)new Two(); // Upcasting.
Here, super class reference o refers to sub
class object.
o.m1();
}
}
Output:
m1 method in class Two
As you can observe in the program that it
is possible to access sub class method but
not super class method when super class
method is overridden in sub class. This
shows that you can access only 50% of the
functionality.
Downcasting (Narrowing) in Java

When subclass reference refers to super


class object, it is called narrowing
or downcasting in java. In other words,
when sub class type is converted into super
class type, it is called downcasting.
Look at the below figure where subclass
reference is pointing to superclass object.

How Downcasting is possible?

To overcome this problem, we will have to


modify the previous code. So, look at the
following source code and modify it.
Program code 4:
package classCasting;
public class One
{
void m1()
{
System.out.println("m1 method in class
One");
}
}
public class Two extends One
{
void m2()
{
System.out.println("m2 method in
class Two");
}
}
public class Test
{
public static void main(String[] args)
{
One o = new Two(); // Super class
reference refers to sub class object.
Two t = (Two)o; // Converting super
class reference type into sub class
reference type.
t.m1(); // Calling m1 method using
reference variable of sub class.
t.m2(); // Calling m1 method using
reference variable of sub class.
}
}
Output:
m1 method in class One
m2 method in class Two
In the above program, superclass reference
type has converted into sub class reference
type. Now, we can call both methods m1()
and m2() using reference variable of sub
class.
In the preceding code, you can observe
that if we create an object of subclass, it is
possible to access all the methods of super
class and subclass. In this way,
downcasting is possible in java.
Java Lambda Expressions
Lambda expression is a new and important
feature of Java which was included in Java
SE 8. It provides a clear and concise way
to represent one method interface using an
expression. It is very useful in collection
library. It helps to iterate, filter and extract
data from collection.
The Lambda expression is used to provide
the implementation of an interface which
has functional interface. It saves a lot of
code. In case of lambda expression, we
don't need to define the method again for
providing the implementation. Here, we
just write the implementation code.
Functional Interface
Lambda expression provides
implementation of functional interface. An
interface which has only one abstract
method is called functional interface.
Why use Lambda Expression
To provide the implementation of
Functional interface.
Less coding.
Java Lambda Expression Syntax
(argument-list) -> {body}
Java lambda expression is consisted of
three components.
1) Argument-list: It can be empty or non-
empty as well.
2) Arrow-token: It is used to link
arguments-list and body of expression.
3) Body: It contains expressions and
statements for lambda expression
No Parameter Syntax
() -> {
//Body of no parameter lambda
}
One Parameter Syntax
(p1) -> {
//Body of single parameter lambda
}
Two Parameter Syntax
(p1,p2) -> {
//Body of multiple parameter lambda
}

Without Lambda Expression


interface Drawable{
public void draw();
}
public class LambdaExpressionExample {
public static void main(String[] args) {
int width=10;

//without lambda, Drawable implementatio


n using anonymous class
Drawable d=new Drawable(){
public void draw()
{System.out.println("Drawing "+width);}
};
d.draw();
}
}

Output:
Drawing 10
Java Lambda Expression Example
Now, we are going to implement the above
example with the help of Java lambda
expression.
@FunctionalInterface //It is optional
interface Drawable{
public void draw();
}

public class LambdaExpressionExample2


{
public static void main(String[] args) {
int width=10;

//with lambda
Drawable d2=()->{
System.out.println("Drawing "+wid
th);
};
d2.draw();
}
}
Test it Now
Output:
Drawing 10

Java Lambda Expression Example: No


Parameter
interface Sayable{
public String say();
}
public class LambdaExpressionExample3{

public static void main(String[] args) {


Sayable s=()->{
return "I have nothing to say.";
};
System.out.println(s.say());
}
}
Test it Now

I have nothing to say.

Java Lambda Expression Example: Single


Parameter
interface Sayable{
public String say(String name);
}
public class LambdaExpressionExample4{

public static void main(String[] args) {

// Lambda expression with single para


meter.
Sayable s1=(name)->{
return "Hello, "+name;
};
System.out.println(s1.say("Sonoo"));

// You can omit function parentheses

Sayable s2= name ->{


return "Hello, "+name;
};
System.out.println(s2.say("Sonoo"));
}
}
Test it Now
Output:
Hello, Sonoo
Hello, Sonoo

Java Lambda Expression Example:


Multiple Parameters
interface Addable{
int add(int a,int b);
}

public class LambdaExpressionExample5{

public static void main(String[] args) {

// Multiple parameters in lambda expr


ession
Addable ad1=(a,b)->(a+b);
System.out.println(ad1.add(10,20));

// Multiple parameters with data type i


n lambda expression
Addable ad2=(int a,int b)->(a+b);
System.out.println(ad2.add(100,200))
;
}
}
Test it Now
Output:
30
300

Java Lambda Expression Example: with or


without return keyword
In Java lambda expression, if there is only
one statement, you may or may not use
return keyword. You must use return
keyword when lambda expression contains
multiple statements.

interface Addable{
int add(int a,int b);
}

public class LambdaExpressionExample6


{
public static void main(String[] args) {

// Lambda expression without return k


eyword.
Addable ad1=(a,b)->(a+b);
System.out.println(ad1.add(10,20));
// Lambda expression with return key
word.
Addable ad2=(int a,int b)->{
return (a+b);
};
System.out.println(ad2.add(100,200))
;
}
}
Test it Now
Output:
30
300

What is constructor chaining in Java?


In Java, constructor chaining is a sequence
of invoking constructors
upon initializing an object. It is used when
we want to invoke a number of
constructors, one after another by using
only an instance. In this section, we will
discuss constructor chaining in Java in
detail with proper examples. Let's have a
quick look at what is a constructor in Java.
Constructor
In Java
, a constructor is the same as a method but
the only difference is that the constructor
has the same name as the class name. It is
used to create an instance of the class. It is
called automatically when we create an
object of the class. It has no return type.
Remember that a constructor cannot
be abstract
, final
, synchronized
, and static
. We cannot override a constructor.
There are two types of constructor in Java:
Default Constructor (also known as a no-
argument constructor)
Parameterized Constructor
Constructor Chaining
In constructor chain, a constructor is called
from another constructor in the same class
this process is known as constructor
chaining. It occurs through inheritance.
When we create an instance of a derived
class, all the constructors of the inherited
class (base class) are first invoked, after
that the constructor of the calling class
(derived class) is invoked.
We can achieve constructor chaining in
two ways:
Within the same class: If the constructors
belong to the same class, we use this
From the base class: If the constructor
belongs to different classes (parent and
child classes), we use the super keyword to
call the constructor from the base class.
Remember that changing the order of the
constructor does not affect the output.
The Need of Constructor Chaining
Suppose, there are five tasks to perform.
There are two ways to perform these tasks,
either implement all the tasks in a single
constructor or create separate tasks in a
single constructor.
By using the constructor chaining
mechanism, we can implement multiple
tasks in a single constructor. So, whenever
we face such types of problems, we should
use constructor chaining. We can make the
program more readable and understandable
by using constructor chaining.
Rules of Constructor Chaining
An expression that uses this keyword must
be the first line of the constructor.
Order does not matter in constructor
chaining.
There must exist at least one constructor
that does not use this
Constructor Calling form another
Constructor
The calling of the constructor can be done
in two ways:
By using this() keyword: It is used when
we want to call the current class
constructor within the same class.
By using super() keyword: It is used when
we want to call the superclass constructor
from the base class.
Note: In the same constructor block, we
cannot use this() and super()
simultaneously.
Constructor Chaining Examples
Calling Current Class Constructor
We use this() keyword if we want to call
the current class constructor within the
same class. The use of this() is mandatory
because JVM
never put it automatically like
the super() keyword. Note that this() must
be the first line of the constructor. There
must exist at least one constructor
without this() keyword
.
Syntax:
this(); or this(parameters list);
For example:

this();
this("Javatpoint");
Let's create a Java program and call the
current class constructor.
ConstructorChain.java

public class ConstructorChain


{
//default constructor
ConstructorChain()
{
this("Javatpoint");
System.out.println("Default constructor cal
led.");
}
//parameterized constructor
ConstructorChain(String str)
{
System.out.println("Parameterized constru
ctor called");
}
//main method
public static void main(String args[])
{
//initializes the instance of example class
ConstructorChain cc = new ConstructorCh
ain();
}

}
Output:

Calling Super Class Constructor


Sometimes, we need to call the superclass
(parent class) constructor from the child
class (derived class) in such cases, we use
the super() keyword in the derived class
constructor. It is optional to write super()
because JVM automatically puts it. It
should always write in the first line. We
get a syntax error if we try to call a
superclass constructor in the child class.
Syntax:

1. super(); or super(Parameter List);


super(): It calls the no-argument or default
constructor of the superclass.
super(parameters): It invokes the
superclass parameterized constructor.
Remember that the superclass constructor
cannot be inherited in the subclass. It can
be called from the subclass constructor by
using the super keyword.
Let's create a Java program and implement
constructor chaining in an inherited class.
ConstructorChaining.java
//parent class or base class
class Demo
{
//base class default constructor
Demo()
{
this(80, 90);
System.out.println("Base class default constr
uctor called");
}
//base class parameterized constructor
Demo(int x, int y)
{
System.out.println("Base class parameterize
d constructor called");
}
}
//derived class or child class
class Prototype extends Demo
{
//derived class default constructor
Prototype()
{
this("Java", "Python");
System.out.println("Derived class default co
nstructor called");
}
//derived class parameterized constructor
Prototype(String str1, String str2)
{
super();
System.out.println("Derived class parameter
ized constructor called");
}
}
public class ConstructorChaining
{
//main method
public static void main(String args[])
{
//initializes the instance of example class
Prototype my_example = new Prototype();
}
}
Output:

Accessing Protected Members in Java


In Java, there are four types of access
modifiers. These are public, private,
default, and protected. To get the idea of
these modifiers, you can refer to access
modifiers in java. In this article, we
discuss the accessibility of protected
members in different cases.
Now let us discuss various scenarios of
accessing protected members which are
listed below as follows:
1. Accessing in the same class
2. Accessing in other classes of the
same package
3. Accessing protected members of a
class in its subclass in the same
package
4. Accessing another class in a
different package
5. Accessing in sub-class in a different
package
Case 1: Accessing protected members in
the same class
// Java Program to Illustrate
// Accessing Protected Members
// in the same class

// Main class
class Sample {

protected int year = 2021;


protected void printYear()
{
System.out.println("Its " + year +
" !!");
}

public static void main(String[] args)


{
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
}
}
Output
2021
Its 2021 !!
Case 2: Accessing protected members in
other classes of the same package
// Java Program to Illustrate Accessing
// Protected Members
// In Other Class of Same Package

// Class 1
class Sample {
protected int year = 2021;
protected void printYear() {
System.out.println("Its
"+year+" !!");
}
}

// Class 2
public class Test {
// Main driver method
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
}
}
Output
2021
Its 2021 !!
Case 3: Accessing protected members of a
class in its subclass in the same package
// Java Program to Illustrate
// Accessing Protected Members
// of a class in its subclass
// in the same package
// Class 1
class Sample {
static protected String title =
"geekforgeeks";
protected int year = 2021;
protected void printYear() {
System.out.println("Its
"+year+" !!");
}
}

// Class 2
public class Test extends Sample {
public static void main(String[] args) {
Sample sample = new Sample();
System.out.println(sample.year);
sample.printYear();
System.out.println(Sample.title);
}
}

2021
Its 2021 !!
geekforgeeks
Case 4: Accessing protected members in
another class in a different package
We cannot access the protected members
of a class in a class (non-subclass) that is
present in a different package.
// Java Program to Illustrate Accessing
Protected
// Members in sub-class in a different
package

package package1;
// Class
public class Sample {

// Protected attributes
static protected String title =
"geeksforgeeks";
protected int year = 2021;
protected void printYear()
{
System.out.println("Its " + year +
" !!");
}
}

// Java Program to Illustrate Accessing


Protected
// Members in Sub-class in a different
Package
package package2;
// Importing class from above package
import package1.Sample;

// Main class
public class Child extends Sample {

// Method 1
void helper()
{
System.out.println(year);
printYear();
System.out.println(Sample.title);
}

// Method 2
// Main driver method
public static void main(String[] args)
{

// Creating child class instance


inside main()
Child child = new Child();
child.helper();
}
}

Garbage Collection in Java


Garbage collection in Java is the process
by which Java programs perform
automatic memory management. Java
programs compile to bytecode that can be
run on a Java Virtual Machine, or JVM for
short. When Java programs run on the
JVM, objects are created on the heap,
which is a portion of memory dedicated to
the program. Eventually, some objects will
no longer be needed. The garbage collector
finds these unused objects and deletes
them to free up memory.

What is Garbage Collection?


In C/C++, a programmer is responsible for
both the creation and destruction of
objects. Usually, programmer neglects the
destruction of useless objects. Due to this
negligence, at a certain point, sufficient
memory may not be available to create
new objects, and the entire program will
terminate abnormally, causing
OutOfMemoryErrors.

But in Java, the programmer need not care


for all those objects which are no longer in
use. Garbage collector destroys these
objects. The main objective of Garbage
Collector is to free heap memory by
destroying unreachable objects. The
garbage collector is the best example of the
Daemon thread as it is always running in
the background.

How Does Garbage Collection in Java


works?
Java garbage collection is an automatic
process. Automatic garbage collection is
the process of looking at heap memory,
identifying which objects are in use and
which are not, and deleting the unused
objects. An in-use object, or a referenced
object, means that some part of your
program still maintains a pointer to that
object. An unused or unreferenced object
is no longer referenced by any part of your
program. So the memory used by an
unreferenced object can be reclaimed. The
programmer does not need to mark objects
to be deleted explicitly. The garbage
collection implementation lives in the
JVM.
Types of Activities in Java Garbage
Collection
Two types of garbage collection activity
usually happen in Java. These are:

Minor or incremental Garbage Collection:


It is said to have occurred when
unreachable objects in the young
generation heap memory are removed.
Major or Full Garbage Collection: It is
said to have occurred when the objects that
survived the minor garbage collection are
copied into the old generation or
permanent generation heap memory are
removed. When compared to the young
generation, garbage collection happens less
frequently in the old generation.
Important Concepts Related to Garbage
Collection in Java
1. Unreachable objects: An object is said
to be unreachable if it doesn’t contain any
reference to it. Also, note that objects
which are part of the island of isolation are
also unreachable.

Integer i = new Integer(4);


// the new Integer object is reachable via
the reference in 'i'
i = null;
// the Integer object is no longer reachable.
garbage collection

2. Eligibility for garbage collection: An


object is said to be eligible for GC(garbage
collection) if it is unreachable. After i =
null, integer object 4 in the heap area is
suitable for garbage collection in the above
image.

Ways to make an object eligible for


Garbage Collector
Even though the programmer is not
responsible for destroying useless objects
but it is highly recommended to make an
object unreachable(thus eligible for GC) if
it is no longer required.
There are generally four ways to make an
object eligible for garbage collection.
Nullifying the reference variable
Re-assigning the reference variable
An object created inside the method
Island of Isolation
Ways for requesting JVM to run Garbage
Collector
Once we make an object eligible for
garbage collection, it may not destroy
immediately by the garbage collector.
Whenever JVM runs the Garbage
Collector program, then only the object
will be destroyed. But when JVM runs
Garbage Collector, we can not expect.
We can also request JVM to run Garbage
Collector. There are two ways to do it :
Using System.gc() method: System class
contain static method gc() for requesting
JVM to run Garbage Collector.
Using Runtime.getRuntime().gc() method:
Runtime class allows the application to
interface with the JVM in which the
application is running. Hence by using its
gc() method, we can request JVM to run
Garbage Collector.
There is no guarantee that any of the above
two methods will run Garbage Collector.
The call System.gc() is effectively
equivalent to the call :
Runtime.getRuntime().gc()
Finalization
Just before destroying an object, Garbage
Collector calls finalize() method on the
object to perform cleanup activities. Once
finalize() method completes, Garbage
Collector destroys that object.
finalize() method is present in Object class
with the following prototype.
protected void finalize() throws Throwable
Based on our requirement, we can override
finalize() method for performing our
cleanup activities like closing connection
from the database.
The finalize() method is called by Garbage
Collector, not JVM. However, Garbage
Collector is one of the modules of JVM.
Object class finalize() method has an
empty implementation. Thus, it is
recommended to override the finalize()
method to dispose of system resources or
perform other cleanups.
The finalize() method is never invoked
more than once for any object.
If an uncaught exception is thrown by the
finalize() method, the exception is ignored,
and the finalization of that object
terminates.
Advantages of Garbage Collection in Java
The advantages of Garbage Collection in
Java are:
It makes java memory-efficient because
the garbage collector removes the
unreferenced objects from heap memory.
It is automatically done by the garbage
collector(a part of JVM), so we don’t need
extra effort.
Real-World Example
Let’s take a real-life example, where we
use the concept of the garbage collector.

Question: Suppose you go for the


internship at GeeksForGeeks, and you
were told to write a program to count the
number of employees working in the
company(excluding interns). To make this
program, you have to use the concept of a
garbage collector.
This is the actual task you were given at
the company:

Write a program to create a class called


Employee having the following data
members.

1. An ID for storing unique id allocated to


every employee.
2. Name of employee.
3. age of an employee.

Also, provide the following methods:

A parameterized constructor to initialize


name and age. The ID should be initialized
in this constructor.
A method show() to display ID, name, and
age.
A method showNextId() to display the ID
of the next employee.
Now any beginner, who doesn’t know
Garbage Collector in Java will code like
this:

// Java Program to count number


// of employees working
// in a company

// Correct code to count number


// of employees excluding interns.

class Employee {

private int ID;


private String name;
private int age;
private static int nextId = 1;

// it is made static because it


// is keep common among all and
// shared by all objects
public Employee(String name, int age)
{
this.name = name;
this.age = age;
this.ID = nextId++;
}
public void show()
{
System.out.println("Id=" + ID + "\
nName=" + name
+ "\nAge=" + age);
}
public void showNextId()
{
System.out.println("Next employee
id will be="
+ nextId);
}
protected void finalize()
{
-nextId;
// In this case,
// gc will call finalize()
// for 2 times for 2 objects.
}
}
public class UseEmployee {
public static void main(String[] args)
{
Employee E = new
Employee("GFG1", 56);
Employee F = new
Employee("GFG2", 45);
Employee G = new
Employee("GFG3", 25);
E.show();
F.show();
G.show();
E.showNextId();
F.showNextId();
G.showNextId();

{
// It is sub block to keep
// all those interns.
Employee X = new
Employee("GFG4", 23);
Employee Y = new
Employee("GFG5", 21);
X.show();
Y.show();
X.showNextId();
Y.showNextId();
X = Y = null;
System.gc();
System.runFinalization();
}
E.showNextId();
}
}
Output
Id=1
Name=GFG1
Age=56
Id=2
Name=GFG2
Age=45
Id=3
Name=GFG3
Age=25
Next employee id will be=4
Next employee id will be=4
Next employee id will be=4
Id=4
Name=GFG4
Age=23
Id=5
Name=GFG5
Age=21
Next employee id will be=6
Next employee id will be=6
Next employee id will be=4

You might also like