12.30 PM Core Java Notes
12.30 PM Core Java Notes
Class:
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 {
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
1. Non static members belongs to object. Whenever an object is created only non static member
gets loaded into the object.
3. Every time we create an object. non static member will get loaded into it
Example 1:
public class A {
int i = 10;
A a1 = new A();
System.out.println(a1.i);
Output:
10
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;
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;
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;
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;
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;
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
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 {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
12
21
Example 2:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
13
33
Example 3:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
14
46
Example 4:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
12
32
Example 5:
public class A {
int i = 5;
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 {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
12
23
Example 2:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
13
36
Example 3:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
12
34
Examples on post & pre increment together:
Example 1:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
12
22
Example 2:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
14
47
Example 3:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
19
Example 4:
public class A {
int i = 10;
System.out.println(i);
System.out.println(j);
Output:
10
40
Example 5:
public class A {
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 {
int i = 10;
System.out.println(i);
System.out.println(j);
}
Output:
12
-2
Overview of methods:
Example 1:
public class A {
A a1 = new A();
a1.test();
System.out.println(500);
Output:
500
Example 2:
public class A {
A.test();
System.out.println(500);
}
Output:
500
1. local variable
2. static variables
4. reference variables
4. Without initializing local variables if used then it will get you an error
Example 1:
public class A {
A a1 = new A();
a1.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 {
A a1 = new A();
a1.test();
System.out.println(i);
Example 3:
public class A {
A a1 = new A();
a1.test();
System.out.println(i);//Error
int i = 10;
System.out.println(i);
Output:
Error
Example 4:
public class A {
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 {
int i ;
System.out.println(i);
Output:
Error
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 {
System.out.println(A.i);
A a1 = new A();
a1.test();
System.out.println(A.i);
Output:
10
10
Example 2:
public class A {
System.out.println(A.i);
Output:
0
Example 3:
public class A {
System.out.println(A.i);
System.out.println(A.j);
System.out.println(A.k);
Output:
1. These variables are created outside all the methods but inside a class without using static
keyword
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;
A a1 = new A();
System.out.print(a1.i);
}
}
Output:
10
Example 2:
public class A {
int i ;
A a1 = new A();
System.out.print(a1.i);
Output:
Syntax:
a. These variables are created inside a method and should be used only within created method
Example 1:
public class A {
A a1 = new A();//Creating
System.out.println(a1);//Using in main
a1.test();//using in main
Output:
symbol: variable a1
location: class A
1 error
Example 2:
public class A {
System.out.println(a1);
Output:
System.out.println(a1);
1 error
c. static reference variable by default will get initialized to null value if object is not created
Example 1:
public class A {
System.out.println(a1);
a1.test();
System.out.println(a1);
Output:
A@7960847b
A@7960847b
Example 2:
public class A {
System.out.println(a1);
}
Output:
null
Which data type I should use to store 10 digit mobile phone number?
Example 1:
public class A {
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
Example 1:
public class A {
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 {
Output:
1 error
Example 3:
public class A {
var i = 10;
Output:
var i = 10;
^
1 error
Example:
public class A {
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
Example 1:
public class A {
int i = 10;
long j = i;
System.out.println(j);
}
}
Output:
10
Example 2:
public class A {
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 {
double j = i;
System.out.println(j);
Output:
10.300000190734863
Example 4:
public class A {
public static void main(String args[]) {
long j = i;
System.out.println(j);
Output:
a. Converting bigger data type to smaller data type is called as explicit down casting
Example 1:
public class A {
int i = 10;
byte j = (byte)i;
System.out.println(j);
Example 2:
public class A {
long i = 10;
int j = (int)i;
System.out.println(j);
}
}
Output:
10
Example 3:
public class A {
double i = 10.3;
float j = (float)i;
System.out.println(j);
Output:
10.3
Example 4:
public class A {
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 {
float i = 10.3f;
long j = (long)i;
System.out.println(j);
Output:
10
Example 6:
public class A {
long i = 9632882052L;
int j = (int)i;
System.out.println(j);
Output:
1042947460
Example 1:
public class A {
int i = 'b';
System.out.println(i);
Output: 98
Example 2:
public class A {
int i = 'ए';
System.out.println(i);
Output:
2319
Example 3:
public class A {
int i = 'அ';
System.out.println(i);
Output:
2949
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
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
Step 2: In the wizards type java, then select java project and click on next and give project name
example: app1
Step 1: In src folder of your project right click and select class
Shortcuts in eclipse:
Methods in Java
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 {
A a1 = new A();
a1.test();//Rule 2
System.out.println(100);//NO Rule
}//rule 3
Output:
100
Example 2:
package methods_example_1;
public class A {
System.out.println(5);//No Rule
System.out.println(10);//No Rule
}//Rule 4
System.out.println(100);//No Rule
Output:
100
10
Example 3:
package methods_example_1;
public class A {
a1.test1();//Rule 2
a1.test2();//Rule 2
}//Rule 4 STOPS
System.out.println(5);//No Rule
}//Rule 3
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
void: when a method is void then it means that the method cannot return any
value
package methods_example_1;
public class A {
}
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 {
Output:
500
Note: Control + 1
Example 9:
package methods_example_1;
public class A {
}
Output:
Example 10:
package methods_example_1;
public class A {
A a1 = new A();
double i = a1.test();
System.out.println(i);
return 10.3;
Output:
10.3
Example 11:
package methods_example_1;
public class A {
A a1 = new A();
String i = a1.test();
System.out.println(i);
}
Output:
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 {
A a1 = new A();
a1.test();
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 {
A a1 = new A();
a1.test();
return;
System.out.println(100);//Never Execute
Output:
Error
Example 3:
package methods_examples_1;
public class A {
A a1 = new A();
a1.test();
System.out.println(100);//Never Execute
return;
Output:
100
Example 4:
package methods_examples_1;
public class A {
A a1 = new A();
int i = a1.test();
System.out.println(i);
return 500;
System.out.println(100);//Never Execute
Output:
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);
System.out.println(100);//Never Execute
return 500;
Output:
100
500
Example 6:
package methods_examples_1;
public class A {
A a1 = new A();
a1.test(100, 200);
System.out.println(i);
System.out.println(j);
}
}
Output
100
200
Example 7:
package methods_examples_1;
public class A {
A a1 = new A();
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 {
A a1 = new A();
a1.test(100,200,300,400,500);
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 {
return;
}
Output:
Compile
Constructors In Java
c. We can create more than one constructor in the same class provided they
have different number of arguments or different type of arguments
Example 1:
package methods_examples_1;
public class A {
A(){
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);
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(){
A a1 = new A();
Output:
Example 4:
package methods_examples_1;
public class A {
void A(){
A a1 = new A();
a1.A();
Output:
From Constructor A
Example 5:
package methods_examples_1;
public class A {
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 {
System.out.println(i);
A a1 = new A();
A a2 = new A(100);
Output:
100
Example 7:
package methods_examples_1;
public class A {
System.out.println(i);
System.out.println(j);
A a1 = new A();
A a2 = new A(100);
A a3 = new A(500,300);
Output:
100
500
300
Example 8:
package methods_examples_1;
public class A {
System.out.println(i);
System.out.println(j);
A a1 = new A(100);
A a2 = new A('a');
Output:
100
Example 9:
package methods_examples_1;
public class A {
System.out.println(i);
System.out.println(j);
A(String i){
System.out.println(i);
A a1 = new A(100);
A a2 = new A('a');
Output:
100
Example 10:
package constructor_example;
public class A {
A(){
return 100;
A a1 = new A();
Output:
Error
Example 11:
package constructor_example;
public class A {
A() {
System.out.println(100);
return;
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:
this keyword
b. using this keyword we can access non static members of the class
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 {
A a1 = new A();
System.out.println(a1);
a1.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;
A a1 = new A();
System.out.println(a1.i);
a1.test();
System.out.println(this.i);
Output:
10
10
Example 3:
package constructor_example;
public class A {
A a1 = new A();
a1.test1();
this.test2();
Output:
From test 1
From test 2
Example 4:
package constructor_example;
public class A {
A a1 = new A();
System.out.println(this);
Output:
Example 5:
package constructor_example;
public class A {
public static void main(String[] args) {
A a1 = new A();
a1.test();
System.out.println(this);
Output:
Example 6:
package constructor_example;
public class A {
A a1 = new A();
System.out.println(a1);
a1.test();
A a2 = new A();
System.out.println(a2);
a2.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 {
int j = 100;
A a1 = new A();
a1.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;
A a1 = new A();
a1.test();
System.out.println(i);
Output
100
Example 9:
package constructor_example;
public class A {
int i = 100;
A a1 = new A();
a1.test();
System.out.println(i);
Output:
Error
Example 10:
package constructor_example;
public class A {
int i = 100;
A a1 = new A();
a1.test();
System.out.println(i);
System.out.println("From x");
Output:
100
From x
Example 11:
package this_examples;
public class A {
A(){
A(int i){
this();
A a1 = new A(100);
Output:
From Constructor A
Example 12:
package this_examples;
public class A {
A(){
A(int i){
System.out.println(i);
this();
A a1 = new A(100);
}
}
Output:
Example 12:
package this_examples;
public class A {
A(){
A(int i){
this();
System.out.println(i);
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);
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 {
Output:
Example 1:
package this_examples;
public class A {
int i = 10;
System.out.println(this.i);
Output:
Example 2:
package this_examples;
public class A {
int i;//0
A a1 = new A();
a1.test();
int i = 10;
System.out.println(this.i);
}
Output:
Example 3:
package this_examples;
public class A {
int i;//0
A a1 = new A();
a1.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();
int i = 10;
this.i = i;
System.out.println(i);
System.out.println(this.i);
Output:
10
10
b. When there is more than one IIB then these IIB's will run in sequence
one after another when an object is created
Example 1:
public class A {
System.out.println(100);
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);
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);
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);
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
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);
System.out.println(50);
Output:
100
Example 2:
package iib_examples;
public class A {
static {
System.out.println(100);
Output:
Example 3:
package iib_examples;
public class A {
static {
System.out.println(100);
static{
System.out.println(500);
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);
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);
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);
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);
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);
Output:
10
Purpose of IIB?
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;
A a1 = new A();
System.out.println(a1.i);
System.out.println(A.j);
}
}
10
100
All static variables are initialized in SIB for better clarity of the code
Example 1:
package sib_iib_constructors_example;
public class A {
static{
i = 10;
j = 20;
k = 30;
System.out.println(A.i);
System.out.println(A.j);
System.out.println(A.k);
Output:
10
20
30
Note:
Example 2:
package sib_iib_constructors_example;
public class A {
int i,j,k;
static{
i = 10;
j = 20;
k = 30;
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;
B b1 = new B();
System.out.println(b1.i);
Output:
10
Example 2:
public class A {
int i = 10;
System.out.println(1000);
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;
System.out.println(1000);
package inheritance_examples;
A a1 = new A();
System.out.println(a1.i);
a1.test();
Output:
10
1000
Example 4:
package inheritance_examples;
public class A {
System.out.println(1000);
package inheritance_examples;
System.out.println(500);
}
}
package inheritance_examples;
System.out.println(10);
C c1 = new C();
c1.test1();
c1.test2();
c1.test3();
Output:
1000
500
10
Example 5:
package inheritance_examples;
public class A {
package inheritance_examples;
public class B {
package inheritance_examples;
Output:
Error
Note:
Example 6:
package inheritance_examples;
public class A {
static int i;
package inheritance_examples;
System.out.println(B.i);//A.i
Output:
0
Example 7:
package inheritance_examples;
public class A {
static int i;
System.out.println("From Test");
package inheritance_examples;
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;
B b1 = new B();
System.out.println(b1.i);//A.i
b1.test();//A.test();
Output:
From Test
c. Using super keyword we can call constructor of parent class, but the
call has to be made from child class constructor
Example 1:
package inheritance_examples;
public class A {
int i;
}
package inheritance_examples;
B b1 = new B();
b1.test();
System.out.println(super.i);
Output:
Example 2:
package inheritance_examples;
public class A {
int i;
System.out.println("x");
package inheritance_examples;
B b1 = new B();
b1.test();
System.out.println(super.i);
super.x();
Output:
Example 3:
package inheritance_examples;
public class A {
int i;
System.out.println("x");
package inheritance_examples;
B b1 = new B();
b1.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;
System.out.println("x");
package inheritance_examples;
B b1 = new B();
b1.test();
System.out.println(super.i);
super.x();
}
Output:
Example 5:
package inheritance_examples;
public class A {
A(){
package inheritance_examples;
B(){
super();
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;
B(){
super(100);
B b1 = new B();
Output:
100
Example 7:
package inheritance_examples;
public class A {
A(int i){
System.out.println(i);
package inheritance_examples;
B(){
System.out.println(500);
super(100);
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;
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
Example 1:
package p1;
public class A {
In the above package keyword defines that class A is created in folder p1.
Note:
Example 1:
package p1;
public class A {
package p2;
import p1.A;
Example 2:
package p1;
public class A {
package p1;
public class C extends A{
Example 3:
package p1;
public class A {
package p2;
Example 4:
package p3.p4.p5;
public class D {
Example 5:
package p2;
import p3.p4.p5.D;
D d1 = new D();
}
}
package p3.p4.p5;
public class D {
Example 6:
package p1;
public class A {
package p1;
package p2;
import p1.A;
import p1.C;
public class B {
A a1 = new A();
C c1 = new C();
Example 7:
package p1;
public class A {
}
package p1;
package p2;
import p1.*;
public class B {
A a1 = new A();
C c1 = new C();
1. private
2. default
3. protected
4. public
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 {
System.out.println("From test");
A a1 = new A();
System.out.println(a1.i);
a1.test();
Output:
10
From test
Example 2:
package p1;
public class A {
package p1;
B b1 = new B();
System.out.println(b1.i);
b1.test();
Output:
error
Example 3:
package p1;
public class A {
System.out.println("From test");
package p1;
public class B {
System.out.println(a1.i);
a1.test();
Output:
error
Example 4:
package p1;
public class A {
System.out.println("From test");
package p2;
import p1.A;
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 {
System.out.println("From test");
package p2;
import p1.A;
public class C {
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
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");
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{
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 {
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 {
A a1 = new A();
System.out.println(a1.i);
a1.test();
Example 1:
package p1;
public class A {
A a1 = new A();
System.out.println(a1.i);
a1.test();
Output:
10
From Test
Example 2:
package p1;
public class A {
System.out.println("From Test");
package p1;
B b1 = new B();
System.out.println(b1.i);
b1.test();
Example 3:
package p1;
public class A {
System.out.println("From Test");
package p1;
public class B {
A a1 = new A();
System.out.println(a1.i);
a1.test();
Example 4:
package p1;
public class A {
protected int i = 10;
System.out.println("From Test");
package p2;
import p1.A;
C c1 = new C();
System.out.println(c1.i);
c1.test();
Output:
10
From Test
Example 5:
package p1;
public class A {
package p2;
import p1.A;
public class C{
A a1 = new A();
System.out.println(a1.i);
a1.test();
Output:
Error
Example 1:
package p1;
class A {
}
package p2;
import p1.A;
Output: Error
Note:
Polymorphism In Java:
When we develop a feature in java that can take more than one form is
called as polymorphism.
a. Overriding:
Example 1:
package polymorphism_examples;
public class A {
package polymorphism_examples;
System.out.println("world");
B b1 = new B();
b1.test();
Output:
world
Example 2:
package polymorphism_examples;
public class A {
System.out.println("Hello");
package polymorphism_examples;
public class B extends A{
System.out.println("world");
B b1 = new B();
b1.test();
A a1 = new A();
a1.test();
Output:
world
Hello
Note:
Example 3:
package polymorphism_examples;
public class A {
System.out.println("Hello");
}
package polymorphism_examples;
@Override
System.out.println("world");
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 {
System.out.println("Hello");
System.out.println("Welcome");
package polymorphism_examples;
System.out.println("world");
B b1 = new B();
b1.test1();
b1.test2();
Output:
world
Welcome
Example 5:
package banking_app;
System.out.println("yes");
System.out.println("2/year");
}
public void minBalance(){
System.out.println("Rs. 5000");
package banking_app;
System.out.println("Unlimited/year");
System.out.println("4%/year");
System.out.println("Rs. 10000");
g.onlineBanking();
g.chqBooks();
g.interest();
g.minBalance();
System.out.println("______________");
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
//Marketing Mailers
//Transactional Mailers
Output:
Encapsulation in Java
Example:
package encapsulation_example;
public class A {
private int i ;
return i;
this.i = i;
this.name = name;
package encapsulation_example;
public class B {
A a1 = new A();
a1.setI(100);
System.out.println(a1.getI());
System.out.println(a1.getName());
Output:
100
Interfaces in Java
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 {
Example 2:
package java_app_2;
public interface A {
package java_app_2;
System.out.println("From Test");
B b1 = new B();
b1.test();
Output:
From Test
Example 3:
package java_app_3;
public interface A {
package java_app_3;
@Override
System.out.println("From test1");
@Override
System.out.println("From test2");
B b1 = new B();
b1.test1();
b1.test2();
Output:
From test1
From test2