0% found this document useful (0 votes)
276 views

12.30 PM Core Java Notes

The document provides an overview of classes, objects, static and non-static variables and methods in Java. It explains that classes are used to create objects using the new keyword. Static members belong to the class while non-static members belong to objects. Local variables are declared within methods and can only be accessed within the method, while static and non-static variables are declared at the class level. It also provides examples demonstrating the use of unary operators like increment/decrement and how methods are called.

Uploaded by

sai venkata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
276 views

12.30 PM Core Java Notes

The document provides an overview of classes, objects, static and non-static variables and methods in Java. It explains that classes are used to create objects using the new keyword. Static members belong to the class while non-static members belong to objects. Local variables are declared within methods and can only be accessed within the method, while static and non-static variables are declared at the class level. It also provides examples demonstrating the use of unary operators like increment/decrement and how methods are called.

Uploaded by

sai venkata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 116

Core Java Notes

Whats app number: 9632882052

Class:

1. Class helps us to create object.

2. Class will create object only when it receives request from new keyword.

new :

1. new keyword sends request to the class asking class to generate an object

2. Once the class has created an object new keyword will get the address of the object and will
store that in a special reference variable

Example 1:
public class A {

public static void main(String args[]) {

A a1 = new A();

A a2 = new A();

A a3 = new A();

System.out.println(a1);

System.out.println(a2);

System.out.println(a3);

Output:

A@7960847b

A@6a6824be

A@5c8da962

What is non static?

1. Non static members belongs to object. Whenever an object is created only non static member
gets loaded into the object.

Note: Static members never gets loaded into the object

Syntax to create an object:


ClassName variableName = new ClassName();

2. Non static members cannot be accessed / used without creating object

3. Every time we create an object. non static member will get loaded into it

Example 1:

public class A {

int i = 10;

public static void main(String[] args){

A a1 = new A();

System.out.println(a1.i);

Output:

10

What are static members ?

1. These members belongs to the class

2. To access static members do not create an object

3. Static members are accessed directly with class name

4. When a member is made static it gets loaded into the common memory of class

5. If a member is static then it will get loaded into the common memory only once throughout the
program

Example:

public class A {

int i = 10;

static int j = 100;

public static void main(String[] args){

System.out.println(A.j);
}

//if we are not using new keyword in the program, its means that no Object is crated

Example 1:

public class A {

int i = 10;

static int j = 100;

public static void main(String[] args){

A a1 = new A();

System.out.println(a1.i);

System.out.println(A.j);

Output:

10

100

Example 2:

public class A {

int i = 10;

int j = 100;

static int k = 500;

public static void main(String[] args){

A a1 = new A();

System.out.println(a1.i);

System.out.println(a1.j);

System.out.println(A.k);
}

Output:

10

100

500

Example 3:

public class A {

int i = 10;

public static void main(String[] args){

A a1 = new A();

System.out.println(a1.i);

A a2 = new A();

System.out.println(a2.i);

A a3 = new A();

System.out.println(a3.i);

//In the above program we are creating 3 objects, hence variable i will get loaded 3 times in the
object

Output:

10

10
10

Example 4:

public class A {

int i = 10;

static int j = 100;

public static void main(String[] args){

A a1 = new A();

System.out.println(a1.i);

A a2 = new A();

System.out.println(a2.i);

A a3 = new A();

System.out.println(a3.i);

System.out.println(A.j);

Output:

10

10

10

100

Unary Operators in Java

1. POST INCREMENT (++): Here we increment the values of the variable by one when next time we
see the same variable in our program
Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i++;

System.out.println(i);

System.out.println(j);

Output:

12

21

Example 2:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i++ + i++;

System.out.println(i);

System.out.println(j);

Output:

13

33
Example 3:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i++ + i++ + i++;

System.out.println(i);

System.out.println(j);

Output:

14

46

Example 4:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i + i++ ;

System.out.println(i);

System.out.println(j);

Output:

12

32
Example 5:

public class A {

public static void main(String args[]) {

int i = 5;

int j = i + i++ + i + i++ ;

System.out.println(i);

System.out.println(j);

Output:

22

2. PRE INCREMENT: Here we increment the value of the variable in same step

Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

int j = ++i + ++i;

System.out.println(i);

System.out.println(j);

Output:

12

23
Example 2:

public class A {

public static void main(String args[]) {

int i = 10;

int j = ++i + ++i + ++i;

System.out.println(i);

System.out.println(j);

Output:

13

36

Example 3:

public class A {

public static void main(String args[]) {

int i = 10;

int j = ++i + i + ++i;

System.out.println(i);

System.out.println(j);

Output:

12

34
Examples on post & pre increment together:

Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

int j = ++i + i++;

System.out.println(i);

System.out.println(j);

Output:

12

22

Example 2:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + ++i + i++ + i++;

System.out.println(i);

System.out.println(j);

Output:

14

47
Example 3:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i-- + i--;

System.out.println(i);

System.out.println(j);

Output:

19

Example 4:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ + i-- + --i + ++i;

System.out.println(i);

System.out.println(j);

Output:

10

40

Example 5:
public class A {

public static void main(String args[]) {

int i = 10;

System.out.println(i++);

System.out.println(++i);

System.out.println(--i);

System.out.println(i--);

System.out.println(i);

Output:

10

12

11

11

10

Example 6:

public class A {

public static void main(String args[]) {

int i = 10;

int j = i++ - ++i;

System.out.println(i);

System.out.println(j);

}
Output:

12

-2

Overview of methods:

1. Methods will execute only when we call it

Example 1:

public class A {

public static void main(String args[]) {

A a1 = new A();

a1.test();

public void test(){

System.out.println(500);

Output:

500

Example 2:

public class A {

public static void main(String args[]) {

A.test();

public static void test(){

System.out.println(500);

}
Output:
500

Types of Variables in Java

1. local variable

2. static variables

3. non static variables

4. reference variables

What are local variables:

1. Local variables are created inside a method

2. Local variables can be used only within created method

3. These variables are accessed directly with its name

4. Without initializing local variables if used then it will get you an error

Example 1:

public class A {

public static void main(String args[]) {

A a1 = new A();

a1.test();

public void test(){

int i = 10;

System.out.println(i);

Output:

10
Note: never use static keyword to create a variable inside a method:

Example 2:

public class A {

public static void main(String args[]) {

A a1 = new A();

a1.test();

public void test(){

static int i = 10;//Error

System.out.println(i);

Example 3:

public class A {

public static void main(String args[]) {

A a1 = new A();

a1.test();

System.out.println(i);//Error

public void test(){

int i = 10;

System.out.println(i);

Output:
Error

Example 4:

public class A {

public static void main(String args[]) {

int i = 10;

System.out.println(i);

Output:

10

Example 5:

Without initializing the variable if it is accessed then you would get an error

public class A {

public static void main(String args[]) {

int i ;

System.out.println(i);

Output:

Error

What are static variables?

1. static variables are created outside all the methods but inside a class with static keyword

2. Static variables belongs to the class and can be used anywhere in the class

3. If we do not initialize static variables then automatically depending on the data type some
default value will get stored in it

Example 1:
public class A {

static int i = 10;

public static void main(String args[]) {

System.out.println(A.i);

A a1 = new A();

a1.test();

public void test(){

System.out.println(A.i);

Output:

10

10

Example 2:

public class A {

static int i ; //0

public static void main(String args[]) {

System.out.println(A.i);

Output:

0
Example 3:

public class A {

static int i,j,k;

public static void main(String args[]) {

System.out.println(A.i);

System.out.println(A.j);

System.out.println(A.k);

Output:

What are non static variables ?

1. These variables are created outside all the methods but inside a class without using static
keyword

2. To access non static variables it is mandatory to create object

3. It is not mandatory to initialize non static variables, if we do not initialize then automatically
some default value will be stored in it

Example 1:

public class A {

int i = 10;

public static void main(String args[]) {

A a1 = new A();

System.out.print(a1.i);

}
}

Output:

10

Example 2:

public class A {

int i ;

public static void main(String args[]) {

A a1 = new A();

System.out.print(a1.i);

Output:

What are reference variables?

1. The main purpose of reference variable is to store objects address

2. The data type of a reference variable is class name

Syntax:

className variableName = new className();

Types of Reference variables:

1. Local reference variable:

a. These variables are created inside a method and should be used only within created method

Example 1:

public class A {

public static void main(String args[]) {

A a1 = new A();//Creating

System.out.println(a1);//Using in main
a1.test();//using in main

public void test(){

System.out.println(a1);//Outside the created method

Output:

/A.java:8: error: cannot find symbol

System.out.println(a1);//Outside the created method

symbol: variable a1

location: class A

1 error

Example 2:

public class A {

public static void main(String args[]) {

A a1 ; //I am creating a reference vaiable here

System.out.println(a1);

Output:

/A.java:4: error: variable a1 might not have been initialized

System.out.println(a1);

1 error

2. Static reference variable:


a. These variables are created outside all the methods but inside a class using static keyword

b. These variables can be used anywhere in the program

c. static reference variable by default will get initialized to null value if object is not created

Example 1:

public class A {

static A a1 = new A(); //Global visibility

public static void main(String args[]) {

System.out.println(a1);

a1.test();

public void test(){

System.out.println(a1);

Output:

A@7960847b

A@7960847b

Example 2:

public class A {

static A a1 ; //Global visibility

public static void main(String args[]) {

System.out.println(a1);

}
Output:

null

Data types in Java in Type casting

Data type Memory Size Default Value


byte 1 byte 0
short 2 bytes 0
int 4 bytes 0
long 8 bytes 0
float 4 bytes 0.0
double 8 bytes 0.0
boolean NA false
var(in JDK 1.10) NA NA
char 2 bytes Empty space
String (Class) Is Class Not A datatype null

Which data type I should use to store 10 digit mobile phone number?

Example 1:

public class A {

public static void main(String args[]) {

long mobilenumber = 9632882052L;

System.out.println(mobilenumber);

Output:

9632882052

Example :

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

float i = 10.3F;

System.out.println(i);

Output:

10.3

var datatype in java ?

a. var data type can store any kind of value in it

b. var data type cannot be static variable

c. var data type cannot be non static variable

Example 1:

public class A {

public static void main(String args[]) {

var i = true;

var j = "Pankaj";

var k = true;

var z = 10.3;

System.out.println(i);

System.out.println(j);

System.out.println(k);

System.out.println(z);

Output:
true

Pankaj

true

10.3

Example 2:

public class A {

static var i = 10;

public static void main(String args[]) {

Output:

A.java:2: error: 'var' is not allowed here

static var i = 10;

1 error

Example 3:

public class A {

var i = 10;

public static void main(String args[]) {

Output:

A.java:2: error: 'var' is not allowed here

var i = 10;
^

1 error

Note: Printing multiple values in java single print statement:

Example:

public class A {

public static void main(String args[]) {

int i = 10;

int j = 100;

System.out.println(i+" "+j);

Output:

10 100

Type Casting:

Process of converting a particular data type into required data type is called as type casting.

1. Auto Up casting:

a. converting smaller data type to bigger data type is called as auto up casting

b. During up casting if data loss happens then we would get an error

Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

long j = i;

System.out.println(j);

}
}

Output:

10

Example 2:

public class A {

public static void main(String args[]) {

long i = 10; //8 bytes

int j = i;

System.out.println(j);

Output:

Error because copying data from bigger memory to smaller memory is not an automated process

Example 3:

public class A {

public static void main(String args[]) {

float i = 10.3f; //4 bytes

double j = i;

System.out.println(j);

Output:

10.300000190734863

Example 4:

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

float i = 10.3f; //4 bytes

long j = i;

System.out.println(j);

Output:

Error because during data copying .3 decimal value will be lost

2. Explicit Down casting:

a. Converting bigger data type to smaller data type is called as explicit down casting

b. During explicit down casting data loss might happen

Example 1:

public class A {

public static void main(String args[]) {

int i = 10;

byte j = (byte)i;

System.out.println(j);

Example 2:

public class A {

public static void main(String args[]) {

long i = 10;

int j = (int)i;

System.out.println(j);

}
}

Output:

10

Example 3:

public class A {

public static void main(String args[]) {

double i = 10.3;

float j = (float)i;

System.out.println(j);

Output:

10.3

Example 4:

public class A {

public static void main(String args[]) {

float i = 10.3f;

int j = (int)i;

System.out.println(j);

Output:

10, in this case .3 value is lost and only integer part is copied in variable j

Example 5:

public class A {

public static void main(String args[]) {

float i = 10.3f;
long j = (long)i;

System.out.println(j);

Output:

10

Example 6:

public class A {

public static void main(String args[]) {

long i = 9632882052L;

int j = (int)i;

System.out.println(j);

Output:

1042947460

Extra examples on auto conversion:

Example 1:

public class A {

public static void main(String args[]) {

int i = 'b';

System.out.println(i);

Output: 98

Example 2:
public class A {

public static void main(String args[]) {

int i = 'ए';

System.out.println(i);

Output:

2319

Example 3:

public class A {

public static void main(String args[]) {

int i = 'அ';

System.out.println(i);

Output:

2949

Steps to install JDK:

Step 1: Download jdk 1.8 from oracle website

Step 2: Double click on .exe file and keep clicking on next button

Step 3: After installation you will see a folder created in the following path with the name java

C:\Program Files\Java

Steps to install eclipse:

Step 1: Download eclipse from Google

Note: ensure you are downloading eclipse for java ee

Step 2: Extract the zipped file and then click on eclipse.exe


Note: you will get a pop up window with the name work space launcher. In this window you can
give the path where you want all you java projects being created in eclipse to be stored in you
computer

Step to Configure JDK with eclipse:

Step 1: In eclipse go to windows>> then preferences

Step 2: Type installed JRE

Step 3: Click on add button

Step 4: Select Standard VM and click on next

Step 5: Browse JRE home by clicking on directory button and select the following path on your
computer:

C:\Program Files\Java\jdk1.8.0_144

Step 6: Click on finish and select the check box JDK 1.8 and click okay and close

Steps to create Java Project in eclipse:

Step 1: Go to file>> new >> Select Project

Step 2: In the wizards type java, then select java project and click on next and give project name
example: app1

Steps to create Java class in Java Project

Step 1: In src folder of your project right click and select class

Step 2: Give class Name example: A and click on finish

Shortcuts in eclipse:

1. type main and then press control + space bar+enter

2. Syso then press control + space bar

Methods in Java

Rules to develop methods:

Rule 1: Execution of the program in java starts from opening bracket of main method
Rule 2: When a method calling statement executes, control is transferred to the matching method

Rule 3: When closing bracket of user defined method runs, control is transferred back to the
matching calling statement

Rule 4: When closing bracket of main method runs the complete program execution would stop

Example 1:

package methods_example_1;

public class A {

public static void main(String[] args) {//Rule 1

A a1 = new A();

a1.test();//Rule 2

}//Rule 4 STOP the program execution

public void test() {//Control Come Here

System.out.println(100);//NO Rule

}//rule 3

Output:

100

Example 2:

package methods_example_1;

public class A {

public static void main(String[] args) {//Rule 1 START

System.out.println(5);//No Rule

A a1 = new A();//NO Rule


a1.test();//Rule 2

System.out.println(10);//No Rule

}//Rule 4

public void test() {//COmes here

System.out.println(100);//No Rule

Output:

100

10

Example 3:

package methods_example_1;

public class A {

public static void main(String[] args) {//Rule 1 STARTS

A a1 = new A();//NO Rule

a1.test1();//Rule 2

a1.test2();//Rule 2

}//Rule 4 STOPS

public void test1() {//Comes Here

System.out.println(5);//No Rule

}//Rule 3

public void test2() {//Comes here

System.out.println(10);//No Rule

}//Rule 3

Output:

5
10

Example 4:
package methods_example_1;

public class A {
public static void main(String[] args) {//Rule 1 STARTS HERE
A.test1();//Rule 2
A.test2();//Rule 2
}//Rule 4
public static void test1() {//Comes here
System.out.println(5);//No Rule Applied
}//Rule 3
public static void test2() {//Comes Here
System.out.println(10);//No Rule
}//Rule 3
}
Output:
5
10

Example 5
package methods_example_1;

public class A {
public static void main(String[] args) {//Rule STARTS HERE
A.test1();//Rule 2
}//Rule 4 STOPS
public static void test1() {//Comes here
System.out.println(5);//No Rule
A a1 = new A();//No Rule
a1.test2();//Rule 2
}//Rule 3
public void test2() {//Comes here
System.out.println(100);//No Rule
}//Rule 3
}
Output:
5
100

Example 6:
package methods_example_1;

public class A {
public static void main(String[] args) {//Rule 1 STARTS HERE
System.out.println(10);//NO Rule
A a1 = new A();//NO Rule
a1.test1();// Rule 2
System.out.println(15);// NO Rule
A a2 = new A();// No Rule
a2.test1();//Rule 2
}//Rule 4 STOPS

public void test1() {


System.out.println(50);// NO Rule
}//Rule 3
}
Output:
10
50
15
50
Example 7:

void: when a method is void then it means that the method cannot return any
value
package methods_example_1;

public class A {

public static void main(String[] args) {


A a1 = new A();
int i = a1.test();
System.out.println(i);
}

public void test() {


return 500;
}

}
Output:
Error, because void methods cannot return any value

Example 8:

For a method to return value it should not be void as shown in the below
program

package methods_example_1;

public class A {

public static void main(String[] args) {


A a1 = new A();
int i = a1.test();
System.out.println(i);
}

public int test() {


return 500;
}

Output:
500

Note: Control + 1

Example 9:
package methods_example_1;

public class A {

public static void main(String[] args) {


A a1 = new A();
char i = a1.test();
System.out.println(i);
}

public char test() {


return 'a';
}

}
Output:

Example 10:

package methods_example_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

double i = a1.test();

System.out.println(i);

public double test() {

return 10.3;

Output:

10.3

Example 11:

package methods_example_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

String i = a1.test();

System.out.println(i);
}

public String test() {

return "Pankaj Sir Academy";

Output:

Pankaj Sir Academy

What is the difference between using only "return" keyword in method and
using " return value" keyword

1. we can use only "return" keyword in void methods. return keyword in void
methods helps us to return the control back to the calling statement. Using
return keyword in void methods is optional

Example 1:

package methods_example_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

a1.test();

public void test() {

return;//We are returning the control back to the calling


statement

Note: After return keyword if we write any java code then that code will
never execute. Such java code will give us an error "Unreachable" statement
Example 2:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

a1.test();

public void test(){

return;

System.out.println(100);//Never Execute

Output:

Error

Example 3:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

a1.test();

public void test(){

System.out.println(100);//Never Execute
return;

Output:

100

Example 4:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

int i = a1.test();

System.out.println(i);

public int test(){

return 500;

System.out.println(100);//Never Execute

Output:

Unreachable Code Error

Example 5:

package methods_examples_1;

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

A a1 = new A();

int i = a1.test();

System.out.println(i);

public int test(){

System.out.println(100);//Never Execute

return 500;

Output:

100

500

Example 6:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

a1.test(100, 200);

public void test(int i, int j){

System.out.println(i);

System.out.println(j);

}
}

Output

100
200

Example 7:

package methods_examples_1;

public class A {

public static void main(String[] args) {

A a1 = new A();

a1.test(100, "Pankaj",'p',true, 10.3);

public void test(int i, String s, char c, boolean b, double d ){

System.out.println(i);

System.out.println(s);

System.out.println(c);

System.out.println(b);

System.out.println(d);

Output:

100

Pankaj

true

10.3

Example 8:

package methods_examples_1;
public class A {

public static void main(String[] args) {

A a1 = new A();

a1.test(100,200,300,400,500);

public void test(int... x){

System.out.println(x[0]);

System.out.println(x[1]);

System.out.println(x[2]);

System.out.println(x[3]);

System.out.println(x[4]);

Output:

100

200

300

400

500

Method Definition: Methods helps us to break our programs into modules and
the code in the module can called any number of times which gives us
reusability of the code

Example 9:

package methods_examples_1;

public class A {

public static void main(String[] args) {

return;

}
Output:

Compile

Constructors In Java

a. Constructors should have same name as that of class

b. Whenever we create an object constructor will be called

c. We can create more than one constructor in the same class provided they
have different number of arguments or different type of arguments

d. By default constructors are internally void

e. When an object is created with no args, then if we do not create a


constructor automatically empty body constructor gets added up in .class
file

Example 1:

package methods_examples_1;

public class A {

A(){

System.out.println("From Constructor A");

public static void main(String[] args) {

A a1 = new A();

A a2 = new A();

Output:

From Constructor A

From Constructor A

Example 2:

package methods_examples_1;
public class A {

A(int i){

System.out.println(i);

public static void main(String[] args) {

A a1 = new A(500);

Output:

500

Example 3:

note: In the below program when we add void before the constructor then
that's no longer treated as constructor, it would be treated as a method

package methods_examples_1;

public class A {

void A(){

System.out.println("From Constructor A");

public static void main(String[] args) {

A a1 = new A();

Output:

Will Compile, but will print nothing

Example 4:

package methods_examples_1;
public class A {

void A(){

System.out.println("From Constructor A");

public static void main(String[] args) {

A a1 = new A();

a1.A();

Output:

From Constructor A

Example 5:

package methods_examples_1;

public class A {

A(){// No Of Args is ZERO

System.out.println("From Constructor One");

A(){// No Of Args is ZERO

System.out.println("From Constructor One");

public static void main(String[] args) {

A a1 = new A();

Output:
Error because in the above program both of the constructor have same number
of arguments

Example 6:

package methods_examples_1;

public class A {

A(){// No Of Args is ZERO

System.out.println("From Constructor One");

A(int i){// No Of Args is ONE

System.out.println(i);

public static void main(String[] args) {

A a1 = new A();

A a2 = new A(100);

Output:

From Constructor One

100

Example 7:

package methods_examples_1;

public class A {

A(){// No Of Args is ZERO

System.out.println("From Constructor One");

A(int i){// No Of Args is ONE

System.out.println(i);

A(int i,int j){// No Of Args is TWO


System.out.println(i);

System.out.println(j);

public static void main(String[] args) {

A a1 = new A();

A a2 = new A(100);

A a3 = new A(500,300);

Output:

From Constructor One

100

500

300

Example 8:

package methods_examples_1;

public class A {

A(int i){//No Of args=1 and type=int

System.out.println(i);

A(char j){//No Of args=1 and type=char

System.out.println(j);

public static void main(String[] args) {

A a1 = new A(100);

A a2 = new A('a');

Output:
100

Example 9:

package methods_examples_1;

public class A {

A(int i){//No Of args=1 and type=int

System.out.println(i);

A(char j){//No Of args=1 and type=char

System.out.println(j);

A(String i){

System.out.println(i);

public static void main(String[] args) {

A a1 = new A(100);

A a2 = new A('a');

A a3 = new A("Pankaj Sir Academy");

Output:

100

Pankaj Sir Academy

Example 10:

package constructor_example;

public class A {

A(){
return 100;

public static void main(String[] args) {

A a1 = new A();

Output:

Error

Example 11:

package constructor_example;

public class A {

A() {

System.out.println(100);

return;

public static void main(String[] args) {

A a1 = new A();

Output:

100

Example 12:

package constructor_example;

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

A a1 = new A(100);

Output:

Error, because it is mandatory to create a constructor when an object with


argument is created

this keyword

a. this keyword is a special reference variable in java that holds current


objects address

b. using this keyword we can access non static members of the class

c. We cannot use this keyword in static methods

d. this keyword gets added automatically in non static methods while access
the objects member

e. Using this keyword we can call current class constructor, but the call
has be from another constructor

f. Using this keyword when you call a constructor it has to be the first
statement inside another construtor

Example 1:

package constructor_example;

public class A {

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1);

a1.test();

public void test(){

System.out.println(this);
}

Output:

constructor_example.A@15db9742

constructor_example.A@15db9742

Example 2:

package constructor_example;

public class A {

int i = 10;

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

public void test(){

System.out.println(this.i);

Output:

10

10

Example 3:

package constructor_example;

public class A {

public static void main(String[] args) {

A a1 = new A();
a1.test1();

public void test1(){

System.out.println("From test 1");

this.test2();

public void test2(){

System.out.println("From test 2");

Output:

From test 1

From test 2

Example 4:

package constructor_example;

public class A {

public static void main(String[] args) {

A a1 = new A();

System.out.println(this);

Output:

Error because we cannot use this keyword in static method

Example 5:

package constructor_example;

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

A a1 = new A();

a1.test();

public static void test(){

System.out.println(this);

Output:

error because we cannot use this keyword in static method

Example 6:

package constructor_example;

public class A {

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1);

a1.test();

A a2 = new A();

System.out.println(a2);

a2.test();

public void test(){

System.out.println(this);

Output:
constructor_example.A@15db9742

constructor_example.A@15db9742

constructor_example.A@6d06d69c

constructor_example.A@6d06d69c

Example 7:

package constructor_example;

public class A {

static int i = 10;

int j = 100;

public static void main(String[] args) {

A a1 = new A();

a1.test();

public void test(){

System.out.println(this.i);

System.out.println(this.j);

Output:

10

100

Example 8:

package constructor_example;

public class A {

int i = 100;

public static void main(String[] args) {

A a1 = new A();
a1.test();

public void test(){

System.out.println(i);

//this keyword gets added automatically

//in non static methods while

//access the objects member

Output

100

Example 9:

package constructor_example;

public class A {

int i = 100;

public static void main(String[] args) {

A a1 = new A();

a1.test();

public static void test(){

System.out.println(i);

//this keyword cannot get added automatically

//in static methods while

//access the objects member

Output:
Error

Example 10:

package constructor_example;

public class A {

int i = 100;

public static void main(String[] args) {

A a1 = new A();

a1.test();

public void test(){

System.out.println(i);

x();//this keyword is added automatically

public void x(){

System.out.println("From x");

Output:

100

From x

Example 11:

package this_examples;

public class A {

A(){

System.out.println("From Constructor A");

A(int i){
this();

public static void main(String[] args) {

A a1 = new A(100);

Output:

From Constructor A

Example 12:

While calling a constructor using this keyword, it cannot be second


statement inside another constructor

package this_examples;

public class A {

A(){

System.out.println("From Constructor A");

A(int i){

System.out.println(i);

this();

public static void main(String[] args) {

A a1 = new A(100);

}
}

Output:

Error because this keyword is second statement

Example 12:

package this_examples;

public class A {

A(){

System.out.println("From Constructor A");

A(int i){

this();

System.out.println(i);

public static void main(String[] args) {

A a1 = new A(100);

Output:

From Constructor A

100

Example 13:

package this_examples;

public class A {

A(){
this(100);

A(int i){

System.out.println(i);

public static void main(String[] args) {

A a1 = new A();

Output:

100

Note:

Local variable name and non static variable name can be same

Example 1

package this_examples;

public class A {

int i = 20;//Non static

public static void main(String[] args) {

int i = 10;//Local Variable

Output:

Will Compile but will print nothing


Note: Using this keyword we cannot access local variables

Example 1:

package this_examples;

public class A {

public static void main(String[] args) {

public void test(){

int i = 10;

System.out.println(this.i);

Output:

Error because we cannot use this keyword to access local variables

Example 2:

package this_examples;

public class A {

int i;//0

public static void main(String[] args) {

A a1 = new A();

a1.test();

public void test(){

int i = 10;

System.out.println(this.i);
}

Output:

Example 3:

package this_examples;

public class A {

int i;//0

public static void main(String[] args) {

A a1 = new A();

a1.test();

public void test(){

int i = 10;

i = this.i;

System.out.println(i);

Output:

Example 4:

package this_examples;

public class A {

int i;//0
public static void main(String[] args) {

A a1 = new A();

a1.test();

public void test(){

int i = 10;

this.i = i;

System.out.println(i);

System.out.println(this.i);

Output:

10

10

Instance Initialization Block (IIB)

a. IIB will be called whenever an object is created

b. When there is more than one IIB then these IIB's will run in sequence
one after another when an object is created

c. Always IIB runs first and then the constructor

Example 1:

public class A {

System.out.println(100);

public static void main(String[] args) {

A a1 = new A();

A a2 = new A();
A a3 = new A();

Output:

100

100

100

Example 2:

package iib_examples;

public class A {

System.out.println(50);

System.out.println(100);

System.out.println(500);

public static void main(String[] args) {

A a1 = new A();

Output:

50

100
500

Example 3:

package iib_examples;

public class A {

A(){

System.out.println(100);

System.out.println(50);

public static void main(String[] args) {

A a1 = new A();

Output:

50

100

Example 4:

package iib_examples;

public class A {

System.out.println(20);

A(){
System.out.println(100);

System.out.println(50);

public static void main(String[] args) {

A a1 = new A();

Output:

20

50

100

Example 5:

package iib_examples;

public class A {

System.out.println(20);

A(int i){

System.out.println(i);

System.out.println(50);

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

A a1 = new A(100);

Output:

20

50

100

static initialization block (SIB):

a. SIB runs automatically and it runs before main method

b. If we have more than one SIB in the program then it will run in sequence
one after another

Example 1:

package iib_examples;

public class A {

static {

System.out.println(100);

public static void main(String[] args) {

System.out.println(50);

Output:

100
Example 2:

package iib_examples;

public class A {

static {

System.out.println(100);

Output:

Error, Because main method is mandatory to run a program

Example 3:

package iib_examples;

public class A {

static {

System.out.println(100);

static{

System.out.println(500);

public static void main(String[] args) {

System.out.println(1000);

Output:

100

500

1000

Interview Question:
Write a java program to call main method twice?

Example 1:

public class A {

static {

A.main(null);

A.main(null);

A.main(null);

public static void main(String args[]) {

System.out.println(500);

Output:

500

500

500

500

Example 1:

package sib_iib_constructors_example;

public class A {

System.out.println(5);

static{

System.out.println(10);

A(){

System.out.println(100);

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

System.out.println(500);

Output:

10

500

Example 2:

Note: Always static block will run first and then main method, and if
object is created then Instance initialization block will run and finally
constructor would run.

package sib_iib_constructors_example;

public class A {

System.out.println(5);

static{

System.out.println(10);

A(){

System.out.println(100);

public static void main(String[] args) {

System.out.println(500);

A a1 = new A();

Output:

10

500

5
100

Example 2:

package sib_iib_constructors_example;

public class A {

System.out.println(5);

static{

System.out.println(10);

A(){

System.out.println(100);

System.out.println(1);

public static void main(String[] args) {

System.out.println(500);

A a1 = new A();

Output:

10

500

100

Example 3:

package sib_iib_constructors_example;

public class A {
static {

System.out.println(1000);

System.out.println(5);

static{

System.out.println(25);

System.out.println(9);

A(){

System.out.println(11);

public static void main(String[] args) {

A a1 = new A();

System.out.println(600);

Output:

1000

25

11

600

Example 4:

package sib_iib_constructors_example;

public class A {
static {

A a1 = new A();

System.out.println(5);

A(){

System.out.println(10);

public static void main(String[] args) {

Output:

10

Purpose of IIB?

Whenever an object is created lets initialize all non static variables in


one place and that will give us better clarity of code

Example 1:

package sib_iib_constructors_example;

public class A {

int i,j,k;

i = 10;

j = 20;

k = 30;

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

A a1 = new A();

System.out.println(a1.i);

System.out.println(a1.j);

System.out.println(a1.k);

Output:

10

20

30

Note:

In IIB both the non static variables and static variables can be
initialized

Example 2:

package sib_iib_constructors_example;

public class A {

int i;

static int j;

i = 10;

j = 100;

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

System.out.println(A.j);

}
}

10

100

What is the purpose of SIB?

All static variables are initialized in SIB for better clarity of the code

Example 1:

package sib_iib_constructors_example;

public class A {

static int i,j,k;

static{

i = 10;

j = 20;

k = 30;

public static void main(String[] args) {

System.out.println(A.i);

System.out.println(A.j);

System.out.println(A.k);

Output:

10

20

30

Note:

In SIB we can never initialize non static variables

Example 2:
package sib_iib_constructors_example;

public class A {

int i,j,k;

static{

i = 10;

j = 20;

k = 30;

public static void main(String[] args) {

System.out.println(A.i);

System.out.println(A.j);

System.out.println(A.k);

Output

Error

OOPS

1. inheritance

2. Polymorphism

3. encapsulation

4. abstraction

Inheritance:

a. Here we inherit non static members of parent class into child class
object

Example 1:

package inheritance_examples;

public class A {

int i = 10;
}

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

System.out.println(b1.i);

Output:

10

Example 2:

public class A {

int i = 10;

public void test(){

System.out.println(1000);

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

System.out.println(b1.i);

b1.test();

Output:

10

1000

Note: In the below program class A and class B are non sub class

Example 3:
package inheritance_examples;

public class A {

int i = 10;

public void test(){

System.out.println(1000);

package inheritance_examples;

public class B{//Class B and class A are non sub class

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

Output:

10

1000

Example 4:

package inheritance_examples;

public class A {

public void test1(){

System.out.println(1000);

package inheritance_examples;

public class B extends A{//test1() + test2()

public void test2(){

System.out.println(500);

}
}

package inheritance_examples;

public class C extends B{//test1() + test2()

public void test3(){

System.out.println(10);

public static void main(String[] args) {

C c1 = new C();

c1.test1();

c1.test2();

c1.test3();

Output:

1000

500

10

Example 5:

Note: In java at class level multiple inheritance is not allowed. Multiple


inheritance makes the design of your software very complex

The below program throws an error, because of multiple inheritance

package inheritance_examples;

public class A {

package inheritance_examples;
public class B {

package inheritance_examples;

public class C extends A,B{//Error

Output:

Error

Note:

static members are not inherited, but it gives us a feel of inheritance in


the following program by converting B.i to A.i

Example 6:

package inheritance_examples;

public class A {

static int i;

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

System.out.println(B.i);//A.i

Output:

0
Example 7:

Note: static members are not inherited, but it gives us a feel of


inheritance in the following program by converting B.i to A.i and B.test()
to A.test()

package inheritance_examples;

public class A {

static int i;

public static void test(){

System.out.println("From Test");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

System.out.println(B.i);//A.i

B.test();//A.test()

Output:

From Test

Example 8:

package inheritance_examples;

public class A {

static int i;
public static void test(){

System.out.println("From Test");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

System.out.println(b1.i);//A.i

b1.test();//A.test();

Output:

From Test

Super keyword in Java

a. Using super keyword we can access members of parent class

b. We cannot use super keyword in static methods

c. Using super keyword we can call constructor of parent class, but the
call has to be made from child class constructor

d. super keyword cannot be second statement inside child class constructor


while calling parent class constructor

Example 1:

package inheritance_examples;

public class A {

int i;

}
package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

b1.test();

public void test(){

System.out.println(super.i);

Output:

Example 2:

package inheritance_examples;

public class A {

int i;

public void x(){

System.out.println("x");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();
b1.test();

public void test(){

System.out.println(super.i);

super.x();

Output:

Example 3:

package inheritance_examples;

public class A {

int i;

public void x(){

System.out.println("x");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

b1.test();

public static void test(){

System.out.println(super.i);
super.x();

Output:

Error because we should not use super keyword inside a static method.

Example 4:

Note: Using super keyword we can access static and non static members, but
it is advisable that we should not access static members with super keyword

package inheritance_examples;

public class A {

static int i;

public static void x(){

System.out.println("x");

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

b1.test();

public void test(){

System.out.println(super.i);

super.x();

}
Output:

Example 5:

package inheritance_examples;

public class A {

A(){

System.out.println("From Consructor A");

package inheritance_examples;

public class B extends A{

B(){

super();

public static void main(String[] args) {

B b1 = new B();

Output:

From Consructor A

Example 6:

package inheritance_examples;

public class A {

A(int i){
System.out.println(i);

package inheritance_examples;

public class B extends A{

B(){

super(100);

public static void main(String[] args) {

B b1 = new B();

Output:

100

Example 7:

package inheritance_examples;

public class A {

A(int i){

System.out.println(i);

package inheritance_examples;

public class B extends A{

B(){

System.out.println(500);
super(100);

public static void main(String[] args) {

B b1 = new B();

Output:

Error because super keyword cannot be second statement inside child class
constructor while calling parent class constructor

Example 8:

package inheritance_examples;

public class A {

A(){

System.out.println(100);

package inheritance_examples;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

Output:

The above program will print 100 because during compilation java compiler
will automatically create constructor with no args and super keyword in
.class file of B
Packages In Java

Definition: Packages are nothing but folders created in java to store your
programs in organized manner

Packages resolves class naming convention

Example 1:

package p1;

public class A {

In the above package keyword defines that class A is created in folder p1.

Note:

If you want to access a class present in different package then we have to


firstly import that class and only then use that as shown in the below
program!!

Example 1:

package p1;

public class A {

package p2;

import p1.A;

public class B extends A{

public static void main(String[] args) {

Example 2:

package p1;

public class A {

package p1;
public class C extends A{

public static void main(String[] args) {

Example 3:

package p1;

public class A {

package p2;

public class B extends p1.A{

public static void main(String[] args) {

p1.A a1 = new p1.A();

Example 4:

package p3.p4.p5;

public class D {

Example 5:

package p2;

import p3.p4.p5.D;

public class B extends D{

public static void main(String[] args) {

D d1 = new D();

}
}

package p3.p4.p5;

public class D {

Example 6:

package p1;

public class A {

package p1;

public class C extends A{

public static void main(String[] args) {

package p2;

import p1.A;

import p1.C;

public class B {

public static void main(String[] args) {

A a1 = new A();

C c1 = new C();

Example 7:

package p1;

public class A {
}

package p1;

public class C extends A{

public static void main(String[] args) {

package p2;

import p1.*;

public class B {

public static void main(String[] args) {

A a1 = new A();

C c1 = new C();

Access Specifier / Access Modifiers

1. private

2. default

3. protected

4. public

private default protected public


Same class yes Yes yes yes

Same package
subclass NO Yes yes yes

Same package
non subclass NO Yes yes yes
different
package No No yes yes
subclass

different
package non No No No yes
subclass

Private:

a. private members are the class are accessible in the same class

Example 1:

package p1;

public class A {

private int i = 10;

private void test(){

System.out.println("From test");

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

Output:

10

From test

Example 2:

private members cannot be accesses in same package sub class

package p1;

public class A {

private int i = 10;

private void test(){


System.out.println("From test");

package p1;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

System.out.println(b1.i);

b1.test();

Output:

error

Note: private members cannot be accessed in different non subclass same


package

Example 3:

package p1;

public class A {

private int i = 10;

private void test(){

System.out.println("From test");

package p1;

public class B {

public static void main(String[] args) {


A a1 = new A();

System.out.println(a1.i);

a1.test();

Output:

error

Note: private members cannot be accessed in different package sub class

Example 4:

package p1;

public class A {

private int i = 10;

private void test(){

System.out.println("From test");

package p2;

import p1.A;

public class C extends A{

public static void main(String[] args) {

C c1 = new C();

System.out.println(c1.i);

c1.test();

Output:

Error
Note: private members cannot be accessed in different package non sub class

Example 5:

package p1;

public class A {

private int i = 10;

private void test(){

System.out.println("From test");

package p2;

import p1.A;

public class C {

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

Output:

Error

Important Note:

Private members are accessible only in same class, outside that class
private members cannot be used

default access specifier:

These members are accessible only in same package, outside the package tese
members cannot be accessed

Example 1:

package p1;
public class A {

int i = 10;

void test(){

System.out.println("From Test");

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

Output:

10

From Test

Example 2:

package p1;

public class A {

int i = 10;

void test(){

System.out.println("From Test");

package p1;
public class B extends A{

public static void main(String[] args) {

B b1 = new B();

System.out.println(b1.i);

b1.test();

Output:

10

From Test

Example 3:

package p1;

public class A {

int i = 10;

void test(){

System.out.println("From Test");

package p1;

public class B {

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);
a1.test();

Output:

10

From Test

Example 4:

package p1;

public class A {

int i = 10;

void test(){

System.out.println("From Test");

package p1;

public class A {

int i = 10;

void test(){

System.out.println("From Test");

}
Example 5:

package p1;

public class A {

int i = 10;

void test(){

System.out.println("From Test");

package p2;

import p1.A;

public class C {

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

Protected Access Specifier:

protected members can be accessed in same package and in different package


onlythrough inheritance

Example 1:

package p1;

public class A {

protected int i = 10;

protected void test(){


System.out.println("From Test");

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

Output:

10

From Test

Example 2:

package p1;

public class A {

protected int i = 10;

protected void test(){

System.out.println("From Test");

package p1;

public class B extends A{

public static void main(String[] args) {

B b1 = new B();

System.out.println(b1.i);
b1.test();

Example 3:

package p1;

public class A {

protected int i = 10;

protected void test(){

System.out.println("From Test");

package p1;

public class B {

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

Example 4:

package p1;

public class A {
protected int i = 10;

protected void test(){

System.out.println("From Test");

package p2;

import p1.A;

public class C extends A{

public static void main(String[] args) {

C c1 = new C();

System.out.println(c1.i);

c1.test();

Output:

10

From Test

Example 5:

package p1;

public class A {

protected int i = 10;

protected void test(){


System.out.println("From Test");

package p2;

import p1.A;

public class C{

public static void main(String[] args) {

A a1 = new A();

System.out.println(a1.i);

a1.test();

Output:

Error

Public access specifier:

Public access specifier can be accessed anywhere in the project

Note: A class in java can be public or default. It cannot be private or


protected

Example 1:

In the below example we get an error because default class cannot be


accessed in different package

package p1;

class A {

}
package p2;

import p1.A;

public class B extends A{

Output: Error

Note:

1. If a constructor is made public then its object can be created anywhere


in the project

2. If a constructor is made default then its object cannot be created in


different package

3. If a constructor is made private then its object can be created in same


class

4. If a constructor is made protected then its object can be created in


same package only just like default constructor

Polymorphism In Java:

When we develop a feature in java that can take more than one form is
called as polymorphism.

Their two ways to achieve polymorphism:

a. Overriding:

Note: How to modify the logic of inherited method in child class:

Definition: In Overriding we inherit a method from parent class and then we


modify the logic of inherited method in child class by once again creating
a method with same signature in child class

Note: Polymorphisms applied only on methods

Example 1:

package polymorphism_examples;

public class A {

public void test(){


System.out.println("Hello");

package polymorphism_examples;

public class B extends A{

public void test(){

System.out.println("world");

public static void main(String[] args) {

B b1 = new B();

b1.test();

Output:

world

Example 2:

package polymorphism_examples;

public class A {

public void test(){

System.out.println("Hello");

package polymorphism_examples;
public class B extends A{

public void test(){

System.out.println("world");

public static void main(String[] args) {

B b1 = new B();

b1.test();

A a1 = new A();

a1.test();

Output:

world

Hello

Note:

While overriding it is advised that we use the annotation @Override. This


annotation will check whether overriding is happening or not

Example 3:

package polymorphism_examples;

public class A {

public void test(){

System.out.println("Hello");

}
package polymorphism_examples;

public class B extends A{

@Override

public void tests(){

System.out.println("world");

public static void main(String[] args) {

B b1 = new B();

b1.test();

Output:

Error because parent class method name and child's class method names are
not matching

Example 4:

package polymorphism_examples;

public class A {

public void test1(){

System.out.println("Hello");

public void test2(){

System.out.println("Welcome");

package polymorphism_examples;

public class B extends A{


@Override

public void test1(){

System.out.println("world");

public static void main(String[] args) {

B b1 = new B();

b1.test1();

b1.test2();

Output:

world

Welcome

Example 5:

package banking_app;

public class GoldAccount {

public void onlineBanking(){

System.out.println("yes");

public void chqBooks(){

System.out.println("2/year");

public void interest(){

System.out.println("No Interest on Balance");

}
public void minBalance(){

System.out.println("Rs. 5000");

package banking_app;

public class PlatinumAccount extends GoldAccount{

public void chqBooks(){

System.out.println("Unlimited/year");

public void interest(){

System.out.println("4%/year");

public void minBalance(){

System.out.println("Rs. 10000");

public static void main(String[] args) {

GoldAccount g = new GoldAccount();

g.onlineBanking();

g.chqBooks();

g.interest();

g.minBalance();

System.out.println("______________");

PlatinumAccount p = new PlatinumAccount();

p.onlineBanking();

p.chqBooks();

p.interest();

p.minBalance();
}

Output:

yes

2/year

No Interest on Balance

Rs. 5000

______________

yes

Unlimited/year

4%/year

Rs. 10000

b. Overloading:

Definition: Here we develop multiple methods with the same name but having
different number of arguments or different type of arguments

public class Email {

//Marketing Mailers

//Transactional Mailers

public void sendEmail(String un, String pwd, String route){

System.out.println("Will send marketing emailers");

public void sendEmail(String un, String pwd, String route,String


tcid){

System.out.println("Will send transaction emailers");

public static void main(String[] args) {

Email e = new Email();

e.sendEmail("xx", "xx", "marketing");


e.sendEmail("xx", "xx", "transactional","12567");

Output:

Will send marketing emailers

Will send transaction emailers

Encapsulation in Java

encapsulation refers to the bundling of data(variables) with the methods


that operate on that data and restricting of direct access of
data(variables).

TO achieve encapsulation we should do the following

1. Make the variables private so that they cannot be accessed directly

2. Create getters and setters to access the data(variables)

Example:

package encapsulation_example;

//We will build methods that will initialize variable i and

//also read the data of variable i

public class A {

private int i ;

private String name;

public int getI() {

return i;

public void setI(int i) {

this.i = i;

public String getName() {


return name;

public void setName(String name) {

this.name = name;

package encapsulation_example;

public class B {

public static void main(String[] args) {

A a1 = new A();

a1.setI(100);

System.out.println(a1.getI());

a1.setName("Pankaj Sir Academy");

System.out.println(a1.getName());

Output:

100

Pankaj Sir Academy

Interfaces in Java

a. In an interface we can create only incomplete methods in it. We can also


call incomplete methods in an interface as abstract methods

b. When a class implements an interface then the class has to complete all
incomplete methods of an interface in the class or else you would get an
error.

Example 1:

package java_app_1;
public interface B {

public void x() ;

Note: The above interface is stored as "B.java"

Example 2:

package java_app_2;

public interface A {

public void test();

package java_app_2;

public class B implements A{

public void test() {

System.out.println("From Test");

public static void main(String[] args) {

B b1 = new B();

b1.test();

Output:

From Test

Example 3:

package java_app_3;

public interface A {

public void test1();

public void test2();


}

package java_app_3;

public class B implements A{

@Override

public void test1() {

System.out.println("From test1");

@Override

public void test2() {

System.out.println("From test2");

public static void main(String[] args) {

B b1 = new B();

b1.test1();

b1.test2();

Output:

From test1

From test2

You might also like