Club - Java Day 1to3
Club - Java Day 1to3
Day1:
Introduction to java:
-Java technology is both a Programming Language and a Platform(OS+Hardware) from Oracle
Corporation.
-Launched on 18th march 2014.
features:
1.Simple-No Pointers, No Multiple Inheritance
For almost every task API (Application Programming Interface) is available; Programmer just need to
know how to use that API.
8. Robust- most of the things are checked during compile time so no risk at runtime
Java puts a lot of emphasis on early checking for possible errors, as Java compilers are able to detect
many problems that would first show up during execution time in other languages.
It provides the powerful exception handling and type checking mechanism as compare to other
programming languages.
8.1.Safe-
If a bytecode contains any virus or malicious code, JVM will not execute it.
This features saves your system especially when you download java code and try to execute.
9. Dynamic-Supports late binding since beginning. You can load any class on the fly and find out its
information.
10. Secure-If bytecode contains virus or malicious code , it cannot be executed on the system. Thanks
to Java’s secure feature.
11. Automatic Garbage Collection :
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 object, or unreferenced object, is no longer referenced by any part of your program.
So the memory used by an unreferenced object can be reclaimed.
-In the Java programming language, all source code is first written in plain text files ending with the
.java extension.
-Those source files are then compiled into .class files by the javac(Java compiler) compiler.
-Java SE8(java standard edition 8)
E-commerce Application
Network or Computing Devices
PDA
Cell Phones [Mobile Information Devices Profile (MIDP)]
PCs
Mobile Tracking Systems
Pulse Meter
Variable :
Variable is name of reserved area allocated in memory.
int data=50;//
Here data is variable
Static Variable-A variable that is declared as static is called static variable. It cannot be local.
class A{
void method(){
}//end
In Java, There are of class
Two Types of Data Types
Primitive Data Types
Non-Primitive Data Types
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
public class Datatypes_Demo
float 0.0f 4 byte
int c=30;
It is a unification of dozens of character sets such as Latin, Greek, Arabic, Russia and many more.
For this purpose, it requires 16 bits.
Thus char in java is 16 bit type.
The range of char is 0 to 65535.
There are no negative chars.
Types of comments:
in java we have 3 types of comments
a) single line //
b) multiline
/* */
c) document comment ( in order to create documentation )
/** **/
control flow:
if/else if/else: used to apply condition on the program
public class If_Demo
{
public static void main(String[] args)
{
int a=10;
if(a>0)
{
System.out.println("it's positive");
}
else if(a<0)
{
System.out.println("it's negative");
}
else
{
System.out.println("it's zero");
}
}
}
For
1:
public class For_Demo_1
{
public static void main(String[] args)
{
int i;
for(i=0;i<5;i++)
{
System.out.println(i);
}
}
}
2:
public class For_Demo_2
{
public static void main(String[] args)
{
int i;
for(i=0;i<5;i++)
{
if(i==3)
{
break; //if I is 3 them break. op 0,1,2,3
}
System.out.println(i);
}
}
}
3:
public class For_Demo_3
{
public static void main(String[] args)
{
int i;
for(i=0;i<5;i++)
{
if(i==3)
{
continue; //is I=3 then continue op 0,1,2,4
}
System.out.println(i);
}
}
}
Nested for loop:
package datatypes_pro;
package firstpro;
import java.util.Scanner;
public class Demo1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
System.out.println("Number accepted is\t"+num);
System.out.println("Enter amount");
double amount=sc.nextDouble();
System.out.println("Amount accepted is\t"+amount);
Properties file:In that file we only send the English world in program
client provide there translation words in front of them and run code by
translate.
Demo 1:
class MyNumber
{
private int num; // instance member or member variable
Demo2:
class MyNumber
{
private int num; // instance member or member variable
Demo 3:
class MyNumber
{
private int num; // instance member or member variable
what is "this"?
Demo 4
class MyNumber
{
private int num; // instance member or member variable
Demo 5:
class MyNumber
{
private int num; // instance member or member variable
Demo 6:
class MyNumber
{
private int num; // instance member or member variable
Demo 7:
class MyNumber
{
private int num; // instance member or member variable
Demo 8:
class MyNumber
{
private int num; // instance member or member variable
}
}
Demo 9:
class MyNumber
{
private int num; // instance member or member variable
}
}
Demo 10:
class MyNumber
{
private int num; // instance member or member variable
}
}
Demo 12:
// call by value
Demo 13,14:
// call by value
From disp one more object created on heap but scope is completed and
reference get destroyed
garbage collector call and mark object for garbage collection
Demo 16:
// call by value
1. Procedural language is based on functions but object oriented language is based on real world
objects.
2. Procedural language gives importance on the sequence of function execution but object
oriented language gives importance on states and behaviours of the objects.
3. Procedural language exposes the data to the entire program but object oriented language
encapsulates the data.
4. Procedural language is complex in nature so it is difficult to modify, extend and maintain but
object oriented language is less complex in nature so it is easier to modify, extend and
maintain.
5. Procedural language provides less scope of code reuse but object oriented language provides
more scope of code reuse.
Gives importance to functions rather than data. Whenever any task is given developers try to
divide it into number of functions.
Data can be global or local. Global data can be accessed or even altered by all the functions,
hence there is no data security.
When the volume of application becomes large, it is very difficult to maintain the code.
Doesn’t model real world problems very well. This is because functions are action-oriented
and do not really correspond to the elements of the problem (e.g.: Customer, Account, Vehicle
etc.)
Object-oriented programming (OOP) refers to software design in which programmers define not only
the data for a data structure, but also the types of operations (functions) that can be applied to the data.
e.g
Account
acc_id
name
balance
setName(), getName()
Class
A Class is a plan which describes the object. We call it as a blue print of how the object should be represented.
Mainly a class would consist of a name, attributes & operations. Considering the above example,
A Mobile can be a class which has some attributes like Profile Type, IMEI Number, Processor, and some more.)
Objects:
Any real world entity which can have some characteristics or which can perform some work is called as Object.
This object is also called as an instance. If we consider the above
example, a mobile manufacturing company, at a time manufactures lacs of pieces of each model which are the
instances. These objects are differentiated from each other via some identity or its characteristics.
AbstractionAbstraction means Hiding an implementation and showing only essential features.It means
providing information about what object does instead of how it does.
Also called : Model Building
Example: Car Brakes(pressing the pedals will stop the vehicle but you do not need to know how it
works.)
Phone-when we make a call or receive a call we just know how to do that, we don't know the internal
working of the phone.
Data encapsulation -> Binding the data and functions together in one unit is called as "Encapsulation".
In java "class" is the example of Encapsulation.
-Things should be encapsulated in such a manner to achieve abstraction
Function Overloading:
Function overloading means defining more than one function with the same name but with different arguments.
e.g.
Eg.1)Int data;
Eg.2)int A;
A=20;
System.out.print(A) //here we initialized A before use so we get output correct and no error
JDK(Java development kit):allows you to write the program and run the program
Why do you need JRE in between java application and operating system.
Each operating system has got something called native code ,now what is job of native code it is used to execute
any program
.class file format is different from respective of native code JRE knows how to convert .class format into
windows native code,JRE on windows will convrt .class file format in windows native code.
OS is only platform
Before we dig into class declarations, let's do a quick review of the rules associated with declaring
classes, import statements, and package statements in a source file:
■ There can be only one public class per source code file.
Public class demo{
p.s.v.m(){
}
}
Public class A{ //it is not allowd
}
■ Comments can appear at the beginning or end of any line in the source code file; they are
independent of any of the positioning rules discussed here.
■ If there is a public class in a file, the name of the file must match the name of the public class. For
example, a class declared as public class Dog { } must be in a source code file named Dog.java.
■ If the class is part of a package, the package statement must be the first line in the source code file,
before any import statements that may be present.
■ If there are import statements, they must go between the package statement (if there is one) and the
class declaration. If there isn't a package statement, then the import statement(s) must be the first
line(s) in the source code file.
If there are no package or import statements, the class declaration must be the first line in the source
code file.
■ import and package statements apply to all classes within a source code file.
In other words, there's no way to declare multiple classes in a file and have
them in different packages, or use different imports.
3)
Class A{
Main{
}//in A class there is main so A class only executed
}
Class B{
}
Flow:
A.java //save
Javac A.java //compile
A.class //weget
Java A //run
Invitation of jvm_ it will check main()_ &main is not their_ it will throw the error
If main get_ call to that class_ If any class declared In class A_ then that class called.
4)
Class A
{
B class execute when function call
}
Class B
{
}
constructor
It is special because
b) parameterized constructor
constructor with at least one argument.
e.g.
MyClass(int k)
{
}
this has to be explicitly provided by the programmer.
e.g.
public class MyClass11
{
int num;
MyClass11(int num)
{
this.num=num;
}
MyClass11()
{
}
p.s.vm
{
MyClass11 m2=new MyClass11(200); // you need parameterized
constructor
MyClass11 m1=new MyClass11(); // you need default or no-arg
constructor
}
}
e.g1:
class Sample
{
// Compiler provides "default constructor" here
e.g2:
class Sample
{
Sample()
{
System.out.println("inside default constructor");
}
private int num;
Sample()
{
System.out.println("inside default constructor");
}
Sample(int num)
{
this.num=num;
System.out.println("inside parameterized constructor");
}
Finalize function:
java does not have destructor.
just before object gets garbage collected, following method gets
called. Programmer can override(define) this finalize method in order
to release resources such as file, database connection or sockets.
Garbage collection can not be forced in java. U can just make a request
for Garbage Collection , by invoking a method "System.gc()". or
"Runtime.getRuntime().gc()".
static member
static members are allocated memory as soon as class gets loaded in the
memory. They are not associated with the instance.
Since they are not associated with instance, they have only one copy in
the memory, irrespective of no.of instances created.
They can be accessed by class name. They are also called as "class
variables".
e.g.
Account object
non-static members - accid, name, balance
static member - rateofinterest.
Can static member function access non-static data ?
No. because static member function can be called without creating object. When object is not created,
non-static member is not allocated memory.
syntax
static
{
}
}
e.g.
class MyClass
{
public static void main(String args[])
{
System.out.println("Welcome to java");
}
}
java MyClass
a) MyClass gets loaded.
b) JVM invokes "main" function.
What are Class Loaders ?
1.footstrap loaders or parameterized class loaders //bootstrap prog load from that
2.extension class loaders //intermediate programs load
3.system class loaders //hi hello simple program load from that
//there is multiple classes in java to work load distribute class loader created
System.out.println(“inside fun”);
System.out.println(“hello world”);
A.fun(); // Then in the main function we have created an instance of class “A” , so class “A” will
be loaded here. This also means that we will have one more instance of class “Class” representing class “A”.
2)
public class MyClass10
{
static
{
System.out.println("in MyClass10 first static
initializer");//1
}
public static void main(String args[])
{
System.out.println("in main"); //3
}
static
{
System.out.println("in MyClass10 second static
initializer");//2
}
3)
public class MyClass11
{
static
{
System.out.println("in MyClass11 first static initializer");
}
public static void main(String args[])
{
System.out.println("in main");
}
static
{
System.out.println("in MyClass11 second static
initializer");
}
}
class A
{
static
{
System.out.println("in A first static initializer"); //a not
called
}
}
4)
public class MyClass12
{
static
{
System.out.println("in MyClass12 first static
initializer");//1
}
public static void main(String args[])
{
System.out.println("in main");//3
new A();
}
static
{
System.out.println("in MyClass12 second static
initializer");//2
}
}
class A
{
static
{
System.out.println("in A first static initializer");//4 }
}
}
But there is error in that program is that if we need to add more
function we need to add constructor also and also if we miss to provide
cnt++ show wrong op
So we will use non static block
Non Static block:
non static block used before constructor and it will execute before each & evry constructor is called it
is use for repetitive operation.
// non-static initializer
{
cnt++; //using this counter count each time when
object created
}
public MyClass14()
{
}
public MyClass14(int num)
{
this.num=num;
}
public void setNum(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}
public static int getCnt()
{
return cnt;
}
public static void main(String args[])
{
MyClass14 m1=new MyClass14();
MyClass14 m2=new MyClass14(200);
m1.setNum(100);
System.out.println(m1.getNum());
System.out.println(m2.getNum());
System.out.println(MyClass14.getCnt());
MyClass14 m3=new MyClass14(300);
System.out.println(m3.getNum());
System.out.println(MyClass14.getCnt());
}
}
final keyword:
when "final" keyword is applied to any member, that means it is constant, once it is initialized.
final member must be initialized.
S.O.P:
class System
{
public static PrintStream out;
public static InputStream in;
}
class PrintStream
{
public void print(){}
public void println(){} //printstram have print() & println()
}
System.out.println();
out.ptintln() we create reference of printStream class and call println function by out.println()
System.out -> out is static member so to call out class_name.member.System is the class.
a) “hello” need to be displayed
b) U need to invoke “print()” or “println()” method println(“hello”)
c) Both the methods are not static in “printstream” class, so you need to reference of
”Printstream” class.
d) “out” is reference of “prinstream” & member of system & type of printstream
out.printstream(“Hello”)
e) Out is a member of system & that member is static in order to access the member no need to
create object of class whenever we want to access the out we did not create object of system
directly call with class name.
What is Singleton:
// developer part
class Singleton
{
private static Singleton obj=new Singleton(); //always object
created on heap
void disp1()
{
System.out.println("in disp1");
}
void disp2()
{
System.out.println("in disp2");
}
// client part
public class SingletonDemo
{
public static void main(String args[])
{
// Singleton s1=new Singleton(); // not allowed=not possible
bcoz constructor is private
// Singleton ref=Singleton.obj; // not possible singleton
cannot resolved because it is private
Singleton ref=Singleton.getSingleton();
ref.disp1();
ref.disp2();
Singleton.getSingleton().disp1();
}
}
if you want to invoke 6-7 functions from main, is it advisable to make them static so that we can
invoke with the syntax "classname.function" ?
non-static method
works on the state of the object. -means for every call we need to create object
static method
is used to access private static variable.
it's a utility method. That means whatever data is provided during invocation static method
works on it and performs the action.
-don’t use static in every time use for important purpose
lazy_eager_resolution
class Sample
{
int num1=100; // num1 will be allocated memory only after we create object/s
static int num2=200; // num2 will be allocated memory as soon as Sample class gets loaded.
}
class A { }
class B { }
class Sample
{
private A ob1=new A(); // class A will be loaded only after first object of Sample gets created.
- lazy resolution
private static B ob2=new B(); // class B will be loaded immediately after class Sample gets
loaded - eager resolution
}
demo
package firstpro;
class A
{
static
{
System.out.println("inside A static block");
}
}
class B
{
static
{
System.out.println("inside B static block");
}
}
public class Demo1
{
private A ref1=new A(); // lazy resolution
private static B ref2=new B(); // eager resolution_that first
because static
output:
demo2:
package firstpro;
class A
{
static
{
System.out.println("inside A static block");
}
}
class B
{
static
{
System.out.println("inside B static block");
}
}
public class Demo2
{
private A ref1=new A(); // lazy resolution
private static B ref2=new B(); // eager resolution
public static void main(String[] args)
{
new Demo2(); // class A will be loaded now
System.out.println("in main");
new Demo2(); // class A will not be loaded again as
// classes are loaded only once in Java
}
}
O/p
Ref2
Ref1
In main
Array:
Final array:
public class FinalArrayDemo
{
3)
public class ArrayDemo3
{
public static void main(String args[])
{
int arr[]={10,20,30};
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
4) package core1;
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
6)public class ArrayDemo5
{
public static void main(String args[])
{
int arr[][]={{10,20,30},{40,50,60},{70,80,90}};
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[i].length;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
package
Array of instances/object:
public class ArrayOfInstance
{
public static void main(String args[])
{
MyClass arr[]=new MyClass[3]; // array of references to
MyClass
for(int i=0,j=10;i<arr.length;i++,j+=10)
{
arr[i]=new MyClass(j); // object creation//array value
is updated in each loop i=0 j=10 ,i=1 j=20,i=2 j=30
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i].getNum());
}
}
}
class MyClass
{
int num;
MyClass(int num)
{
this.num=num;
}
int getNum()
{
return num;
}
}
package info
package is a collection of related classes and interfaces.
java.awt
java.io
java.util
java.lang
and so on
syntax
options are
c - create
f - file
u - update. i.e. to add more file/s to an existing jar file
x - extract the file/s
t - test the jar file
m - manifest. i.e. serving some imp information.
Client - we all
who use those classes developed by developer.
Reusability
means using existing type while defining a new type. It can be achieved in two ways:
a) composition/aggregation [has-a relationship]
b) inheritance [is-a relationship]
you go for composition/aggregation when you want to use some of the functionalities of existing type
inside new type.
e.g.
while designing "Car" you would reuse "Engine" by composition/aggregation, because "Car"
is not an "Engine" it just needs some functionalities of Engine.
you go for inheritance when you realize that new type is "same as" existing type.
e.g.
while designing "Car" you would reuse "FourWheeler" because Car is same as FourWheeler.
Inheritance:
The ability for a new class to be created from an existing class by extending it is known as
inheritance. It provides reusability. Basically you go for inheritance when u realize that "new type is
same as existing type".
e.g.
class Person
{
private String name,address;
private int age;
// setters and getters
}
now we can derive "Student" class from Person.
class Student extends Person
{
private String qualification;
private int rollno,ccppid;
2)Default
3)Protected
4)Public
Note:
Top level classes are the classes which are not nested classes(means class within classes)
In java rule is top level class can be either “Public” or “Default” it cannot be “Private” or “Protected”
But nested classes have all access modifier
In java by default parent is Java.lang.object
Demo1:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1); //First print disp1
}
}
class sub extends base
{
int num2=20;
void disp2()
{
System.out.println(num2); //second print disp2
}
}
public class Demo1
{
public static void main(String args[])
{
sub s1=new sub();
s1.disp1();
s1.disp2();
}
}
Demo2:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base()
{
System.out.println("in base no-arg"); //from base invoke nd
execute and print and then sub execute
}
}
class sub extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub()
{
System.out.println("in sub no-arg"); // from main we invoke
sub constructor then implicitly invoke to base constructor
}
}
public class isa
{
public static void main(String args[])
{
sub s1=new sub();
s1.disp1();
s1.disp2();
}
}
//
in base no-arg
in sub no-arg
10
20
Demo3:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub()
{
System.out.println("in sub no-arg"); //no def constructor in
base /and we cannot invoke base parameterized constructor from sub
}
}
public class isa
{
public static void main(String args[])
{
sub s1=new sub(); // Error
s1.disp1();
s1.disp2();
}
}
//Show error
Demo 4:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub()
{
super(10); //super is use to explicitly invoke base para
constructor
System.out.println("in sub no-arg");
// super(10); Error, super has to be on first line
}
}
public class Demo4
{
public static void main(String args[])
{
sub s1=new sub();
s1.disp1();
s1.disp2();
}
}
Demo 5:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub()
{
super(10);
System.out.println("in sub no-arg");
}
sub(int num2)
{
this.num2=num2;
System.out.println("in sub param"); //show error bcoz in
only one constructor have explicitly invoking
}
}
public class Demo5
{
public static void main(String args[])
{
sub s1=new sub();
s1.disp1();
s1.disp2();
}
}
/
Demo 6:
/ when base class does not have "no-arg constructor", all the
constructors of sub class must explicitly invoke "parameterized
constructor" of base class.
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub()
{
super(10);
System.out.println("in sub no-arg");
}
sub(int num2)
{
super(20);
this.num2=num2;
System.out.println("in sub param");
}
}
public class Demo6
{
public static void main(String args[])
{
sub s1=new sub();
s1.disp1();
s1.disp2();
}
}
Demo 7:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub1 extends base //error bcoz we do not def constructor in
sub1/we do not pass value to parameterized
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
}
class sub2 extends sub1
{
int num3=30;
void disp3()
{
System.out.println(num3);
}
sub2(int num3)
{
this.num3=num3;
}
}
public class isa
{
public static void main(String args[])
{
sub2 s1=new sub2(10);
s1.disp1();
s1.disp2();
s1.disp3();
}
}
Demo 8:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub1 extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub1()
{
super(5);
} //in multilevel now program run correct
}
class sub2 extends sub1
{
int num3=30;
void disp3()
{
System.out.println(num3);
}
sub2(int num3)
{
this.num3=num3;
}
}
public class Demo8
{
public static void main(String args[])
{
sub2 s1=new sub2(10);
s1.disp1();
s1.disp2();
s1.disp3();
}
}
Demo 9:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub1 extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub1()
{
super(5);
}
sub1(int num2) //we not explicit call to para constructor
{
this.num2=num2;
}
}
class sub2 extends sub1
{
int num3=30;
void disp3()
{
System.out.println(num3);
}
sub2(int num3)
{
this.num3=num3;
}
}
public class Demo9
{
public static void main(String args[])
{
sub2 s1=new sub2(10);
s1.disp1();
s1.disp2();
s1.disp3();
}
}
Demo 10:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub1 extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub1()
{
super(5);
}
sub1(int num2)
{
super(8); //explicit call to para
this.num2=num2;
}
}
class sub2 extends sub1
{
int num3=30;
void disp3()
{
System.out.println(num3);
}
sub2(int num3)
{
super(45);
this.num3=num3;
}
}
public class Demo11
{
public static void main(String args[])
{
sub2 s1=new sub2(10);
s1.disp1();
s1.disp2();
s1.disp3();
}
}
Demo 11:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub1 extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub1()
{
super(5);
}
sub1(int num2)
{
super(8);
this.num2=num2;
}
}
class sub2 extends sub1
{
int num3=30;
void disp3()
{
System.out.println(num3);
}
sub2(int num3)
{
super(45); //para call
this.num3=num3;
}
}
public class Demo11
{
public static void main(String args[])
{
sub2 s1=new sub2(10);
s1.disp1();
s1.disp2();
s1.disp3();
}
}
Demo 12:
class base
{
int num1=10;
void disp1()
{
System.out.println(num1);
}
base(int num1)
{
this.num1=num1;
System.out.println("in base param");
}
}
class sub1 extends base
{
int num2=20;
void disp2()
{
System.out.println(num2);
}
sub1()
{
super(5);
}
sub1(int num2)
{
super(8);
this.num2=num2;
}
}
class sub2 extends sub1
{
int num3=30;
void disp3()
{
System.out.println(num3);
}
sub2(int num3)
{
super(45);
this.num3=num3;
}
sub2(int a,int b) //no any error from this
{
num3=a;
}
}
public class Demo12
{
public static void main(String args[])
{
sub2 s1=new sub2(10);
s1.disp1();
s1.disp2();
s1.disp3();
}
}
Overriding
It is a feature to provide specific behaviour to parent class.
class fourwheeler{
fourwheeler disp1()
{
//overriden
}
}
class car extends fourwheeler{
car disp1() //overriding (covariant return type)
{
}
}
{
void disp()---------------------------> <default> protected public
{ protected public
sop() public
}
}
Final method:
If method is final we cannot override
Demo 13:
class base
{
void disp() // overridden
{
System.out.println("base disp"); //hide that disp
}
}
class sub1 extends base
{
void disp() // overriding
{
System.out.println("sub disp");
}
}
public class isa
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Demo 14:
class base
{
void disp() // overriden
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp() // overriding
{
System.out.println("sub disp");
super.disp(); //now disp of base call
}
}
public class Demo14
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Demo 15:
class base
{
final void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp() // error,can not override final method
{
System.out.println("sub disp");
}
}
public class Demo15
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Demo 16:
class base
{
final void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
/* void disp() // error,can not override final method if we hide
that automatically invoke base
{
System.out.println("sub disp");
}*/
}
public class Demo16
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Final class:
Final class cannot be inherited
If whatever class is more than sufficient for any scenario then the
developer make final class for e.g String class
Demo 17
final class base
{//when class make final then class make final
}
class sub1 extends base
{
}
public class isa
{
public static void main(String args[])
{
base s1=new base();
}
}
Demo 18 no any changes
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp()
{
System.out.println("sub disp");
}
}
public class isa
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Demo 19:
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp(int k) // overloading
{
System.out.println("sub disp");
}
}
public class Demo19
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Demo 20:
class base
{
A disp()
{
System.out.println("base disp");
return null;
}
}
class sub1 extends base
{
A disp()
{
System.out.println("sub disp");
return null;
}
}
public class Demo20
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Demo 21:
// example of co-variant return type
class A{}
class B extends A{}
class base
{
A disp()
{
System.out.println("base disp");
return null;
}
}
class sub1 extends base
{
B disp() // co-variant return type
{
System.out.println("sub disp");
return null;
}
}
public class Demo21
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Demo 22:
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
/*void disp()
{
System.out.println("sub disp");
}*/
/*protected void disp()
{
System.out.println("sub disp");
}*/
public void disp()
{
System.out.println("sub disp");
}
}
public class Demo22
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Disp 23:
class base
{
protected void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp() // error
{
System.out.println("sub disp");
}
/*protected void disp()
{
System.out.println("sub disp");
}
public void disp()
{
System.out.println("sub disp");
}*/
}
public class Demo23
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
Demo 24:
class base
{
public void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
/*void disp() // error
{
System.out.println("sub disp");
}*/
/*protected void disp() // error
{
System.out.println("sub disp");
}*/
public void disp()
{
System.out.println("sub disp");
}
}
public class Demo24
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp();
}
}
what is Binding?
Ref.disp()
1.type(always compiler knows)======in early
binding(static,final,private)
2.content(can be changed or runtime also) ====in late binding(remaining
else)
Ref.disp()
Ref= type of ref is base
Demo 26:
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp()
{
System.out.println("sub1 disp");
}
}
class sub2 extends sub1
{
}
public class isa
{
public static void main(String args[])
{
base ref=new sub2(); // upcasting
ref.disp(); // late binding
}
}
Demo 27:
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp()
{
System.out.println("sub1 disp");
}
void print()
{
System.out.println("sub1 print");
}
}
public class isa
{public static void main(String args[])
{
base ref=new sub1(); // upcasting
ref.disp(); // late binding
ref.print(); //error
}}
Day 5:
Example 1:
class Animal
{
void makeSound()
{
}
}
class Tiger extends Animal
{
void makeSound() // overriding makeSound() of Animal
{
S.o.p("roar");
}
}
class Dog extends Animal
{
void makeSound() // overriding makeSound() of Animal
{
S.o.p("bark");
}
}
class Cat extends Animal
{
void makeSound() // overriding makeSound() of Animal
{
S.o.p("meow");
}
}
public class AnimalDemo1
{
p.s.v.main(String args[])
{
// create objects of Tiger, Dog and Cat
// invoke "makeSound()" on these objects
Tiger t1=new Tiger();
t1.makeSound();
Dog d1=new Dog();
d1.makeSound();
Cat c1=new Cat();
c1.makeSound();
}
}
When do we do overriding?
}
}
let's hope that your code is working fine now.
add one more child class of Animal i.e. "Elephant" and override
"makeSound()" in it.
remove the class "Tiger" from your hierarchy and think what change do
you need to make inside "perform()" so that code will work fine.
you must have seen that whenever any changes happen to our class
hierarchy, we need to make changes in the "perform()" method. This is
called as "Maintenance drawback".
main()
{
invoke perform by passing instances of Animal child classes
}
in the above code "ref" is of type "Animal". But it can take any child
class object as an argument. When we pass any child object to "ref" [
which is of parent class type ] , it is known as "Upcasting".
copy "perform()" method from the previous code and paste it inside
"AnimalDemo4" class.
now think how will you invoke "hunting()" method from "perform()"
method ?
}
p.s.v.main(String args[])
{
perform(new Dog());
perform(new Cat());
perform(new Tiger());
perform(new Elephant());
even though above code works it is not logically right because what we
are doing is the tiger object which is created inside main function on
that we are performing "makeSound" and inside "perform()" we are
creating a new tiger on which we are invoking "hunting()".
Solution:
what happend?
class Electronic_Appliance
{
void on()
{
}
void off()
{
}
}
class TV extends Electronic_Appliance
{
void on()
{
S.o.p("TV on");
}
void off()
{
S.o.p("TV off");
}
}
}
}
Solution:
if it works, then copy the entire code to new project and add one more
method in "Washing_Machine" class
void fillWater()
{
S.o.p("fillwater");
}
open your persondemo project and let's add some methods inside "Person"
class only :
void walk()
{
S.o.p("walk");
}
void talk()
{
S.o.p("talk");
}
void eat()
{
S.o.p("eat");
}
now invoke these methods with the help of all the child class objects.
class Person
{
void walk()
{
// code to walk
}
void talk()
{
// code to talk
}
void eat()
{
// code to eat
}
void sleep()
{
// code to walk
}
}
one more function which we can think about "Person" but we cannot write
implementation inside "Person" class i.e. "performDuties()" . Its
implementation must be given inside child classes.
But how will you make it compulsory for child classes to define
"performDuties()" ?
you will have to make it "abstract" and for that class also must be
"abstract".
e.g.
yes, it will be invoked from sub class constructor when sub class
gets instantiated.
e.g
abstract class A
{
abstract void disp1();
abstract void disp2();
abstract void disp3();
abstract void disp4();
+
concrete methods
}
abstract class B extends A
{
}
e.g.
void performDuties()
{
S.o.p("duties of Student");
}
void performDuties()
{
S.o.p("duties of Employee");
}
Example1
create new project
Solution:
What is abstraction?
with the help of abstract class you can achieve 0 to 100% abstraction.
What does that mean?
if abstract class does not have any abstract method, that means there
is no abstraction.
if abstract class has got some abstract methods ( at least one ), there
is a partial abstraction.
if abstract class has got all the abstract methods, in that case we can
achieve 100% abstraction.
Example 2
create a new project
EventDemo1 class with main function
class GuiApp1
{
// how can we extend MouseEvent and WindowEvent both in this
class?
}
is it possible to say
one way is
void mouseEntered()
{
S.o.p("mouse entered");
}
void mouseExited()
{
S.o.p("mouse exited");
}
}
class GuiApp2 extends WindowEvent
{
void windowClosing()
{
S.o.p("window closing");
}
void windowOpening()
{
S.o.p("window opening");
}
}
now create objects of these two classes and invoke their methods
from main function.
since our MouseEvent and WindowEvent classes have got only abstract
methods ( contracts ) we can have them as "interfaces" instead of
"abstract classes".
inteface MouseEvent
{
void mouseClicked();
void mouseEntered();
void mouseExited();
}
interface WindowEvent
{
void windowClosing();
void windowOpening();
}
class GuiApp implements MouseEvent,WindowEvent
{
// define all abstract methods and write some message in all
these methods
}
inside main function create object of "GuiApp" class and invoke all the
methods ( 5 methods )
intefaces are abstract in nature. i.e. they have all abstract methods
(100% abstraction )
Example 2:
let's see one more example.
interface Storage
{
void store();
void load();
void display();
}
class FileStorage implements Storage // implementation of Storage
{
public void store()
{
S.o.p("store inside filesystem");
}
public void load()
{
S.o.p("load from filesystem");
}
public void display()
{
S.o.p("display from filesystem");
}
}
class DatabaseStorage implements Storage
{
public void store()
{
S.o.p("store inside Database");
}
public void load()
{
S.o.p("load from Database");
}
public void display()
{
S.o.p("display from Database");
}
}
Solution:
e.g.
abstract class Shape
{
abstract void draw(); // every child must define draw
}
interface example.......
abstraction means showing relevant attributes of an object and hiding
unnecessary details.
The user who want to use the methods of the interface, he only
knows the contract of this interface i.e its methods, information about
the implementation is completely hidden from the user, thus achieving
100% abstraction.
class Weapon
{
void attack()
{
}
}
class Gun extends Weapon
{
void attack()
{
System.out.println("attack with Gun");
}
}
class Bomb extends Weapon
{
void attack()
{
System.out.println("attack with Bomb");
}
}
class Sword extends Weapon
{
void attack()
{
System.out.println("attack with Sword");
}
}
public class WeaponDemo1
{
static void perform(Weapon ref)
{
ref.attack(); // polymorphic invocation
}
public static void main(String args[])
{
perform(new Gun());
perform(new Bomb());
perform(new Sword());
}
}
Demo 2:
class Weapon
{
void attack()
{
}
}
class Gun extends Weapon
{
void attack()
{
System.out.println("attack with Gun");
}
void fillBullets()
{
System.out.println("filling bullets");
}
}
class Bomb extends Weapon
{
void attack()
{
System.out.println("attack with Bomb");
}
}
class Sword extends Weapon
{
void attack()
{
System.out.println("attack with Sword");
}
}
public class WeaponDemo1_a
{
static void perform(Weapon ref)
{
// along with attack also invoke "fillBullets()" method of Gun
ref.fillBullets();
ref.attack(); // polymorphic invocation //but show error
bcoz parent no having attach function
}
public static void main(String args[])
{
perform(new Gun());
//perform(new Bomb());
//perform(new Sword());
}
}
Demo 3:
class Weapon
{
void attack()
{
}
}
class Gun extends Weapon
{
void attack()
{
System.out.println("attack with Gun");
}
void fillBullets()
{
System.out.println("filling bullets");
}
}
class Bomb extends Weapon
{
void attack()
{
System.out.println("attack with Bomb");
}
}
class Sword extends Weapon
{
void attack()
{
System.out.println("attack with Sword");
}
}
public class WeaponDemo1_b
{
static void perform(Weapon ref)
{
// along with attack also invoke "fillBullets()" method of Gun
// ref.fillBullets(); fillBullets() is not there inside //
"Weapon" class, so u can't invoke it using Weapon class reference.
Demo 4:
class Weapon
{
void attack()
{
}
}
class Gun extends Weapon
{
void attack()
{
System.out.println("attack with Gun");
}
void fillBullets()
{
System.out.println("filling bullets");
}
}
class Bomb extends Weapon
{
void attack()
{
System.out.println("attack with Bomb");
}
}
class Sword extends Weapon
{
void attack()
{
System.out.println("attack with Sword");
}
}
public class WeaponDemo2
{
static void perform(Weapon ref)
{
// along with attack also invoke "fillBullets()" method of Gun
// we would like to invoke "fillBullets()" on that Gun which is there
inside "ref"
Gun ob=(Gun)ref; // downcasting
ob.fillBullets(); //but in that in all weapons there is
fill bullet call by downcasting
ref.attack(); // polymorphic invokation
}
public static void main(String args[])
{
perform(new Gun());
//perform(new Bomb());
//perform(new Sword());
}
}
Demo 5:
class Weapon
{
void attack()
{
}
}
class Gun extends Weapon
{
void attack()
{
System.out.println("attack with Gun");
}
void fillBullets()
{
System.out.println("filling bullets");
}
}
class Bomb extends Weapon
{
void attack()
{
System.out.println("attack with Bomb");
}
}
class Sword extends Weapon
{
void attack()
{
System.out.println("attack with Sword");
}
}
public class WeaponDemo4
{
static void perform(Weapon ref)
{
// along with attack also invoke "fillBullets()" method of Gun
}
}
e.g.
if "ref" refers to "Dac_Dbda" and u check
if(ref instanceof Dac_Dbda) it returns "true"
class base
{
void disp()
{
S.o.p("base disp");
}
}
class sub extends base
{
private void disp()
{
S.o.p("sub disp");
}
}
above code will not compile, but for the time being let's assume that
it will compile successfully. What can be the side effect of this code?
here for the compiler "ref" is of type "base", so compiler will search
"disp" in base
is it there ? yes
is it accessible ? yes
is it final ? no
is is static ? no
class base
{
void disp()
{
S.o.p("base disp");
}
}
class sub extends base
{
private void disp() // compilation error
{
S.o.p("sub disp");
}
}
class base
{
String disp()
{
S.o.p("base disp");
return null;
}
}
class sub extends base
{
Object disp()
{ //object_string,int.Null
We cant put string in object bcoz
string is sub type of object
S.o.p("sub disp");
return null;
}
}
above code will not compile, but for the time being let's assume that
it will compile successfully. What can be the side effect of this code?
here for the compiler "ref" is of type "base", so compiler will search
"disp" in base
is it there ? yes
is it accessible ? yes
is it final ? no
is is static ? no
String s=ref.disp();
class base
{
String disp()
{
S.o.p("base disp");
return null;
}
}
class sub extends base
{
Object void disp() // Compilation error
{
S.o.p("sub disp");
return null;
}}
Demo 28:
If upcasting is done downcasting will be successful and if upcasting
fail downcasting will fail at run time
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp()
{
System.out.println("sub1 disp");
}
}
public class Demo28
{
public static void main(String args[])
{
base ref1=new sub1(); //upcasting
sub1 s1=(sub1)ref1; //downcasting //sub reference
pointing sub object
System.out.println("first test over");
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp()
{
System.out.println("sub1 disp");
}
}
public class is
{
public static void main(String args[])
{
base ref1=new sub1(); //upcasting
if(ref1 instanceof base) // sub is base
{
System.out.println("it is base");
}
if(ref1 instanceof sub1) // sub is sub
{
System.out.println("it is sub1");
}
package day4;
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub1 extends base
{
void disp()
{
System.out.println("sub1 disp");
}
}
public class is
{
public static void main(String args[])
{
base ref1=new sub1(); //upcasting
if(ref1 instanceof sub1) //sub is sub
{
sub1 s1=(sub1)ref1;
}
else
{
System.out.println("can not convert to s1");
}
System.out.println("first test over");
base ref2=new base();
if(ref2 instanceof sub1) //base is sub
{
sub1 s2=(sub1)ref2;
}
else
{
System.out.println("can not convert to s2");
}
System.out.println("second test over");}}
Demo 30:
// use of upcasting
class Shape
{
void draw(){}
}
class Triangle extends Shape
{
void draw()
{
System.out.println("in Triangle draw");
}
}
class Circle extends Shape
{
void draw()
{
System.out.println("in Circle draw");
}
}
class Rect extends Shape
{
void draw()
{
System.out.println("in Rect draw");
}
}
perform(new Triangle());
}
}
Demo 31:
perform(new Triangle());
perform(new Poly());
}
}
Demo 33:
interface emp1 //interface class created
{
void disp1(); // public and abstract
}
class sub1 implements emp1
{
/*public*/ void disp1()
{
System.out.println("sub1 disp1");
}
}
public class Demo33
{
public static void main(String args[])
{sub1 s1=new sub1();
s1.disp1();}}
interface emp1
{
void disp1(); // by default in interface functions are public
and abstract
}
interface emp2
{
void disp2();
}
class sub1 implements emp1,emp2
{
public void disp1()
{
System.out.println("sub1 disp1");
}
public void disp2()
{
System.out.println("sub1 disp2");
}
}
public class Demo34
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp1();
s1.disp2();
}
}
}
}
Demo 35:
interface emp1
{
void disp1(); // public and abstract
}
interface emp2
{
void disp2();
}
interface emp3 extends emp1,emp2 //prior java 8 muliple inheritance not
happen
{
void disp3();
}
class sub1 implements emp3
{
public void disp1()
{
System.out.println("sub1 disp1");
}
public void disp2()
{
System.out.println("sub1 disp2");
}
public void disp3()
{
System.out.println("sub1 disp3");
}
}
public class Demo35
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp1();
s1.disp2();
s1.disp3();
}
}
Demo 37:
interface emp1
{
void disp1(); // public and abstract
}
interface emp2
{
void disp2();
}
interface emp3 extends emp1,emp2
{
void disp3();
}
class base
{
void print()
{
System.out.println("base print");
}
}
class sub1 extends base implements emp3
{
public void disp1()
{
System.out.println("sub1 disp1");
}
public void disp2()
{
System.out.println("sub1 disp2");
}
public void disp3()
{
System.out.println("sub1 disp3");
}
}
public class Demo36
{
public static void main(String args[])
{
sub1 s1=new sub1();
s1.disp1();
s1.disp2();
s1.disp3();
s1.print();
}
}
Demo 38:
class base
{
public void disp()
{
System.out.println("in base disp");
}
}
class sub1 extends base
{
public void disp()
{
System.out.println("in sub1 disp");
}
}
class sub2 extends base
{
public void disp()
{
System.out.println("in sub2 disp");
}
}
class sub3 extends base
{
public void disp()
{
System.out.println("in sub3 disp");
}
}
public class Demo38
{
for(int i=0;i<arr.length;i++)
{
arr[i].disp();
}
}
}
Demo 39:
class base
{
public void disp()
{
System.out.println("in base disp");
}
}
class sub1 extends base
{
public void disp()
{
System.out.println("in sub1 disp");
}
}
class sub2 extends base
{
public void disp()
{
System.out.println("in sub2 disp");
}
}
class sub3 extends base
{
public void disp()
{
System.out.println("in sub3 disp");
}
}
public class Demo39
{
for(int i=0;i<arr.length;i++)
{
if(arr[i] instanceof sub2)
{
arr[i].disp();
break;
}
}
}
}
Demo 40:
interface base
{
void disp();
}
class sub1 implements base
{
public void disp()
{
System.out.println("in sub1 disp");
}
}
class sub2 implements base
{
public void disp()
{
System.out.println("in sub2 disp");
}
}
class sub3 implements base
{
public void disp()
{
System.out.println("in sub3 disp");
}
}
public class Demo40
{
for(int i=0;i<arr.length;i++)
{
if(arr[i] instanceof sub2)
{
arr[i].disp();
break;
}
}
}
}
Special:
class base
{
int i;
base()
{
add(1);
}
void add(int v)
{
i+=v;
}
void print()
{
System.out.println(i);
}
}
class sub extends base
{
sub()
{
add(2);
}
void add(int v) //jiska object uski method call in java
{
i+=v*2; //i=1*2=2 i=2*2+2=6 i=8*2+6=22
}
}
public class Special
{
static void disp(base b)
{
b.add(8);
b.print();
}
public static void main(String args[])
{
disp(new sub());
}
}
R n D static block
class base
{
static
{
System.out.println("in base static");
}
base()
{
System.out.println("base const");
}
}
class sub extends base
{
static
{
System.out.println("in sub static");
}
}
public class myclass1
{
System.out.println("....................");
sub s=new sub();
}
}
o/p:
in base static
base const
....................
in sub static
base const
Instance of:
class base
{
}
class sub extends base
{
}
public class myclass extends sub
{
System.out.println("**********************");
base b=new sub();
if(b instanceof base) //call
{
System.out.println("base");
}
if(b instanceof sub)
{
System.out.println("sub");
}
if(b instanceof myclass)
{
System.out.println("myclass");
}
if(b instanceof Object) //call
{
System.out.println("Object");
}
}
}
// myclass
sub
base
Object
**********************
base
sub
Object
Program to interface and program to implementation info:
Demo1:
class Weapon
{
void attack()
{
}
// some other stuff
}
class Gun extends Weapon
{
void attack()
{
System.out.println("Gun attack");
}
}
class Sword extends Weapon
{
void attack()
{
System.out.println("Sword attack");
}
}
class Bomb extends Weapon
{
void attack()
{
System.out.println("Bomb attack");
}
}
class Soldier
{
// program to implementation
class Weapon
{
void attack()
{
}
// some other stuff
}
class Gun extends Weapon
{
void attack()
{
System.out.println("Gun attack");
}
}
class Sword extends Weapon
{
void attack()
{
System.out.println("Sword attack");
}
}
class Bomb extends Weapon
{
void attack()
{
System.out.println("Bomb attack");
}
}
class Soldier
{
void perform()
{
c.drive();
or
b.drive();
}
}
vs
class Driver
{
// program to interface
FourWheeler ref;
Demo 4:
class base
{
static int a=20;
static
{
System.out.println("base static");
}
}
class sub extends base
{
static int a=10;
static
{
System.out.println("sub static");
}
}
public class MyClass1
{
public static void main(String[] args)
{
System.out.println(sub.a);
}}
Early binding:
Static used in this so there is early binding.
Demo1:
class base
{
static void disp() //static used here so early binding
{
System.out.println("base disp");
}
}
class sub extends base
{
static void disp()
{
System.out.println("sub disp");
}
}
public class Demo1
{
public static void main(String args[])
{
sub s=new sub();
s.disp(); // sub disp
base ref=new sub();
ref.disp();
Demo 2:
class base
{
void disp()
{
System.out.println("base disp");
}
}
class sub extends base
{
static void disp() // error overriding method is static
{
System.out.println("sub disp");
}
}
public class c1
{
public static void main(String args[])
{
sub s=new sub();
s.disp();
base ref=new sub();
ref.disp();
}
}
Demo 3:
class base
{
static void disp()
{
System.out.println("base disp");
}
}
class sub extends base
{
void disp() // error overridden method is static
{
System.out.println("sub disp");
}
}
public class Demo3
{
public static void main(String args[])
{
sub s=new sub();
s.disp();
base ref=new sub();
ref.disp();
}
}
class base
{
static void disp(int k)
{
System.out.println("base disp");
}
}
class sub extends base
{
static void disp() //overloading done
{
System.out.println("sub disp");
}
}
public class c1
{
public static void main(String args[])
{
sub s=new sub();
s.disp(); //sub disp call
*/
}
}
Demo 5:
class base
{
static void disp()
{
System.out.println("base disp");
}
}
class sub extends base
{
static void disp(int k)
{
System.out.println("sub disp");
}
}
public class Demo5
{
public static void main(String args[])
{
sub s=new sub();
s.disp();
s.disp(20);
base.disp();
sub.disp(20);
*/
}
}
Demo 6:
class base
{
final void disp()
{
System.out.println("base disp");
}
}
class sub extends base
{
/*void disp() Not Possible
{
System.out.println("sub disp");
}*/
}
public class Demo6
{
public static void main(String args[])
{
base ref=new sub();
ref.disp(); // early binding
}
}
Demo 7:
class base
{
private void disp()
{
System.out.println("base disp");
}
}
class sub extends base
{
void disp()
{
System.out.println("sub disp");
}
}
public class c1
{
public static void main(String args[])
{
base ref=new sub();
ref.disp(); // error disp() has private access in base
}
}
Demo 8:
class sub extends Demo8
{
void disp()
{
System.out.println("sub disp");
}
}
public class Demo8
{
private void disp()
{
System.out.println("Demo8 disp");
}
public static void main(String args[])
{
Demo8 ref=new sub();
ref.disp(); // early binding
}
}
class base
{
static int a=20;
static
{
System.out.println("base static");
}
}
class sub extends base
{
static int a=10;
static
{
System.out.println("sub static");
}
}
public class MyClass1
{
public static void main(String[] args)
{
System.out.println(sub.a);
}
base static
sub static
10
Second:
class base
{
static void disp(int k)
{
System.out.println("base disp");
}
static
{
System.out.println("base static block");
}
}
class sub extends base
{
static void disp()
{
System.out.println("sub disp");
}
static
{
System.out.println("sub static block");
}
}
public class Demo4
{
public static void main(String args[])
{
sub.disp(4);
}
}
output:
Third:
class base
{
static void disp(int k)
{
System.out.println("base disp");
}
static
{
System.out.println("base static block");
}
}
class sub extends base
{
static void disp()
{
System.out.println("sub disp");
}
static
{
System.out.println("sub static block");
}
}
public class Demo4
{
public static void main(String args[])
{
sub.disp();
}
}
/*
*/
class Sample
{
static void disp1()
{
System.out.println("in disp1");
}
void disp2()
{
System.out.println("in disp2");
}
}
public class one
{
}
}
/*
output:
in disp1
*/
R_n_D in java:
public class Demo2
{
void disp(Object o)
{
System.out.println("in Object disp");
}
void disp(String o)
{
System.out.println("in String disp");
}
/*
output:
in string disp
in string disp
in object disp
*/
About Interface:
interface myinterface
{
void disp1();
void disp2();
}
class sub implements myinterface
{
public void disp1()
{
System.out.println("disp1");
}
public void disp2()
{
System.out.println("disp2");
}
public void print()
{
System.out.println("print");
}
}
public class InterfaceDemo
{
public static void main(String args[])
{
myinterface ref=new sub();
ref.disp1();
ref.disp2();
// ref.print(); // error print not in myinterface
System.out.println(ref.toString()); // works
}
}
/*
*/
About Overloading:
Demo 1:
class base
{
void disp(Object ref)
{
System.out.println("in Object method");
}
void disp(String ref)
{
System.out.println("in String method");
}
}
public class MyClass
{
public static void main(String[] args)
{
base b=new base();
b.disp("hello"); //nearest
}
}
Demo2:
class base
{
void disp(Object ref)
{
System.out.println("in Object method");
}
}
class sub extends base
{
void disp(String ref)
{
System.out.println("in String method");
}
}
public class MyClass
{
public static void main(String[] args)
{
base b=new sub();
b.disp("hello");
/*here for compiler, b is of type "base"
compiler will check "disp("hello") " inside "base" class
yes
it's disp(Object)
*/
s.disp(100);
}
}
Demo 3:
class base
{
void disp(String ref)
{
System.out.println("in String method");
}
}
class sub extends base
{
void disp(Object ref)
{
System.out.println("in Object method");
}
}
public class MyClass
{
public static void main(String[] args)
{
base b=new sub();
b.disp("hello");
/*
output:
in String method
in String method
in Object method
Favour composition over inheritance is a one of the popular object oriented design principles, which helps us to
create flexible and maintainable code in object oriented languages.
Inheritance drawbacks:
a) Tight coupling- if base class ( Fourwheeler) is changed, sub class (Car) will break.
b) inheritance breaks encapsulation. white-box reuse. That is, with inheritance, the parent class
implementation is often visible to the subclasses.
Composition Advantages:
black-box reuse as it does not break encapsulation. “Car” knows only an interface “Engine”. It doesn’t know the
implementation
Loose coupling, program to interface. During runtime any implementations (such as “HondaEngine”
,”MarutiEngine” or “BMWEngine” can be passed to “Engine ref” and “on()” method can be invoked
polymorphically.)
White box-in white box we know which car we invoke e.g BMW
Black box-we invoke Engine we don’t know which engine is invoked.
In Object Oriented Programming, there are many different types of relationships which can exist between two or
more classes. The most common two types are:
This usually represents when there are two classes, ClassA and ClassB, and either:
There are two sub-types of Association relationships — Aggregation and Composition. What’s the difference
between these two?
Composition
Composition implies that the contained class cannot exist independently of the container. If the container is
destroyed, the child is also destroyed.
Take for example a Page and a Book. The Page cannot exist without the Book, because the book is composed
of Pages. If the Book is destroyed, the Page is also destroyed.
In code, this usually refers to the child instance being created inside the container class:
class Book:
def __init__(self):
page1 = Page('This is content for page 1')
page2 = Page('This is content for page 2')
self.pages = [page1, page2]
class Page:
def __init__(self, content):
self.content = contentbook = Book() # If I destroy this Book instance,
# the Page instances are also destroyed
Aggregation
So thinking of a Car and an Engine, the Engine doesn’t need to be destroyed when the Car is destroyed.
class Car:
def __init__(self, engine):
self.engine = engine
class Engine:
def __init__(self):
passengine = Engine()
car = Car(engine) # If I destroy this Car instance,
# the Engine instance still exists
In a UML diagram, both Aggregation and Composition are represented with a diamond arrow between the
classes. The diamond end goes on the side of the container.
Here’s an example:
Aggrigation Demo:
package aggregation;
class Teacher
{
private String name;
private int age;
public Teacher(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
class Department
{
private Teacher teacher;
private String dname;
public Department(String dname)
{
teacher=null;
this.dname=dname;
}
public String getDname()
{
return dname;
}
public void addTeacher(Teacher teacher)
{
this.teacher=teacher;
}
public void perform()
{
System.out.println(dname+"\t");
teacher.work();
}
}
}
Sequence:
1) Department class gets instantiated and its members are allocated
memory.
2) "md" reference is created on the stack.
3) "md" refers to the instance of Department inside heap.
4) Teacher class gets instantiated and its members are allocated
memory.
5) "t1" reference is created on the stack.
6) "t1" refers to the instance of Teacher inside heap.
7) when "addTeacher()" method gets called,"teacher" reference of
Teacher class inside the instance of Department refers to the Teacher
instance on the heap.
8) we say "md=null" inside the application.
9) Department instance on the heap is marked for GC.
10) That does not affect Teacher instance available on heap because it
is still referenced by "t1" reference on the stack.
Composition Demo:
package aggregation;
import java.util.Scanner;
class Room
{
private String name;
public Room(String name)
{
this.name=name;
}
public Room()
{
name=null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
};
class House
{
private Room rooms[];
private String name;
private String address;
private int no_rooms;
public House(String name,String address,int no_rooms)
{
this.name=name;
this.address=address;
this.no_rooms=no_rooms;
this.rooms=new Room[no_rooms];
String rname;
Scanner sc=new Scanner(System.in);
for(int i=0;i<no_rooms;i++)
{
System.out.println("enter room name");
rname=sc.next();
this.rooms[i]=new Room();
this.rooms[i].setName(rname);
}
}
This():
Trial 1:
public class MyDynamicArray
{
public MyDynamicArray()
{
this(10); //so it invoke below constructor
}
Trial 2:
class base
{
base()
{
System.out.println("in base no-arg");
}
}
public class trial1 extends base
{
trial1()
{
this(30);
System.out.println("in def const");
}
trial1(int k)
{
this(20,40);
System.out.println("in 1 param");
}
trial1(int x,int y)
{
System.out.println("in 2 param");
}
public static void main(String args[])
{
trial1 t=new trial1();
}
}
Trial 3:
class base
{
base(int k)
{
System.out.println("in base param");
}
}
public class trial2 extends base
{
trial2()
{
this(30);
System.out.println("in def const");
}
trial2(int k)
{
this(20,40);
System.out.println("in 1 param");
}
trial2(int x,int y)
{
super(30); // super is required here
Enum pro
enum MyFont
{
PLAIN,BOLD,ITALIC
}
Enum:
You can also create your own constants by marking a variable static const. But sometimes you'll want to create
a set of constant values to represent the only valid values for a variable. This set of valid values is commonly
referred to as an enumeration.
Let's say that you 're creating a GUI Application where, u need to create ur component and set the font style (
plain or bold or italic).
Demo 1:
class MyFont
{
public static final int PLAIN=0;
public static final int BOLD=1;
public static final int ITALIC=2;
}
class MyComponent
{
public void setStyle(int style)
{
switch(style)
{
case 0: System.out.println("plain");
break;
case 1: System.out.println("bold");
break;
case 2: System.out.println("italic");
break;
default: System.out.println("unpredictable font");
}
}
}
public class Demo1
{
public static void main(String args[])
{
MyComponent mc=new MyComponent();
mc.setStyle(MyFont.BOLD);
mc.setStyle(MyFont.ITALIC);
// mc.setStyle(MyFont.plain); //Error
mc.setStyle(1); // saves typing
mc.setStyle(6); // can be risky
}
}
Demo 2:
enum MyFont
{
PLAIN,BOLD,ITALIC
}
class MyComponent
{
publicvoid setStyle(MyFont font)
{
switch(font)
{
case PLAIN: System.out.println("plain");
6 break;
case BOLD: System.out.println("bold");
break;
case ITALIC: System.out.println("italic");
break;
}
}
}
public class Demo1
{
public static void main(String args[])
{
MyComponent mc=new MyComponent();
mc.setStyle(MyFont.BOLD);
mc.setStyle(MyFont.ITALIC);
mc.setStyle(MyFont.PLAIN);
}
}
The good news about this technique is that it DOES make the code easier to read. The other good news is that
you can't ever change the value of the fake enums you've created; e.g. BOLD will always be 1. The bad news is
that there's no easy or good way to make sure that the value of “style” will always be 0, 1, or 2. If some hard to
find piece of code sets “style” equal to 6, it's possible that your code will break..
Garbage collector:
Runtime.getRuntime().gc();
Class Runtime
getRuntime is reference of runtime======static
gc is the function=====non static method
e.g System.out.print();
Example:
Immutable demo:
class Immutable
{
private int num;
System.out.println(i1);
System.out.println(i2);
}
}
Mutable demo:
class Mutable
{
private int num;
Object methods:
Demo1:
package day6;
class MyNum
{
int num;
MyNum(int num)
{
this.num=num;
}
}
public class ObjectDemo1
{
public static void main(String args[])
{
MyNum m1=new MyNum(100);
MyNum m2=new MyNum(100);
if(m1.equals(m2))
{
System.out.println("m1 and m2 are equals");
}
else
{
System.out.println("m1 and m2 are not equals"); //else
printed
}
if(m1==m2)
{
System.out.println("m1 and m2 are ==");
}
else
{
System.out.println("m1 and m2 are not ==");//else printed
}
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());
}
}
Demo2:
package day6;
class MyNum
{
int num;
MyNum(int num)
{
this.num=num;
}
if(m1.equals(m2))
{
System.out.println("m1 and m2 are equals");
}
else
{
System.out.println("m1 and m2 are not equals");
}
if(m1==m2)
{
System.out.println("m1 and m2 are ==");
}
else
{
System.out.println("m1 and m2 are not ==");
}
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());}}
Demo 3:
class MyNum
{
int num;
MyNum(int num)
{
this.num=num;
}
System.out.println(m1.toString());
System.out.println(m2);
if(m1==m2)
{
System.out.println("m1 and m2 are ==");
}
else
{
System.out.println("m1 and m2 are not ==");
}
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());
}
}
What is diff between equals() and == ?
By default equals() and == operator both are same they check wether two
references refer to same obj or not cause equals() method of object
class
Internally uses == operator
The diff is that equals() can be overridden in order check the content
Demo 4:
class MyNum
{
int num;
MyNum(int num)
{
this.num=num;
}
System.out.println(m1.toString());
System.out.println(m2);
if(m1==m2)
{
System.out.println("m1 and m2 are ==");
}
else
{
System.out.println("m1 and m2 are not ==");
}
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());}}
Demo 5:
class MyNum
{
int num;
MyNum(int num)
{
this.num=num;
}
System.out.println(m1.toString());
System.out.println(m2);
if(m1==m2)
{
System.out.println("m1 and m2 are ==");
}
else
{
System.out.println("m1 and m2 are not ==");
}
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());
}
}
Demo 6:
class MyNum
{
int num;
MyNum(int num)
{
this.num=num;
}
System.out.println(m1.toString());
System.out.println(m2);
if(m1.equals(m2))
{
System.out.println("m1 and m2 are equals");
}
else
{
System.out.println("m1 and m2 are not equals");
}
if(m1==m2)
{
System.out.println("m1 and m2 are ==");
}
else
{
System.out.println("m1 and m2 are not ==");
}
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());
}
}
Demo 7:
class MyNum
{
int num;
MyNum(int num)
{
this.num=num;
}
System.out.println(m1.toString());
System.out.println(m2);
if(m1.equals(m2))
{
System.out.println("m1 and m2 are equals");
}
else
{
System.out.println("m1 and m2 are not equals");
}
if(m1==m2)
{
System.out.println("m1 and m2 are ==");
}
else
{
System.out.println("m1 and m2 are not ==");
}
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());}}
Equals() and hashCode() both are contract if equals is true must
hashCode() also returns the same value
2.If two objects are equal according to the equals(Object) method, then calling the hashCode method on
each of the two objects must produce the same integer result.
3.It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then
calling the hashCode method on each of the two objects must produce distinct integer results. They may
return same hashcode.
Demo1:
Demo2:
package day6;
public class StringDemo2
{
public static void main(String args[])
{
String s1=new String("hello");
String s2=new String("hello");
System.out.println(s1);
System.out.println(s2);
if(s1==s2)
{
System.out.println("s1 and s2 are ==");
}
else
{
System.out.println("s1 and s2 are not ==");
}
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
}
}
Demo3:
package day6;
public class StringDemo3
{
public static void main(String args[])
{
String s1=new String("hello");
String s2="hello"; //string pool used
System.out.println(s1);
System.out.println(s2);
if(s1.equals(s2))
{
System.out.println("s1 and s2 are equals");
}
else
{
System.out.println("s1 and s2 are not equals");
}
if(s1==s2)
{
System.out.println("s1 and s2 are ==");
}
else
{
System.out.println("s1 and s2 are not ==");
}
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
}
}
Demo 4:
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
if(s2.equals(s3))
{
System.out.println("s2 and s3 are equals");
}
else
{
System.out.println("s2 and s3 are not equals");
}
if(s2==s3)
{
System.out.println("s2 and s3 are ==");
}
else
{
System.out.println("s2 and s3 are not ==");
}
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
}
}
Demo 5:
package day6;
public class StringDemo5
{
public static void main(String args[])
{
String s1=new String("hello");
String s2="hello";
String s3="hello";
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
if(s1.equals(s3))
{
System.out.println("s1 and s3 are equals");
}
else
{
System.out.println("s1 and s3 are not equals");
}
if(s1==s3)
{
System.out.println("s1 and s3 are ==");
}
else
{
System.out.println("s1 and s3 are not ==");
}
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
}
}
Demo 6:
public class StringDemo6
{
public static void main(String args[])
{
String s1="hello";
String s2=new String("hello");
if(s1==s2)
{
System.out.println("s1 and s2 are ==");
}
else
{
System.out.println("s1 and s2 are not ==");
}
}
}
Demo 7:
if(s1.equals(s2))
{
System.out.println("s1 and s2 are equals");
}
else
{
System.out.println("s1 and s2 are not equals");
}
}
}
Demo 8://
/* When the intern method is invoked, if the pool already contains a
string equal to this String object as determined by the equals(Object)
method, then the string from the pool is returned
*/
//2000 2000
if(s1 == s2.intern())
{
System.out.println("s1 and s2 are ==");
}
else
{
System.out.println("s1 and s2 are not ==");
}
}}
S2 will check the value of “Hello” and intern method goes to stringpool and
check the object where the value will match
Demo 9
*/
String s1=s2.intern();
if(s1 == s2)
{
System.out.println("s1 and s2 are ==");
}
else
{
System.out.println("s1 and s2 are not ==");
}
}
}
Op are not equal
Demo 10:
public class StringDemo10
{
public static void main(String args[])
{
String s1="hello";
System.out.println(s1);
System.out.println(s2);
}
}
Demo 11:
* in this code what if we would like to ignore cases */
String s2="Admin";
System.out.println(s1);
System.out.println(s2);
if(s1==s2)
{
System.out.println("s1 and s2 are ==");
}
else
{
System.out.println("s1 and s2 are not ==");
}
if(s1.equals(s2))
{
System.out.println("s1 and s2 are equals");
}
else
{
System.out.println("s1 and s2 are not equals");
}
}
}
admin
Admin
s1 and s2 are not ==
s1 and s2 are not equals
Demo 12:
String s2="Admin";
System.out.println(s1);
System.out.println(s2);
if(s1==s2) // false
{
System.out.println("s1 and s2 are ==");
}
else
{
System.out.println("s1 and s2 are not ==");
}
if(s1.equals(s2)) // false
{
System.out.println("s1 and s2 are equals");
}
else
{
System.out.println("s1 and s2 are not equals");
}
if(s1.equalsIgnoreCase(s2)) // true
{
System.out.println("s1 and s2 are equalsIgnoreCase");
}
else
{
System.out.println("s1 and s2 are not
equalsIgnoreCase");
}
}
}
Demo 13:
public class StringDemo13
{
public static void main(String args[])
{
String s1="hello";
}
}
Demo 14:
public class StringDemo14
{public static void main(String args[])
{
String s1="hello";
String s2="hel";
//2000 4000
if(s1==(s2+"lo"))
{System.out.println("true");
}
else
{System.out.println("false");
}
}}
Demo 15:
public class StringDemo15
{
public static void main(String args[])
{
String s1=new String("hello");
}
}False
Demo 16:
public class StringDemo16
{
public static void main(String args[])
{
String s1="hello";
final String s2="hel";
}
}
Demo 17:
int val=str1.compareTo(str2);
System.out.println("comparison is\t"+val);//-8 in two strin
System.out.println("Length of str1 is
\t"+str1.length());//10
String str3=str1.replace('e','i');//replace e to i
System.out.println(str1);
System.out.println(str3);
System.out.println(str1.substring(2));//llo
String str4="ABCDEFG";
String str5=str4.toLowerCase();//abcdefg
System.out.println(str4);
System.out.println(str5);
String str6=str1.toUpperCase();//ABCDEFG
System.out.println(str1);
System.out.println(str6);
}
}
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
}
}
}
}
Before JDK1.5
int num=10;
JDK1.5 onwards
int num=10;
Integer ob=num; // Autoboxing
Before JDK1.5
int num=10;
int temp=ob.intValue();
JDK1.5 onwards
int num=10;
Integer ob=num; // Autoboxing
int temp=ob; // unboxing
Demo1:
public class WrapperDemo1
{
public static void main(String args[])
{
// how do u wrap primitive inside the wrapper class ?
int num=100;
Integer ob=new Integer(num);
System.out.println(ob);
}
}
Demo2:
o/p = 135
Demo 4
public class WrapperDemo4
{
public static void main(String args[])
{
String str=" 125 ";
int k=Integer.parseInt(str.trim());
k+=10;
System.out.println(k);
}
}
Demo 5:
public class WrapperDemo5
{
public static void main(String args[])
{
int num=100;
int k=ob.intValue();
System.out.println(k);
}
}
Demo 6:
public class WrapperDemo6
{
public static void main(String args[])
{
int num=100;
System.out.println(num);
Day 7:
How to pass primitive to a method which has Object as a parameter JDK5
onwards
show(Object ref)
{
S.o.p(ref);
}
we can call this show() function as follows:
int num=100;
show(num); // it's possible from jdk5 onwards
i.e. compile does autoboxing ( converts int to Integer ) and pass that
Integer to the method which has "Object" as a parameter.
Varargs:
disp(10,20);
i.e. we must pass 2 arguments while calling this method. neither less
than 2 nor more than 2
}
[ ... is called as ellipsys]
Demo 1:
public class Demo1
{
void disp(int ...set) //elipsys
{
for(int i=0;i<set.length;i++)
{
System.out.println(set[i]);
}
for(int k:set) // foreach_(Same convert it like traditional
for loop) set will internally traverse 10=k ,20=k
{
System.out.println(k);
}
}
public static void main(String args[])
{
Demo1 d=new Demo1();
d.disp(10,20);
System.out.println(".................");
d.disp(100,200,300,400,500);
}
}
getClass().getName()
class A
{
public String toString()
{
return "My Name is\t"+getClass().getName();
}
}
Demo2:
class A
{
public String toString()
{
return "My Name is\t"+getClass().getName();
}
}
class B
{
public String toString()
{
return "My Name is\t"+getClass().getName();
}
}
class C
{
public String toString()
{
return "My Name is\t"+getClass().getName();
}
}
class D
{
public String toString()
{
return "My Name is\t"+getClass().getName();
}
}
public class Demo2
{
void disp(Object ...col)
{
for(int i=0;i<col.length;i++)
{
System.out.println(col[i]);
}
System.out.println("Using foreach loop");
for(Object ref:col)
{
System.out.println(ref);
}
}
public static void main(String args[])
{
A ob1=new A();
B ob2=new B();
C ob3=new C();
D ob4=new D();
new Demo2().disp(ob1,ob2,ob3,ob4);
}
}
overloading_info_with_widening_and boxing:
Overloading- considering
a)Widening (upcasting )
b)Autoboxing
c)Var-args
1)
void disp(int x) //windings
{
System.out.println("in int");
}
void disp(short y) //windings
{
System.out.println("in short");
}
byte b=30;
disp(b);
Ans:- in short
Primitive widening uses the "smallest" method argument possible.
2)
class A
{
void disp(Integer x) //boxing_Wrapper
{
S.o.p("in Integer");
}
void disp(int x) //widenings
{
S.o.p("in int");
}
.......
int num=5;
disp(num);
}
Ans:- in int
Boxing has taken a back seat
3)
class A
{
void disp(Integer x) //boxing
{
S.o.p("in Integer");
}
void disp(long x) //widening
{
S.o.p("in long");
}
.......
int num=5;
disp(num);
}
Ans:- in long
widening beats boxing
4)
class A
{
void disp(int x,int y) //widening
{
System.out.println("in two ints");
}
void disp(byte ...x) //varargs
{
System.out.println("in byte var arg");
}
.....
byte a=3,b=6;
disp(a,b);
}
Ans:- in two ints
Compiler chooses old style
5)
class A
{
void disp(Byte x,Byte y) //boxing
{
System.out.println("in two Bytes");
}
void disp(byte ...x) //varargs
{
System.out.println("in byte var arg");
}
.......
byte a=3,b=6;
disp(a,b);
}
Ans:- in two Bytes
boxing beats var-args
6)
e.g following code will not work
// compiler error
7)
Overloading When combining Widening and Boxing.
byte b=4;
disp(b); //boxing disp(new Byte(b)) first autoboxing i.e. byte
will be converted to Byte and then Byte will be passed to Object [
upcasting ]
8)
int p=20,q=30;
disp(p,q);
arguments which can be given to the running java program. it also means
that arguments given to main function. in Java main function is always
ready to accept command line arguments.
str="125";
// lets convert str into int
int k=Integer.parseInt(str);
k+=10;
System.out.println(k); // 135
}
}
Imp_Info_Protected:
Protected members are “accessible outside the package only through inheritance“.
i.e you can access a protected member of a class in its subclass present in some other
package directly as if the member is present in the subclass itself. But that protected member
will not be accessible in the subclass outside the package by using parent class’s reference.
e.g.
package core1;
public class Base
{
protected int cnt=20;
}
package core2;
import core1.Base;
Demo1:
package trial;
package trial1;
import trial.MyClass1;
class Base
{
Object disp()
{
System.out.println("in Base disp");
return null;
}
}
class Sub extends Base
{
String disp()
{
System.out.println("in Sub disp");
return null;
}
}
public class BridgeDemo
{
public static void main(String args[])
{
Base ref=new Sub();
ref.disp(); // during runtime check the content of "ref"
and invoke "Object disp()" method
// so during runtime since the content of "ref" is
"Sub", "Object disp()" of Sub which is a bridge method gets
//called which invokes "String disp()" which programmer has
defined.
Why exception:
Thid advantage is, because all exceptions thrown within a program are
objects, the grouping or categorizing of exceptions is a natural
outcome of the class hierarchy.
Sql exception-if we pass select , from table it will compile In java but
show error in sql.
Throw
Exceptions can be thrown by either java run time environment or by the code itself. JRE throws
exception when java’s rules are violated. An example is, when the code accesses an array location
which is not available then ArrayIndexOutOfBoundsException is thrown. Pretty nice name right and
obviously it explains the problem. All the java exception classes are not having any attributes and
standard methods. It is a common design. Class name describes what exception it is and the hierarchy
forms an organization chart kind of structure using which java exceptions are used.
Programmers can throw an exception to indicate a problem condition. Either java’s predefined
exceptions can be used or custom exceptions can be created by extending the already available
exceptions. Programmer can throw a custom exception or a predefined Java exception. Mostly custom
exceptions are created based on business conditions. Programmer can use the Java keyword ‘throw‘
to generate an exception. It is done by instantiating the exception class of choice and then thrown.
try – catch
A thrown exception should be handled. If the program does not handles an exception, then it will be
handled by the java run time environment. A block of code where an exception is expected should be
surrounded by try – catch block. try indicates the start of the exception handling block and catch the
end of the block. Following catch a block of code can be written which is the exception handling code
block. This is the part the handles the exception. A catch will have an exception identified and it will
catch only that type of exception. Type means the same exception and all its sub classes. There can be
multiple catch blocks for a try block.
throws
When a Java method is going to throw an exception, to indicate that as part of the method signature
‘throws‘ keyword should be used followed by the exception. It means that the caller of this method
should handle the exception given in the throws clause. There can be multiple exceptions declared to
be thown by a method. If the caller of that method does not handles the exception, then it propagates
to one level higher in the method call stack to the previous caller and similarly till it reaches base of
the method call stack which will be the java’s run time system.
finally
After catch block there can be one more block of code declared as ‘finally‘. Irrespective of whether
an exception is thrown or not, the finally block of code will always be executed. Important piece of
code that must be executed, even if a program fails belong to this finally block. Example would be
closing a database connection, a file handle, etc.
Demo 1:
}//ArrayIndexBoundException
Demo2:
ArrayIndexOutOfBoundsException:
public class Example2
{
public static void main(String args[])
{
int arr[]=new int[4];
System.out.println("Array created");
try
{
arr[4]=10;
}
catch(ArrayIndexOutOfBoundsException ae) //exception class
sub class
{
//System.out.println(ae);
ae.printStackTrace(); //check on which line have
exception
}
System.out.println("Array assigned");
}
}
Demo3:
// one try , multiple catch blocks.
public class Example3
{
public static void main(String args[])
{
int arr[]=new int[4];
Example6 e1=null;
System.out.println("Array created");
try
{
arr[4]=10; //only check first line error so throw to
catch
e1.toString();
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
}
catch(NullPointerException ne)
{
System.out.println(ne);
}
System.out.println("Array assigned");
}
}
Demo 4:
// the most generic catch block.
public class Example4
{
public static void main(String args[])
{
int arr[]=new int[4];
System.out.println("Array created");
try
{
arr[4]=10;
}
catch(Exception ae) //father of all exception_generic catch
block
{
System.out.println(ae);
}
System.out.println("Array assigned");
}
}
Demo 5:
// one try , multiple catch blocks.
/* when u define one try and multiple catch, the rule is most specific
catch block should precede most generic catch block
public class Example5
{
public static void main(String args[])
{
int arr[]=new int[4];
System.out.println("Array created");
try
{
arr[4]=10;
}
catch(ArrayIndexOutOfBoundsException ae)
{
// if i want to perform any specific task in case of
AIOOB(ArrayIndex………)
System.out.println(ae);
}
catch(Exception ae)
{
System.out.println(ae);
}
System.out.println("Array assigned");}}
Demo 6:
public class Example6
{
public static void main(String args[])
{
int arr[]=new int[4];
System.out.println("Array created");
try
{
arr[4]=10;
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println("In catch\t"+ae);
}
finally
{
System.out.println("I am always printed"); //always
printed
}
System.out.println("Array assigned");
}
}
Demo 7:
/*
finally{} block always executes whether or not exception is raised
*/
public class Example7
{
public static void main(String args[])
{
int arr[]=new int[4];
System.out.println("Array created");
try
{
arr[3]=10;
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println("In catch\t"+ae);
}
finally
{
System.out.println("I am always printed");
}
System.out.println("Array assigned");
}
}
What is “handle or declare” rule?
Ans:- a) whenever any method raises checked exception/s , method has to either handle
[try….catch] or declare [throws] that checked exception/s.
b) whenever u invoke a method which has declared [using throws] checked exception/s , caller
method has to either handle [try….catch] or declare [throws] that checked exception/s.
throws-declare
throw-raise
handle-try catch
you create checked exception when you would like your client to take
corrective action/s in case of exception. i.e. checked exception
somehow enforces client to handle it. (using try...catch) and the
corrective action/s can be taken inside catch block.
you create unchecked exception when you feel there is no need of any
corrective action by client in case of exception.
Client 1:
public class ClientApp1
{
static void disp()throws MyException
{
Calc c=new Calc();
int result=0;
result=c.sqr(-9);
System.out.println(result);
}
public static void main(String args[])
{
try
{
disp();
}
catch(MyException m)
{
System.out.println(m);
}
System.out.println("Done");
}}
ClientApp 2:
try
{
some code
}
catch(Object e) //exception or error type
{
why?
etc.
and the rule is along with "throw" you can have class of type
"Throwable" only.
Types of Exceptions in Java
Demo 1
package core1;
import java.io.IOException;
package core1;
import java.io.IOException;
Demo 3:
package core1;
import java.io.IOException;
import java.io.IOException;
Demo 5:
package core1;
import java.io.IOException;
package core1;
import java.io.IOException;
try
{
j.disp1();
}
catch(IOException e)
{
System.out.println(e); //now it worksp properly
}
}
}
/*
Catching multiple exceptions in a single catch block reduces code
duplication and increases efficiency.
/*
output:
/ by zero
*/
|=>(as well as) single pipe ,use more than one exception
Demo 2:
package firstpro;
public class Demo2
{
public static void main(String[] args)
{
try {
int array[] = new int[10];
array[10] = 30 / 5;
}
catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
/*
output:
java.lang.ArrayIndexOutOfBoundsException: 10
at firstpro.Demo2.main(Demo2.java:10)
Done
*/
Demo 3:
/*If the base exception class has already been specified in the
catch block, do not use child exception classes in the
same catch block. Otherwise, we will get a compilation error.*/
package firstpro;
Demo 4:
/*If the base exception class has already been specified in the
catch block, do not use child exception classes in the
same catch block. Otherwise, we will get a compilation error.*/
package firstpro;
}
R n d about runtime exception:
package datatypes_pro;
import java.io.IOException;
}
void disp2()throws RuntimeException // works
{
}
void disp3()
{
throw new Exception(); // error either handle or declare
bcoz parent not handle child
}
void disp4()
{
throw new RuntimeException(); //works
}
public static void main(String ...arg)
{
System.out.println("in main");
}
catch(IOException ii)
{
ii.printStackTrace();
}
/*
* No problem about following try and catch block
*/
try
{
}
catch(RuntimeException re)
{
re.printStackTrace();
}
}
}
e.g
try
{
FileInputStream fis=new FileInputStream("abc.txt");
// code to read the file
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
fis.close();
}
what is exactly Automatic Resource Management (ARM) ?
// ARM block
when you compile above code , compiler will convert this code into:
try
{
FileInputStream fis=new FileInputStream("abc.txt");
// code to read the file
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
fis.close();
}
No. inside ARM block you can write only those classes which
implement "AutoCloseable" or "Closeable" interface.
Assertions
How to do in eclipse:
Right click->run as configurator->name of code->arguments->vm
arguments->-ea so it enables assertion
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
System.out.println("done");
}
}
No class def:
class Y
{
class Z
{
}
public class Demo
{
static Y ref=new Y();
public static void main(String args[])
{
System.out.println("in main");
Z ref1=new Z();
}
}
If we compile both class x and y if anyone class is deleted so it shows no class def
same argument
return type can be covariant
access specifier same or more than that
class base
{
public void disp()throws IOException
{
}
}
class sub extends base
{
public void disp()
{
}
}
main()
{
base ref=new sub();
ref.disp();
class base
{
public void disp()throws IOException
{
}
}
class sub extends base
{
public void disp()throws IOException
{
}
}
main()
{
base ref=new sub();
ref.disp();
class base
{
public void disp()throws IOException
{
}
}
class sub extends base
{
public void disp()throws EOFException
{
}
}
main()
{
base ref=new sub();
ref.disp();
class base
{
public void disp()throws IOException
{
}
}
class sub extends base
{
public void disp()throws Exception
{
}
}
main()
{
base ref=new sub();
ref.disp();
}
class base
{
public void disp()
{
}
}
class sub extends base
{
public void disp()throws Exception // compilation error(parent
cannot access child)
{
}
}
main()
{
base ref=new sub();
ref.disp();
}
Day7_remaining part
Inner classes:
The inner class shares a special relationship with the outer class i.e. the inner class has access to all
members of the outer class and still have its own type is the main advantages of Inner class.
Advantage of inner class is that they can be hidden from the other classes in the same package and
still have the access to all the members (private also) of the enclosing class. This increases the level of
encapsulation.
Inner classes are best used in the event handling mechanism and to implement the helper classes.
Nested classes are divided into two categories: static and non-static.
Nested classes that are declared static are called static nested classes.
classes which are defined inside any method are known as "Local Inner Classes".
static
{
System.out.println("in Outer static block");
}
void outerDisp()
{
System.out.println(num1+"\t"+num2+"\t"+num3+"\t"+num4);
}
o1.outerDisp();
i.innerDisp();
}
}
Anonymous class:
It is not compact:
interface Myinterface{
void disp();
@Override
class task{
}
It is compact:
interface emp
{
void disp();
}
public class AnonymousDemo
{
static emp getEmp() //static method
{
return new emp() //return , initialization
{
public void disp() //definition implement
{
System.out.println("in disp");
}
};
}
/*static emp getEmp()
{
emp e=new emp() //reference created
{
public void disp() //implement,initialization
{
System.out.println("in disp diff way");
}
};
return e; //return reference
}*/
public static void main(String args[])
{
emp e1=AnonymousDemo.getEmp();
e1.disp();
}
}
Day 9:
Cloning:
The clone() saves the extra processing task for creating exact same copy of an object. If we perform it by using
the new keyword, it will take a lot of processing to be performed that is why we use object cloning.
If the cost of creating a new object is large and creation is resource intensive, we clone the object. We use the
interface Cloneable and call clone() method to clone the object.
Java founders did not make Object class implements Cloneable. Had they
made Object class implement Cloneable , each and every class in Java
would have become Cloneable ( eligible for cloning). This is what they
( java founders) did not want. They wanted to give a choice to the
programmer. If programmer would like to clone the object , then he can
implement the class with Cloneable otherwise not.
If we object class implements clonable interface then all each and every classes
then become clonable.
now this class is derived from Object which has "protected Object
clone()" method. It means MyClass has the same method because it hasn't
overridden "clone()" method. Now think can client from some other
package invoke "clone()" method of MyClass?
answer is :
if client creates the child class of "MyClass" then client can
invoke "clone()" method through inheritance ( direct call) , but if
client creates object of MyClass and tries to invoke it is not
possible. Client will not always create child class of MyClass without
any valid reason.
In this case if developer would like clients to invoke "clone()" using
the reference of MyClass, he will override it with "public"
accessibility.
Hence to give a choice to a programmer (in our example developer)
whether or not his clone method should be invoked by client, "clone()"
method is protected in Object class.
import java.util.Arrays;
}
public class Test8
{
public static void main(String args[])
{
Center c1=new Center("Vita","mumbai",new int[]{100,200,300,400,500});
System.out.println(c1);
// c1.setName --- not possible
// c1.setAddress --- not possible //no hava a setters to
become immutable class members
// c1.setPrnnos --- not possible
int temp[]=c1.getPrnnos();
System.out.println("let's display all prnnos");
for(int i=0;i<temp.length;i++)
{
System.out.println(temp[i]);
}
temp[1]=0; // violation of Immutability you can make
changes in array so it becomes mutable.
System.out.println(c1);
}
}
With copy1:
package core1;
import java.util.Arrays;
}
public class Test8
{
public static void main(String args[])
{
Center c1=new Center("Vita","mumbai",new
int[]{100,200,300,400,500});
System.out.println(c1);
int temp[]=c1.getPrnnos();
System.out.println("let's display all prnnos");
for(int i=0;i<temp.length;i++)
{
System.out.println(temp[i]);
}
temp[1]=0; // No problem as we are modifying copy
System.out.println(c1);
}
}
With copy2:
package core1;
import java.util.Arrays;
}
public class Test8
{
public static void main(String args[])
{
Center c1=new Center("Vita","mumbai",new
int[]{100,200,300,400,500});
System.out.println(c1);
int temp[]=c1.getPrnnos();
System.out.println("let's display all prnnos");
for(int i=0;i<temp.length;i++)
{
System.out.println(temp[i]);
}
temp[1]=0; // No problem as we are modifying copy
System.out.println(c1);
}
}
Test 1:
package core;
class Engine
{
private int speed;
}
public class Test1
{
catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Done");
}
Test 2:
class Engine
{
private int speed;
@Override
public String toString() {
return "Engine [speed=" + speed + "]";
}
}
class Car implements Cloneable
{
private Engine ref;
private String name;
public Car(String name) {
super();
this.name = name;
this.ref=new Engine(100);
}
public Engine getRef() {
return ref;
}
public void setRef(Engine ref) {
this.ref = ref;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object clone()
{
Car ob=null;
try {
ob=(Car)super.clone(); //super.clone() call and
downcat that clone in car and put it in ob
}
catch (CloneNotSupportedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return ob;
}
@Override
public String toString() {
return "[name=" + name + " ref=" + ref + "]";
} //aapn dilel print honr address print honr use nhi kel tar
}
public class Test2
{
public static void main(String[] args)
{
Car c=new Car("Nano");
Car c1=(Car)c.clone();//downcasting and copy created
System.out.println(c);
System.out.println(c1);
c1.getRef().setSpeed(400); //set speed 400 to engine and
getRef() from car
System.out.println("After Modification");
System.out.println(c);
System.out.println(c1);
}
}
Test 3:
class Engine
{
private int speed;
@Override
public String toString() {
return "Engine [speed=" + speed + "]";
}
}
class Car implements Cloneable
{
private Engine ref;
private String name;
public Car(String name) {
super();
this.name = name;
this.ref=new Engine(100);
}
public Engine getRef() {
return ref;
}
public void setRef(Engine ref) {
this.ref = ref;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object clone()
{
Car ob=null;
try {
ob=(Car)super.clone();
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
ob.ref=new Engine(ref.getSpeed());//
return ob; //new engine created and deep copy copying and
speed changed
}
@Override
public String toString() {
return "[name=" + name + " ref=" + ref + "]";
}
}
public class Test3
{
public static void main(String[] args)
{
Car c=new Car("Nano");
Car c1=(Car)c.clone();
System.out.println(c);
System.out.println(c1);
c1.getRef().setSpeed(400);
System.out.println("After Modification");
System.out.println(c);
System.out.println(c1);
}
}
Immutablity
class base
{
private int num;
public base(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}
}
public class Demo1
{
public static void main(String args[])
{
base b1=new base(100);
System.out.println(b1.getNum());
}
}
// 2.If the instance fields include references to mutable objects,
don't allow those objects to be changed:
// Don't provide methods that modify the mutable objects.
class MyClass
{
private int data;
public MyClass(int data)
{
this.data=data;
}
public void setData(int data)
{
this.data=data;
}
public int getData()
{
return data;
}
}
class base
{
private int num;
private MyClass ref=new MyClass(25);
public base(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}
public void dispRef()
{
System.out.println(ref.getData());
}
public void changeRef() // do not provide this _do not write
{
ref.setData(50);
}
}
public class Demo2
{
public static void main(String args[])
{
base b1=new base(100);
System.out.println(b1.getNum());
b1.dispRef();
b1.changeRef(); // Violation of Immutability
b1.dispRef();
}
}
class MyClass
{
private int data;
public MyClass(int data)
{
this.data=data;
}
public void setData(int data)
{
this.data=data;
}
public int getData()
{
return data;
}
}
class base
{
private int num;
private MyClass ref=new MyClass(25);
public base(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}
public void dispRef()
{
System.out.println(ref.getData());
}
public MyClass getRef() // do not provide this
{
return ref;
}
}
public class Demo3
{
public static void main(String args[])
{
base b1=new base(100);
System.out.println(b1.getNum());
b1.dispRef();
MyClass ref=b1.getRef(); // Violation of Immutability
ref.setData(-3);
b1.dispRef();
}
}
class base
{
private int num;
public base(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}
}
class sub extends base
{
private int num;
public sub(int num)
{
super(0);
this.num=num;
}
public void setNum(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}
}
}
public static void main(String args[])
{
base ref1=getBase(100);
// invoke perform twice for "b1"
perform(ref1);
perform(ref1);
}
}
/*
to avoid above problem,
Ensure that the class can't be extended. This prevents careless or
malicious subclasses from compromising the immutable behavior of the
class by behaving as if the object's state has changed.
*/
The standard argument for making immutable classes final is that if you
don't do this, then subclasses can add mutability, thereby violating
the contract of the superclass. Clients of the class will assume
immutability, but will be surprised when something mutates out from
under them.
By declaring immutable classes final, we impose a guarantee that malicious subclasses capable of
changing the state of the object and violating assumptions that clients often make regarding
immutability, are not a concern.
Reflection can be used to get information about classes,interfaces,their constructors, and methods during
runtime i.e. on the fly.
Through reflection, we can invoke methods at runtime irrespective of the access specifier used with them.
Imp:
reflection does not show parent class methods if parent class is in
different package.
Demo 1:
import java.lang.reflect.Constructor; //java.lang open to all there
is reflect is class need to mention and constructor is method
import java.lang.reflect.Field;
import java.lang.reflect.Method;
for(int i=0;i<fields.length;i++)
{
System.out.println(fields[i]);
}
}
}
Reflection api
We create class and also there classes add in class Class and run on runtime
It is a technique where you get to know info about class that during runtime
Demo 5:3 and 4 are same only comments removed:
import java.lang.reflect.Field;
import java.lang.reflect.Method;
class Sample
{
private int num1=200;
public int num2=400;
public Sample()
{
System.setSecurityManager(new SecurityManager());
}
private void disp1()
{
System.out.println("in disp1");
}
public void disp2()
{
System.out.println("in disp2");
}
public int getNum1()
{
return num1;
}}
public class ReflectionDemo5
{
public static void main(String args[])
{
try
{
Class c=Class.forName("Sample");//new sample
class loaded in instance of class Class
System.out.println("Before modifying\t"+s.getNum1());
System.out.println("After modifying\t"+s.getNum1());
}
catch(Exception ee)
{
System.out.println(ee);
}}}
Demo 2:
import java.util.*;
class one
{
void disp1()
{
System.out.println("in disp1");
}
}
class two
{
void disp2()
{
System.out.println("in disp2");
}
}
class three
{
void disp3()
{
System.out.println("in disp3");
}
}
}
}
Day 10:
Invoking default and parameterized constructor:
// how to instantiate a class by invoking default constructor or
parameterized constructor with the help of Reflection API
package reflectionapi;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;//don’t know about
this
import java.lang.reflect.*;
class MyClass
{
private String message;
public MyClass() {
super();
}
@Override
public String toString() {
return "MyClass [message=" + message + "]";
}
}
public class Demo {
Object first=null;
try {
first=c.newInstance(); //load another instance of
myclass and store. //this time class not loaded bcoz already created
reference provide to
}
catch (InstantiationException | IllegalAccessException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
MyClass ref=null;
if(first instanceof MyClass)
{
ref=(MyClass)first;//load myclass by downcating
}
ref.setMessage("hello world");
System.out.println(ref.getMessage());
System.out.println("instantiate MyClass using parameterized
constructor");
Constructor ctor=null; //constructor class and instance is
ctor
try {
ctor=c.getDeclaredConstructor(String.class);
}//it will check constructor which will take string as a
argument
catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object second=null;
try {
second=ctor.newInstance("welcome");//constructor
created and add welcome
} catch (InstantiationException | IllegalAccessException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(second);
Bridge method:
import java.lang.reflect.*;
class base
{
Object disp()
{
System.out.println("in base disp");
return null;
}
}
class sub extends base
{
String disp()//compile don’t knows both object disp() is same so
it will create object disp()and from object disp call string disp()
automatically(implicitly)
{
System.out.println("in sub disp");
return null;
}
}
public class BridgeDemo
{
public static void main(String args[])
{
base ref=new sub();
ref.disp();
try
{
Class c=Class.forName("sub");
Method arr[]=c.getDeclaredMethods();
System.out.println(arr.length);
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
System.out.println(arr[i].isBridge());//is bridge is
used to ceheck the method is bridge or not byf=default is false first
time is false then it become true
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
Multi-Threading
Multi-tasking
a) process-based multi-tasking
b) thread-based multi-tasking
//only one work at a time but everyone think that it can do more than
one work at a time
process-based vs thread-based
Thread-Schedular
a) pre-emptive //do work of first and after complete it then second
b) time-slice. //provide time to each process
a) java.lang.Thread
b) java.lang.Runnable (interface)
c) java.lang.Object
Thread class:- this is the most imp. class required in order to create
multi-threading application.
Following are its methods.
a) start
is used to register thread with jvm schedular
b) run
is used by the programmer to define thread execution body, but
will be called by jvm schedular whenever it executes a particular
thread.
when the run method is over, thread is dead.
c) sleep (static)
is used to make thread sleep for some time
d) setName
to set the name of thread
e) getName
to get the name of thread
f) currentThread
returns the currently running thread
g) setPriority
to set the priority
in java priorities are numbers from 1 to 10
1 - minimum priority
5 - normal priority
10 - maximum priority
h) getPriority
to get the priority
i) join
join() method is used for waiting the thread in execution until
the thread on which join is called is not completed.
void run();
1) extends Thread
2) implements Runnable
e.g.
extends Thread
by def. every java application has main thread created by jvm. This
thread is used to execute main() function.
In the above code, there are 2 threads
main thread
user defined thread i.e. t1
Hence there are 2 call stacks in the above code. one for main() and
other for t1 ( run() method ).
when main function is over, main thread dies, but user defined thread/s
can continue. They will be taken care by JVM.
i.e. in the above code, after "t1.start()" when main() function is
over, main thread dies , but t1's execution will be managed by JVM.
{
System.out.println(Thread.currentThread());
Th2 t1=new Th2();//t1 inform shedular to call main
t1.setName("first");
t1.start();
}
}
class thread{
private runnable target;
public thread(Runnable target)//in implements we need to write ob bcoz
in upper class no def constructor only parameterized constructor
{
this.target=target;
}
above program also proves that threads can share the memory.
e.g.
synchronized keyword
method :- all the statements are protected.
in java every object has a lock. This lock can be accessed by only one
thread at a time. The lock will be released as soon as the thread
completes its job and thus another thread can acquire the lock.
This lock comes into picture only when object has got non-static
synchronized method/s or block.
whichever thread executes the synchronized method first, it acquires
the lock. Other thread/s have to be in "seeking lock state".
a) wait
it will make thread , release the lock and go to wait pool.//wait
kr bolto first la ani baher jaun ye
b) notify
it will make the thread to move from wait pool to seeking lock
state. //line mdhe hubar baherun yeun
c) notifyAll
it will make all the threads to move from wait pool to seeking
lock state. //sagle baherun yaa ni line mdhe hubara
Transection code:
-using interface reference all the methods of object class can be
invoked it is because interface will be implemented by some or the
other class and every class is derived from object class.
}
}
public void run() //we cant apply static to run bcoz run is
overriding
{
disp1();
}
public static void main(String args[])
{
ClassLock1 c=new ClassLock1();
ClassLock1 c1=new ClassLock1();
Thread t1=new Thread(c);
Thread t2=new Thread(c1);
t1.start();
t2.start();
}}
ClassLock2:
ClassLock 2a:
public class ClassLock2_a implements Runnable
{
static void disp1()
{
synchronized(ClassLock2_a.class)//reference of class class
instance which aquire the lock
{
for(int i=0;i<5;i++)
{
System.out.println("static "+i);
}
}
}
public void run()
{
disp1();
}
public static void main(String args[])
{
ClassLock2_a c=new ClassLock2_a();
ClassLock2_a c1=new ClassLock2_a();
Thread t1=new Thread(c);
Thread t2=new Thread(c1);
t1.start();
t2.start();
}
}
ClassLock3:
In case of Class lock, When u say cs.wait() cs.nofify() or cs.notifyAll() what does it mean ?
It means that, wait(), notify() and notifyAll() are in Object class and they are final , so they cannot be
overridden.
Class class is a child of Object class.
When we say cs.wait(),cs.nofify() or cs.notifyAll() it means that these methods are inherited in Class
class which we are invoking.
//object lock-wait notify notifyall in object class the lock on any object derived from object class
Classlock-wait notify notifyall in object class derived in instance of class class
public class Th9 implements Runnable
{
public void run()
{
synchronized(this)
{
for(int i=0;i<5;i++)
{
System.out.println("Hello"+i);
}
}
}
public static void main(String args[])
{
Th9 ob=new Th9();
Thread t1=new Thread(ob);
Thread t2=new Thread(ob);
t1.start();
t2.start();
in the above code "Both the threads are over" will not be displayed in
the end because it is a statement of main. It is because as we know ,
main thread completes first and user defined thread are continue, they
are taken care by JVM.
if we want that "Both the threads are over" should be displayed at the
end, we have to make sure that main thread will complete only after the
completion of "t1" and "t2".
join() method
join method makes caller thread (main thread) to wait for called thread
(t1 and t2) to complete.
in the above code, when main() function calls "t1.join()" for example,
it says "join me at your end".
Since main() is calling "t1.join()" and "t2.join()" , it is added to
the end of both t1 and t2. That's why now the statement "Both the
threads are over" is getting executed at the end.
Thread states
Born
runnable
running
blocked
dead
User threads
user defined threads
main thread
Daemon thread
e.g. garbage collection thread
Daemon threads are the threads which are at the mercy of user thread/s.
Their only purpose is to serve user defined thread/s. When there is no
user thread alive, Daemon thread will die.
//Runtime.getRuntime().gc();
System.out.println("Main function executed
By\t"+Thread.currentThread().getName());
}
output:
inside finalized method
finalize method invoked by
Thread[Finalizer,8,system](name,priority,group)
true
Main function executed By main
Day 11:
Difference betn implicit and explicit lock
1) acquiring and releasing lock needs to be done explicitly.
2) extended feature like fairness, which can be used to provide lock to
longest waiting thread.
3) ability to trying for lock with or without timeout. Thread doesn’t
need to block infinitely, which was the case with implicit
synchronization.
Background
Server Programs such as database and web servers repeatedly execute requests from multiple clients
and these are oriented around processing a large number of short tasks. An approach for building a
server application would be to create a new thread each time a request arrives and service this new
request in the newly created thread. While this approach seems simple to implement, it has significant
disadvantages. A server that creates a new thread for every request would spend more time and
consume more system resources in creating and destroying threads than processing actual requests.
Since active threads consume system resources, a JVM creating too many threads at the same time
can cause the system to run out of memory. This needs to limit the number of threads being created.
Using threadpool
When my task is completed manager provide second task
Under light to moderate load, thread per task approach is an improvement over sequential execution.
As long as the request arrival rate does not exceed the server’s capacity to handle the requests, this
approach offers better responsiveness.
Disadvantage of unbounded thread creation
For production use, however, the thread-per-task approach has some practical drawbacks, especially
when a large number of threads may be created.
Thread lifecycle overhead:- Thread creation and teardown are not free. The actual overhead varies
across platforms, but thread creation takes time, introducing lethargy into request processing, and
requires some processing activity by JVM and OS. If requests are frequent and lightweight, as in most
server applications, creating a new thread for each request can consume significant computing
resources.
Resource consumption:- Active threads consume system resources, especially memory. When there
are more runnable threads than available processors, threads sit idle. Having many idle threads can tie
up a lot of memory and having many threads competing for the CPUs can impose other performance
costs as well.
Executors
If your program creates a large number of short-lived threads, then it should instead use a thread pool.
A thread pool contains a number of idle threads that are ready to run. You give a Runnable to the pool
, and one of the threads calls the run method. When the run method exits, the thread doesn’t die but
stays around to serve the next request.
Executors simplify concurrent programming by managing Thread objects for you. Executors allow
you to manage the execution of asynchronous tasks without having to explicitly manage the lifecycle
of threads.
We can use an Executor instead of explicitly creating Thread objects . An ExecutorService ( an
Executor with a service lifecycle – e.g. shutdown ) knows how to build the appropriate context to
execute Runnable objects.
An Executor implementation is likely to create threads for processing tasks. But the JVM can’t exit
until all the threads have terminated, so failing to shutdown an Executor could prevent the JVM from
exiting. Since Executors provide a service to the applications, they should be able to shutdown as
well.
The call to shutdown() prevents new tasks from being submitted to that Executor. The current thread
( e.g. main thread ) will continue to run all tasks submitted before shutdown() was called. The
program will exit as soon as all the tasks in the Executor finish.
Executors Factory Methods
a) newCachedThreadPool :- a cached thread pool has more flexibility to reap idle threads
when the current size of the pool exceeds the demand for processing, and to add new
threads when demand increases, but places no bound on the size of the pool.
Executor demo1;
import java.util.concurrent.*;
class myapp implements Runnable
{
public void run()
{
perform();
}
void perform()
{
for(int i=0;i<5;i++) //5 tasks only three threads work on it
{
System.out.println("Hello\t"+I+"\t"+Thread.currentThread());
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
}
public class ExecutorDemo1
{
public static void main(String args[])
{
ExecutorService
/*
shutdown() prevents new tasks from being submitted to that Executor.
The current thread ( e.g. main thread ) will continue to run all tasks
submitted before shutdown() was called.
*/
exec.shutdown(); // if u don't invoke, then jvm will not
shutdown
System.out.println("done");
// exec.execute(new myapp()); don't give any task after
shutdown here rejectedExecutionException perform
}
}
Executor demo2:
/*
a FixedThreadPool uses a limited set of threads to execute the
submitted tasks.
*/
import java.util.concurrent.*;
class myapp implements Runnable
{
public void run()
{
perform();
}
void perform()
{
for(int i=0;i<5;i++)
{
System.out.println("Hello\t"+i+"\t"+Thread.currentThread());
}
}
}
public class ExecutorDemo2
{
public static void main(String args[])
{
ExecutorService exec=Executors.newFixedThreadPool(2);//2
THREAD fixed
for(int i=0;i<3;i++)
{
exec.execute(new myapp()); //THREE TASK CREATED
}
System.out.println("After submitting tasks");
+"\t"+Thread.currentThread());
}
}
Though ReentrantLock provides same visibility and orderings guaranteed as implicit lock, acquired
by synchronized keyword in Java, it provides more functionality and differ in certain aspect. main
difference between synchronized and ReentrantLock is ability to trying for lock with or without
timeout. Thread doesn’t need to block infinitely, which was the case with synchronized. Let’s see few
more differences between synchronized and Lock in Java.
In explicit lock thread check again and again is this first thread work is completed
Similarly tryLock() with timeout can be used to timeout if lock is not available in certain time period.
3) ReentrantLock also provides convenient method to get List of all threads waiting for lock.
In short, Lock interface adds lot of power and flexibility and allows some control over lock
acquisition process, which can be influenced to write highly scalable systems in Java.
Await
Signal
Signal all (condition interface available)
Major drawback of using ReentrantLock in Java is , wrapping method body inside try-finally
block, which makes code unreadable and error-prone. Another disadvantage is that, now programmer
is responsible for acquiring and releasing lock, which is a power but also opens gate for new subtle
bugs, when programmer forget to release the lock in finally block.
Things to remember:
Demo1:
import java.util.concurrent.locks.*;
public class Demo1 implements Runnable
{
ReentrantLock mylock=new ReentrantLock();//reertrantlock is class
public void run()
{
mylock.lock(); //lock
for(int i=0;i<5;i++)
{
System.out.println("Hello"+i);
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
mylock.unlock(); //unlock
}
public static void main(String args[])
{
Demo1 ob=new Demo1();
Thread t1=new Thread(ob,"first");
Thread t2=new Thread(ob,"second");
t1.start();
t2.start();
}
}
Demo1A:
// combination of Thread pool and Explicit locking
import java.util.concurrent.locks.*;
import java.util.concurrent.*;
public class Demo1_a implements Runnable
{
ReentrantLock mylock=new ReentrantLock();//we use import
java.util.concurrent.locks
public void run()
{
mylock.lock();
for(int i=0;i<5;i++)
{
System.out.println("Hello"+i);
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
mylock.unlock();
}
public static void main(String args[])
{
Demo1 ob=new Demo1();
ExecutorService exec=Executors.newFixedThreadPool(2);
exec.execute(ob);
exec.execute(ob);
exec.shutdown();
}
}
Demo 2:
// class lock with ReentrantLock
import java.util.concurrent.locks.*;
public class Demo2 implements Runnable
{
static ReentrantLock mylock=new ReentrantLock(); //we lock the
instance of class Class
static void disp()
{
mylock.lock();
for(int i=0;i<5;i++)
{
System.out.println("Hello"+i);
}
mylock.unlock();
}
public void run()
{
disp();
}
public static void main(String args[])
{
Demo2 ob1=new Demo2();
Demo2 ob2=new Demo2();
Thread t1=new Thread(ob1);
Thread t2=new Thread(ob2);
t1.start();
t2.start();
}
}
Demo 3:
import java.util.concurrent.locks.*;
public class TryLockDemo1 implements Runnable
{
ReentrantLock mylock=new ReentrantLock();
public void run()
{
while(true)
{
if(mylock.tryLock()) //second obj check again and again
{
for(int i=0;i<8;i++)
{
System.out.println("Hello"+i+"\t"+Thread.currentThread().getName(
));
}
mylock.unlock();
break;
}
else
{
System.out.println(Thread.currentThread().getName()+"
doing something else");
}
}
}
public static void main(String args[])
{
TryLockDemo1 ob=new TryLockDemo1();
Thread t1=new Thread(ob,"first");
Thread t2=new Thread(ob,"second");
t1.start();
t2.start();
}
}
Op:
Hello 0 first
second doing something else
second doing something else
Hello 1 first
second doing something else
Hello 2 first
second doing something else
Hello 3 first
second doing something else
Hello 4 first
second doing something else
Hello 5 first
second doing something else
Hello 6 first
second doing something else
Hello 7 first
second doing something else
Hello 0 second
Hello 1 second
Hello 2 second
Hello 3 second
Hello 4 second
Hello 5 second
Hello 6 second
Hello 7 second
Demo 2:
import java.util.concurrent.locks.*;
import java.util.concurrent.*;
public class TryLockDemo2 implements Runnable
{
ReentrantLock mylock=new ReentrantLock();
public void run()
{
while(true)
{
try
{
if(mylock.tryLock(2,TimeUnit.MILLISECONDS))//2 milisecons
is constant timeunit is enum
{
for(int i=0;i<18;i++)
{
System.out.println("Hello"+i+"\t"+Thread.currentThread().getName(
));
}
mylock.unlock();
break;
}
else
{
System.out.println(Thread.currentThread().getName()+"
doing something else");
}
}
catch(InterruptedException ie)
{
System.out.println("in catch\t"+ie);
}
}
}
public static void main(String args[])
{
TryLockDemo2 ob=new TryLockDemo2();
Thread t1=new Thread(ob,"first");
Thread t2=new Thread(ob,"second");
t1.start();
t2.start();
}
}
Day 12:
stream
byte streams :- for reading and writing bytes , image or sound files
also. It is also used to read and write java Objects.
For inputputstream
For outputstream
import java.io.*;
import java.util.*;
public class FileDemo
{
public static void main(String args[])
{
File f=new File("c:\\temp\\FileDemo.java");
System.out.println("File Name\t"+f.getName());
System.out.println("Path\t"+f.getPath());
System.out.println("Abs Path\t"+f.getAbsolutePath());
System.out.println(f.exists()?"Exists":"Doesn't Exists");
System.out.println(f.canWrite()?"Can Write":"Can Not
Write");
System.out.println(f.canRead()?"Can Read":"Can Not Read");
System.out.println(f.isDirectory()?"It is Directory":"It is
not Directory");
System.out.println(f.isFile()?"Yes File":"No File");
System.out.println(f.isAbsolute()?"is Absolute":"It is Not
Absolute");
System.out.println("File Last Modified\t"+new
Date(f.lastModified()));
System.out.println("File size is\t"+f.length());
}
}
First
package java8;
import java.io.*;
public class First
{
public static void main(String args[])
{
File f=new File("d:\\FileDemo.java\\fh1.txt");
if(!f.exists())
{
System.out.println("file does not exists");
System.exit(0);
}
try(FileInputStream fis=new FileInputStream(f))
{
byte b[]=new byte[((int)f.length())]; //bcoz bydefault
bytecode is long so we typecast
fis.read(b); //for read b
String ss=new String(b);
System.out.println(ss); //we need to convert it in string
to read
}
catch(Exception ee)
{
ee.printStackTrace();
}
}}
Second
package java8;
import java.io.*;
public class Second
{
public static void main(String args[])
{
try(FileOutputStream fos=new
FileOutputStream("d:\\abc1.txt",true))
//by default it is false so te override hot raht prt write vprl ki
mhnje lihlel ahe tyarvr prt ekda liht n phil ghlvt when true used
(append works) and te ky krt ki next line la enter krt jat
{
byte b[]=new byte[100]; //in that we read
System.out.println("Enter data");
int k=System.in.read(b);
fos.write(b,0,k); //by using this we write(array,0psun
start,k input+2 ( /n/r))
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
Third:
import java.io.*;
public class Third
{
public static void main(String args[])
{
char arr1[]=null;
try(FileWriter fw=new FileWriter("e:\\abc2.txt"))
{
char arr[]={'a','b','c','d','e'}; //unicode
fw.write(arr);
}
catch(IOException ie)
{
ie.printStackTrace();
}
try(FileReader fr=new FileReader("e:\\abc2.txt"))
{
for(int i=0;i<arr1.length;i++)
{
System.out.println(arr1[i]);
}
}
}
Fourth:
import java.io.*;
// Program to write and read primitives in file
/* DataOutputStream lets an application write primitive Java data types
to an output stream in a portable way. An application can then use a
data input stream to read the data back in. */
//we use datainput/dataoutput stream for read and write primitive bcoz
inputstram do not knows hot to read write stream
sixth
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.io.*;
//Program to write and read in a file using RandomAccessFile
//class
public class Sixth
{
public static void main(String args[])
{
try(RandomAccessFile rf=new
RandomAccessFile("e:\\temp\\xy.txt","rw"))
{
rf.seek(rf.length()); //start from
System.out.println("Enter data");
byte b[]=new byte[200];
int k=System.in.read(b);
rf.write(b,0,k);
rf.seek(0); //start from 0
byte c[]=new byte[(int)rf.length()];
rf.read(c);
String ss=new String(c);
System.out.println(ss);
rf.seek(rf.length()); //start from after entered
length
System.out.println("Enter data again");
k=System.in.read(b);
rf.write(b,0,k);
System.out.println("Finally reading all data");
rf.seek(0);
c=new byte[(int)rf.length()];
rf.read(c);
ss=new String(c);
System.out.println(ss);
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
1) File f=new File("d:\\FileDemo.java");
here we just create a File class object just to check wheather
file exists or not, since we r reading from file ,if file doesn't exist
we would like to exit.
if(!f.exists())
{
System.out.println("file does not exists");
System.exit(0);
}
fis.read(b);
here we read the contents of file inside byte array.
System.out.println(ss);
here we display the contents of file.
2)
FileOutputStream fos=new FileOutputStream("d:\\abc1.txt",true)
above statement will create a file if file does not exist.
argument "true" inside FileOutputStream constructor indicates
that if file exists then it will get appended.
int k=System.in.read(b);
we accept input from user inside "b" which is byte array.
here "k" will hold number of bytes read.
fos.write(b,0,k);
here we write user input stored inside byte array to file.
write has 3 arguments:
1) byte array to be written
2) it should start with 0
3) how many exact no. of bytes we would like to write inside
file.
object persistence
There are five total ways to create objects in Java, which are explained below with their
examples followed by bytecode of the line which is creating the object.
To use clone() method on an object we need to implement Cloneable and define the clone()
method in it.
Java cloning is the most debatable topic in Java community and it surely does have its
drawbacks but it is still the most popular and easy way of creating a copy of any object until
that object is full filling mandatory conditions of Java cloning.
5. Using deserialization:
Whenever we serialize and deserialize an object, the JVM creates a separate object for us. In
deserialization, the JVM doesn’t use any constructor to create the object.
To deserialize an object we need to implement a Serializable interface in our class.
Example
Let’s consider an Employee class for which we are going to create the objects:
First:
import java.io.*;
public class First implements Serializable //need to implement
serilizable
{
String name="sachin";
int age=20;
//using serilize we print above instance using object
transient Thread t=new Thread(); //ignore it
public static void main(String args[])
{
First s=new First();
try(FileOutputStream fos=new
FileOutputStream("e:\\ab1.txt"))
{
try(ObjectOutputStream oos=new
ObjectOutputStream(fos))
//he knows how to write but don’t know where to write
{
oos.writeObject(s); //Serialization
}
}
catch(Exception ee)//if you don’t follow serilization
rules you got NotSerlizedException
{
ee.printStackTrace();
}
System.out.println(s.name+"\t"+s.age+"\t"+s.t);
s=null;
Second:
import static java.lang.System.*;
import java.io.*;
class base //implements Serializable
{
int num1=30;
base()
{
out.println("base const");
}
}
class sub extends base implements Serializable
{
int num2=60;
sub()
{
out.println("sub const");
}
}
public class Second
{
public static void main(String args[])throws Exception
{
sub s=new sub();
s.num1=100;//override 100 in base
s.num2=200;//override 200 in sub
out.println("Before Saving\t"+s.num1+"\t"+s.num2);
FileOutputStream fos=new FileOutputStream("my.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(s);
oos.close();
s=null;
FileInputStream fis=new FileInputStream("my.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
sub ref=(sub)ois.readObject();//downcasting
out.println("After Retrieving\t"+ref.num1+"\t"+ref.num2);
}
}
Before saving 100 200
After retriving(parat object bnvto) 30 200//bcoz only child class
have serialized
Example 3
import java.io.*;
class base implements Serializable
{
public base()
{
System.out.println("base def");
}
int num1=30;
}
class sub extends base
{
public sub()
{
System.out.println("sub def");
}
int num2=40;
}
public class Third
{
public static void main(String args[])
{
sub s1=new sub();
s1.num1=60;
s1.num2=70;
System.out.println(s1.num1+"\t"+s1.num2);
try(FileOutputStream fos=new
FileOutputStream("a.txt"))
{
try(ObjectOutputStream oos=new
ObjectOutputStream(fos))
{
oos.writeObject(s1);
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
/**
*
*/
/**
* @author Sriram
*
*/
public class Student implements Serializable
{
public Student()
{
System.out.println("inside Student default constructor");
}
static
{
System.out.println("inside Student class static block");
}
StudentDemo:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import pack1.Student;
try {
fos=new FileOutputStream("e:\\temp.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos=new ObjectOutputStream(fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos.writeObject(s1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Client code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import pack1.Student;
try {
fis=new FileInputStream("D:\\forclient\\temp.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ois=new ObjectInputStream(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ref=(Student) ois.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Student info\t"+ref);
}
}
Why we use externizable:
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used
during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are
compatible with respect to serialization. If the receiver has loaded a class for the object that has a
different serialVersionUID than that of the corresponding sender's class, then deserialization will result in
an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field
named serialVersionUID that must be static, final, and of type long:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a
default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object
Serialization Specification. However, it is strongly recommended that all serializable classes explicitly
declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may
vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during
deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations,
a serializable class must declare an explicit serialVersionUID value.
SerialversionUID:
As per java docs, during serialization, runtime associates with each serializable class a version number, called a
serialVersionUID, which is used during de-serialization to verify that the sender and receiver of a serialized object have
loaded classes for that object that are compatible with respect to serialization.
Simply put, the serialVersionUID is a unique identifier for Serializable classes. This is used during the deserialization of an
object, to ensure that a loaded class is compatible with the serialized object. If no matching class is found, an
InvalidClassException is thrown.
serialization mechanism generates serialversionuid (if you have not defined it explicitly) for the class , of which
object you are trying to serialize. This serialversionuid is then stored inside the file.
during deserialization the class of which object you want to deserialize , needs to be loaded.
Here, deserialization mechanism generates serialversionuid (if you have not defined it explicitly) for the class , of
which object you are trying to deserialize. This serialversionuid is then checked with the serialversionuid stored inside file. if
they match then deserialization becomes successful else you get an exception "InvalidClassException" and deserialization
fails.
Since the default serialVersionUID computation differs on different JVM implementations, it is highly recommended for a
class which implements Serializable interface to declare serialversionuid explicitly in order to ensure successful
deserialization across all the platforms.
s.store(100);
Integer ref1=(Integer)s.get();
now here compiler will not give any error as this statement is
syntactically correct. But during runtime "String" will be returned
which we are trying to cast with "Integer" and this will result into
"ClassCastException".
Generic 1
import static java.lang.System.*;
public class Generic1<T>//conventional type parameter
{
private T first; //private object first
void setVal(T first) //(object first)
{
this.first=first;
}
T getVal() //objext getVal
{
return first;
}
public static void main(String args[])
{
Generic1<String> g1=new Generic1<String>();
g1.setVal("Hello Generic");
out.println(g1.getVal());
}
}
Jvm don’t know the generic so compiler convert it to object by using
type earaser
Generic2.fun();
}
}
Generic 3:
import static java.lang.System.*;
import java.util.*;
class shape
{
shape draw()
{
return this;
}
}
class triangle extends shape //this extends is inheritance vala
{
shape draw()
{
return this;
}
}
class rect extends shape
{
shape draw()
{
return this;
}
}
class common<T extends shape> // this extends is different -check t is
type of shape
{
T ob;
public common(T ob)
{
this.ob=ob;
}
public T fun()
{
return ob;
}
}
public class Generic3
{
}
}
Generic 3a:
import static java.lang.System.*;
interface shape
{
void draw();
}
class triangle implements shape
{
public void draw()
{
out.println("in draw of triangle");
}
}
class rect implements shape
{
public void draw()
{
out.println("in draw of rect");
}
}
class common<T extends shape> //t is of type of shape
{
T ob;
public common(T ob)
{
this.ob=ob;
}
public void fun()
{
ob.draw();
}
}
public class Generic3a
{
Generic 4:
class a{}
class b extends a{}
class MyClass
{
<T>void accept(T t) //method generic banadiya//void accept(Object
t)
{
System.out.println(t);
}
public<T extends a>void disp(T t) //public void disp(a t)a is
parent <a,b is of type a>every child Is type of parent
{
System.out.println(t);
}
}
public class Generic4
{
public static void main(String args[])
{
MyClass m1=new MyClass();
m1.accept(20);
m1.accept(m1);
Generic4 tr=new Generic4(); //provide any name to cllass
m1.accept(tr);
b ob=new b();
m1.disp(ob);
// m1.disp(tr); Error
}
}
Generic 5:
class a{}
class b extends a{}
class MyClass
{
<T>void accept(T t)
{
System.out.println(t);
}
public<T extends a, K> M disp(T t,B ref) //K is another generic
//and M is ref of object
//(a t,Object ref)
{
System.out.println(t);
return ref;
}
}
public class Generic4
{
public static void main(String args[])
{
MyClass m1=new MyClass();
m1.accept(20);
m1.accept(m1);
Generic4 tr=new Generic4();
m1.accept(tr);
b ob=new b();
m1.disp(ob,tr);
// m1.disp(tr); Error
}
}
Type Eraser:
public class Generic1<T>
{
private T first;
void setVal(T first)
{
this.first=first;
}
T getVal()
{
return first;
}
when u compile above class, compiler will remove all the generic
information because JVM can't understand Generics. This is known as
"Type Erasure". So after compilation the above class will be as
follows:
Collection Api:
Iterator
allows u to traverse through containers.
Algorithms
sort,count,max,min etc.
Entire support for Collection API has been given inside "java.util"
package.
List
index based, duplicates allowed
Set
unique, sometimes sorted
Map
key - value pair
put using key and value
get using only key
ArrayList
good for traversal,random access
Vector
slow, thread-safe, legacy class
LinkedList
doubly linked list, good for insertion and deletion from middle.
HashSet
fast, order not guaranteed
TreeSet
slow, sorted
HashMap
fastest map implementation
TreeMap
slow, sorted keys
Hashtable
slow,thread-safe,legacy class
Demo1:
import java.util.*;
import static java.lang.System.*;
public class ArrayListDemo
{
public static void main(String args[])
{
List <String>a1=new ArrayList<String>();//list is parent of
arraylist array list is a implementation of List(interface)
out.println("Initial size of a1: "+a1.size());//
a1.add("c");//add is method of List interface
a1.add("a");
a1.add("e");
a1.add("b");
a1.add("d");
a1.add("f");
a1.add(1,"a2"); //1 position pe add a2
out.println("After adding size of a1: "+a1.size());
out.println("Contents of a1: "+a1);
a1.remove("f"); //in that remove f
a1.remove(2); //in that 2nd pos element is removed
out.println("After removing size of a1: "+a1.size());
out.println("Contents of a1: "+a1);
}
import java.util.*;
import static java.lang.System.*;
public class ArrayListDemo1
{
public static void main(String args[])
{
List <Integer>a1=new ArrayList<Integer>();
out.println("Initial size of a1: "+a1.size());
a1.add(5);
a1.add(10);
a1.add(15);
a1.add(20);
//a1.add(2);
a1.add(30);
out.println("After adding size of a1: "+a1.size());
out.println("Contents of a1: "+a1);
//a1.remove(2); // remove element at index 2
a1.remove(new Integer(2)); // remove element "2"
out.println("After removing size of a1: "+a1.size());
out.println("Contents of a1: "+a1);
}
}
Linkedlist:
import java.util.*;
import static java.lang.System.*;
public class LinkedListDemo
{
public static void main(String args[])
{
List <String>l=new LinkedList<String>();
l.add("f");
l.add("b");
l.add("d");
l.add("e");
l.add("c");
((LinkedList)l).addLast("z");//add last and add first is
methods of Linkedlist we do downcasting
((LinkedList)l).addFirst("a");
l.add(1,"a2");
out.println("Original contents of l "+l);
l.remove("f");
l.remove(2);
out.println("Contents after deletion "+l);
((LinkedList)l).removeFirst();
((LinkedList)l).removeLast();
out.println("contents after deleting first and last "+l);
String val=l.get(2);
out.println("element at second index is "+val);
}
}
Iterator:
Demo 1:
import java.util.*;
public class IteratorDemo
{
public static void main(String args[])
{
List<String> mylist=new ArrayList<String>();
mylist.add("hello");
mylist.add("welcome");
mylist.add("all the best");
// now let's create iterator
//Iterator-interface
//.iterator-method
while(itr.hasNext())
{
System.out.println(itr.next());
}
}}
/* "hasNext()" method checks whether there are elements inside list
for traversal. it returns true or false. */ check krt ki element
ahet ka list mdhe
/* "next()" method returns the current element and places the record
pointer on the next element */ //return current and next la jato
Iterator itr=a1.iterator();
here "iterator()" is a method of ArrayList which returns the
child class of
Iterator interface.
while(itr.hasNext())
// hasNext() returns boolean i.e. it returns true as long as
elements are there in ArrayList for traversal.
{
S.o.p(itr.next());
next() method will return the current element and place the
record pointer on next element.
}
/*
The Arrays class in java.util package is a part of the Java Collection
Framework. This class provides static methods to dynamically create and
access Java arrays.
*/
import java.util.Arrays;
import java.util.List;
Vector
ArrayList
LinkedList
CopyOnWriteArrayList
Different between Vector,ArrayList and LinkedList is quite clear but difference between ArrayList and
CopyOnWriteArrayList doesn’t seems to be clear at first glance. Here I am trying to explain between difference between
ArrayList and CopyOnWriteArrayList , with one simple example.As we all know ArrayList is not thread safe so any
simultaneous thread can access and modify the content of list simultaneously.Here lies the dangerous,
ConcurrentModificationException. When one thread is Iterating over an ArrayList and any other thread(which may be the
same thread) modify the content of list and when we again call next() on the iterator ,we get this exception. Which means
that no thread can modify the ArrayList while an Iterator is iterating over this. We can solve this by surrounding the code that
try to modify this list in a synchronized block with some lock so that the thread trying to modify the ArrayList wait for the
iterator to complete.
This seems simple solution but not a efficient one where there are many threads iteration over an ArrayList because each
thread have to wait for a considerable time.
Another possible solution could be to use CopyOnWriteArrayList instead of ArrayList. This is same as ArrayList with one
difference. All operations that change the contents of aCopyOnWriteArrayList collection cause the underlying array to be
replaced with a copy of itself before the contents of the array are changed. Any active iterators will continue to see the
unmodified array, so there is no need for locks. Iterators created by a CopyOnWriteArrayList object cannot change the
underlying array. Though these iterators do have methods for changing the underlying collection, their implementations
throw an UnsupportedOperationException instead of modifying the underlying collection.
Demo 1:
import java.util.*;
import java.util.concurrent.*;
public class First
{
public static void main(String args[])
{
List<String> list= new ArrayList<String>();
list.add("vivek");
list.add("kumar");
Iterator i =list.iterator();
while(i.hasNext())
{
System.out.println(i.next());
//list.add("abhishek"); // ConcurrentModificationException
bcoz it is ArrayList
//iteration veli add hot nahi
}
}
}
Demo 2:
import java.util.*;
import java.util.concurrent.*;
public class Second
{
public static void main(String args[])
{
List<String> list= new CopyOnWriteArrayList<String>();
list.add("vivek");
list.add("kumar");
System.out.println("Before modification\t"+list);
Iterator i =list.iterator();
while(i.hasNext())
{
System.out.println(i.next());
list.add("abhishek");//now showing correct
//i.remove();// UnsupportedOperationException
}
System.out.println("after modification\t"+list);
System.out.println("After modification:");
Iterator i2 =list.iterator();
while(i2.hasNext())
{System.out.println(i2.next());
}}}
Demo 3:
We does not do remove in CopyOnWriteArrayList
Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is
the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table
is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of
buckets.
Concurrent hashmap:
-hashtable complete synchronized
-slow
put operation
subsequent entries
Get operation
hashcode -
bucket is determined for search
==
- true - get the value
false
equals - true - get the value
false - linked list will be traverse and
subsequently == and equals are invoked.
Hashmapbasic working:
import java.util.HashMap;
import java.util.Map;
System.out.println(name);
name=mymap.get(40); //we get null
System.out.println(name);
mymap.put(10,"Reena");
System.out.println("Map is\t"+mymap);
}
Hashmapdemo1:
package datatypes_pro;
return empid+"\t"+deptcode+"\t"+citycode;
}
}
public class HashMapDemo1
{
public static void main(String args[])
{
Employee e1=new Employee("e001",1,10);
Employee e2=new Employee("e002",1,12);
Employee e3=new Employee("e003",2,10);
Employee e4=new Employee("e004",1,13);
String val=map.get(e2);
System.out.println(val);}}
Check flow in ppt at address:
D:\java\Day_13.zip\Day_13\Generics_and_Collection\Collection_API\Map_Im
plementations on that address
Demo1:
//package datatypes_pro;
return empid+"\t"+deptcode+"\t"+citycode;
}
}
public class HashMapDemo1_a
{
public static void main(String args[])
{
Employee e1=new Employee("e001",1,10);
Employee e2=new Employee("e002",1,12);
Employee e3=new Employee("e003",2,10);
Employee e4=new Employee("e004",1,13);
Employee e5=new Employee("e005",1,12); //In that overwrite
in position of whose value is same
Employee e6=e3;
String val=map.get(e2);
System.out.println(val);
val=map.get(e3);
System.out.println(val);
}
}
Tree Map:
1)
import java.util.Map;
import java.util.TreeMap;
ref1.put(p1, 100);
ref1.put(p2, 200);
System.out.println(ref1);
}
}
Comparable:
3)
import java.util.*;
class Person implements Comparable<Person>
{
private String name;
private int age;
public Person(String name,int age)
{
this.name = name;
this.age=age;
}
public int compareTo(Person ref)//solution fir previous code
//method of comparable interface //if same both then 0 if first less
then negative if some is higher then positive
{
return name.compareTo(ref.name);//string vala comapreTo
}
public String toString()
{
return "["+name+"\t"+age+"]";
}
}
public class TreeMapDemo3
{
public static void main(String[] args)
{
Person p1=new Person("abc",20);
Person p2=new Person("xyz",17);
TreeMap<Person,Integer>ref1=new TreeMap<Person,Integer>();
ref1.put(p1, 100);
ref1.put(p2, 200);
System.out.println(ref1);
}
}
4)
import java.util.*;
class Person implements Comparable<Person>
{
private String name;
private int age;
public Person(String name,int age)
{
this.name = name;
this.age=age;
}
public int compareTo(Person ref)
{
if(age>ref.age)
{
return 1;
}
else if(age<ref.age)
{
return -1;
}
else
{
return 0;
}
}
public String toString()
{
return "["+name+"\t"+age+"]";
}
}
public class TreeMapDemo4
{
public static void main(String[] args)
{
Person p1=new Person("abc",20);
Person p2=new Person("xyz",17);
TreeMap<Person,Integer>ref1=new TreeMap<Person,Integer>();
ref1.put(p1, 100);
ref1.put(p2, 200);
System.out.println(ref1);
}
}
comparator
//comparable demo have drawbacks of only one key is sorted
//and maintainance drawback n krnya sthi
//so for that comparator interface
import java.util.*;
class Person
{
private String name;
private int age;
public Person(String name,int age)
{
this.name = name;
this.age=age;
}
public String toString()
{
return "["+name+"\t"+age+"]";
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class NameComparator implements Comparator<Person>
{
public int compare(Person ref1,Person ref2)
{
return ref1.getName().compareTo(ref2.getName());
}
}
public class TreeMapDemo5
{
public static void main(String[] args)
{
Person p1=new Person("abc",20);
Person p2=new Person("xyz",17);
TreeMap<Person,Integer>ref1=new TreeMap<Person,Integer>(new
NameComparator());
ref1.put(p1, 100);
ref1.put(p2, 200);
System.out.println(ref1);
}
}
Treemap 6:
import java.util.*;
class Person
{
private String name;
private int age;
public Person(String name,int age)
{
this.name = name;
this.age=age;
}
public String toString()
{
return "["+name+"\t"+age+"]";
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
class NameComparator implements Comparator<Person>
{
public int compare(Person ref1,Person ref2)
{
return ref1.getName().compareTo(ref2.getName());
}
}
class AgeComparator implements Comparator<Person>
{
public int compare(Person ref1,Person ref2)
{
if(ref1.getAge()>ref2.getAge())
{
return 1;
}
else if(ref1.getAge()<ref2.getAge())
{
return -1;
}
else
{
return 0;
}
}
}
public class TreeMapDemo6
{
public static void main(String[] args)
{
Person p1=new Person("abc",20);
Person p2=new Person("xyz",17);
TreeMap<Person,Integer>ref1=new TreeMap<Person,Integer>(new
AgeComparator());
ref1.put(p1, 100);
ref1.put(p2, 200);
System.out.println(ref1);
}
}
Serializable in arraylist:
now read from file , the ArrayList and traverse it using Iterator
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
}
public class ArrayList_Serialization_Demo
{
public static void main(String args[])
{
List<Student>mylist=new ArrayList<Student>();
Student s1=new Student();
Student s2=new Student();
s1.setRollno(1);
s1.setName("Abc");
s2.setRollno(2);
s2.setName("Xyz");
mylist.add(s1);
mylist.add(s2);
FileOutputStream fos=null;
FileInputStream fis=null;
ObjectOutputStream oos=null;
ObjectInputStream ois=null;
try {
fos=new
FileOutputStream("e:\\temp1\\examples\\temp.txt");
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos=new ObjectOutputStream(fos);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos.writeObject(mylist);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // serialization
try {
fis=new FileInputStream("e:\\temp1\\examples\\temp.txt");
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ois=new ObjectInputStream(fis);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<?> ref=null;
try {
ref=(List<?>) ois.readObject();//ref mdhe jaat list
jati
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator<?> itr=ref.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Using #map:
import java.util.*;
import java.io.*;
class MyClass implements Serializable
{
FileOutputStream fos=new
FileOutputStream("e:\\temp1\\examples\\temp.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
//m1.num=200;
m1.num=300;
System.out.println(m1.num); // 300
FileInputStream fis=new
FileInputStream("e:\\temp1\\examples\\temp.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Map<?,?> map=(Map<?,?>)ois.readObject();
System.out.println(ref.num); // 100
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
import java.util.*;
import java.io.*;
class MyClass implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
int num;
MyClass(int num)
{
this.num=num;
}
}
public class HashMap_Serialization_Demo
{
public static void main(String args[])
{
try
{
Map<String,MyClass>h=new HashMap<String,MyClass>();
FileOutputStream fos=new
FileOutputStream("e:\\temp1\\examples\\temp.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
//m1.num=200;
m1.num=300;
System.out.println(m1.num); // 300
FileInputStream fis=new
FileInputStream("e:\\temp1\\examples\\temp.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Map<?,?> map=(Map<?,?>)ois.readObject();
System.out.println(ref.num); // 100
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
Properties file:
Demo1:
Read properties file:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
Set properties:
package threadrpo;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
// add properties to it
p.setProperty("name", "Sachin");
p.setProperty("email",
"[email protected]");//both dtrings key
value
// store the properties to a file
try {
p.store(new FileWriter("e:\\info.properties"),
"Basic properties example");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
List<String> mylist1=Collections.synchronizedList(mylist);//collections
class in which they have synchronizedlist method to make safe
Map<String,Integer> mymap1=Collections.synchronizedMap(mymap);
/*
when u say
Not really.
Solution:
size() as well as get() methods invocation must happen together as one
unit, no interference from other threads should be there, or else we’ll
get the above exception.The fact that the individual methods are
synchronized is irrelevant. In fact, we can go back to using the non-
synchronized ArrayList, and the program ill work, as long as we
synchronize properly to make thesize() and get() calls happen as one
atomic, indivisible unit of execution:
*/
import java.util.*;
class HelloThread
{
int i=1;
*/
import java.util.*;
class HelloThread
{
int i=1;
try
{
thread1.join();
thread2.join();
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
System.out.println("ArrayList is\t"+arrayList);
}
}
Mixing in generic:
Test1:
import java.util.*;
public class Test1
{
void disp(List mylist)
{
Iterator it=mylist.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String args[])
{
List<Integer>m=new ArrayList<Integer>();
m.add(20);
m.add(40);
Test1 t=new Test1();
t.disp(m);
}
}
Test 2:
import java.util.*;
public class Test2
{
void disp(List mylist) //non generic
{
Iterator it=mylist.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
mylist.add(0,"hello");
}
public static void main(String args[])
{
List<Integer>m=new ArrayList<Integer>(); //generic
//use generic and non generic so it is mixing
m.add(20);
m.add(40);
Test2 t=new Test2();
t.disp(m);
System.out.println("in main\t"+m); //[“hello” ,20,40]
}
}
Test 3:
import java.util.*;
public class Test3
{
void disp(List mylist)
{
Iterator it=mylist.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
mylist.add(0,"hello");
}
public static void main(String args[])
{
List<Integer>m=new ArrayList<Integer>();
m.add(20);
m.add(40);
Test3 t=new Test3();
t.disp(m);
Integer ob=m.get(0); // risk involved
//in integer it trying to add string so it exception
System.out.println(ob);
}
}
import java.util.*;
class base
{
}
class sub extends base
{
}
class sub1 extends base
{
}
public class ArrayStoreExcepDemo
{
Upcasting in generic:
Animal arr[]=new Dog[2]; // allowed because during runtime if you say
arr[1]=new Cat();
There is a JVM to check what is illegal and give you
ArrayStoreException.
But
mylist.add(new Cat())
even though it is illegal,JVM will not be able to detect it because it
doesn't know anything about generics. All generic information compiler
removes at the time of compilation. ( Type Erasure )
Hence in case of generics compiler itself has to consider all possible
scenarios of runtime and guide you accordingly.
e.g.
disp(new ArrayList<Animal>());
test1:
import java.util.*;
class Animal
{
void eat()
{
}
public String toString()
{
return getClass().getName();
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog eat");
}
public String toString()
{
return getClass().getName();
}
}
class Cat extends Animal
{
void eat()
{
System.out.println("Cat eat");
}
public String toString()
{
return getClass().getName();
}
}
public class Test1
{
static void disp(List<Animal> arr) //if we make animal generic
then all child become generic
{
arr.add(new Dog());
arr.add(new Cat());
System.out.println(arr);
}
public static void main(String args[])
{
disp(new ArrayList<Animal>());
}
}
Test 1_a
import java.util.*;
class Animal
{
void eat()
{
}
public String toString()
{
return getClass().getName();
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog eat");
}
public String toString()
{
return getClass().getName();
}
}
class Cat extends Animal
{
void eat()
{
System.out.println("Cat eat");
}
public String toString()
{
return getClass().getName();
}
}
public class Test1_a
{
static void disp(List<Animal> arr)
{
arr.add(new Dog());
arr.add(new Cat());
}
}
Test 2:
import java.util.*;
class Animal
{
void eat()
{
}
public String toString()
{
return getClass().getName();
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog eat");
}
public String toString()
{
return getClass().getName();
}
}
class Cat extends Animal
{
void eat()
{
System.out.println("Cat eat");
}
public String toString()
{
return getClass().getName();
}
}
public class Test2
{
static void disp(List<? extends Animal> arr)
{
for(Animal l:arr)
{
l.eat();
}
}
public static void main(String args[])
{
Dog d[]={new Dog(),new Dog()};
List<Dog>l=new ArrayList<Dog>();
l.add(d[0]);
l.add(d[1]);
disp(l);
Test 3
import java.util.*;
class Animal
{
void eat()
{
}
public String toString()
{
return getClass().getName();
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog eat");
}
public String toString()
{
return getClass().getName();
}
}
class Cat extends Animal
{
void eat()
{
System.out.println("Cat eat");
}
public String toString()
{
return getClass().getName();
}
}
public class Test4
{
static void disp(List<? super Dog> arr) //parent only
{
arr.add(new Dog());
arr.add(new Dog());
for(Object d:arr)
{
if(d instanceof Dog)
{
((Dog)d).eat();
}
else if(d instanceof Cat)
{
((Cat)d).eat();
}
}
System.out.println(arr);
}
public static void main(String args[])
{
Dog d[]={new Dog(),new Dog()};
List<Animal>l=new ArrayList<Animal>();
l.add(new Cat());
disp(l);
List<Dog>l1=new ArrayList<Dog>();
l1.add(new Dog());
disp(l1);
}
}
Test 4
Test 6
import java.util.*;
class Animal
{
void eat()
{
}
public String toString()
{
return getClass().getName();
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog eat");
}
public String toString()
{
return getClass().getName();
}
}
class Cat extends Animal
{
void eat()
{
System.out.println("Cat eat");
}
public String toString()
{
return getClass().getName();
}
}
public class Test6
{
static void disp(List<Object> arr)//fkt object chalel child nhi
chlnr
{
arr.add(new Dog()); // allowed
arr.add(200); // allowed
System.out.println(arr);
}
public static void main(String args[])
{
List<Object>ob=new ArrayList<Object>();
disp(ob);
List<Integer>ob1=new ArrayList<Integer>();
// disp(ob1); // error, not allowed//show error in child
}
}
Before Java8 interfaces were too tightly coupled with their implementation classes. I.e. it was not possible to add a method
in interface without breaking the implementer class. Once you add a method in an interface, all its implemented classes had
to define method body of this new method. e.g. if you add a single method in an interface List, it breaks everything. You
need to add its implementation in every class that implements List interface. Imagine in real world how many custom
classes would change if you add method declarations in interfaces like this.
A default method is a method defined and not just declared in an interface whose method header begins with the default
keyword. Every class that implements the interface inherits the interface's default methods and can override them.
Demo1:
interface emp
{
String disp1();
default String disp2()
{
return "in disp2"; //in interface we use concrete method we use
default.
}
}
class MyClass implements emp
{
public String disp1()
{
return "in disp1";
}
}
public class Demo1
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp1());
System.out.println(m.disp2());
}
}
Demo2:
// error: Duplicate default methods named disp2 with the parameters () and ()
are inherited from the types emp1 and emp
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class MyClass implements emp,emp1//show error bcoz both have duplicate disp2
method
{
public String disp1()
{
return "in disp1";
}
}
public class Demo2
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp1());
System.out.println(m.disp2());
}
}
Demo3:
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class MyClass implements emp,emp1
{
// must be public
public String disp2() //in that disp2() of class wins
{
return "in overridden method of MyClass";
}
public String disp1() /
{
return "in disp1";
}
}
public class Demo3
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp1());
System.out.println(m.disp2());
}
}
Demo3_a:
package trial;
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class xyz //that class method will wins
{
public String disp2()
{
return "in disp2 of xyz";
}
}
class MyClass extends xyz implements emp,emp1
{
Demo3_b:
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class xyz
{
public String disp2()
{
return "in disp2 of xyz";
}
}
class MyClass extends xyz implements emp,emp1
{
String disp2() // error/ error bcoz need to make it public bcoz in
parent it is by default public
{
return "in disp2 of MyClass";
}
public String disp1()
{
return "in disp1";
}
}
public class Demo3_b
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp1());
System.out.println(m.disp2());
}
}
Demo 3_c:
package trial;
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class xyz
{
public String disp2()
{
return "in disp2 of xyz";
}
}
class MyClass extends xyz implements emp,emp1
{
public String disp2() //error is solved by providing public
{
return "in disp2 of MyClass";
}
public String disp1()
{
return "in disp1";
}
}
public class Demo3_c
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp1());
System.out.println(m.disp2());
}
}
Demo 4:
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1 extends emp //extend interface
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class MyClass implements emp1
{
public String disp1()
{
return "in disp1";
}
}
public class Demo4
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp1());
System.out.println(m.disp2());//jvlchyala call kel
}
}’
Demo 5
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1 extends emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class MyClass implements emp1
{
// must be public
public String disp2()
{
return "in overridden method of MyClass";
}
public String disp1()
{
return "in disp1";
}
}
public class Demo5
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp1());
System.out.println(m.disp2());
}
}
Demo 6:
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1 extends emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class MyClass implements emp1
{
// must be public
public String disp2()
{
System.out.println(emp1.super.disp2());
//System.out.println(emp.super.disp2()); // Illegal reference to super type
emp, cannot bypass the more specific direct super type emp1
//ekch pappa vrch cl kru shkto
Demo 6_a:
interface emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1 extends emp
{
String disp1();
default String disp2()
{
System.out.println(emp.super.disp2());//disp 2 call of emp
// System.out.println(super.disp2()); // super reference is
illegal in interface context
return "in disp2 of emp1";
}
}
class MyClass implements emp1
{
// must be public
public String disp2()
{
System.out.println(emp1.super.disp2());
//System.out.println(super.disp2()); // error: The method disp2()
is undefined for the type Object
//System.out.println(emp.super.disp2()); //Illegal reference to super type
emp, cannot bypass the more specific direct super type emp1
Demo 7:
package core1;
interface emp
{
int k=10; // public static and final
// private int g=40; modifier private not allowed
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1 extends emp
{
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class MyClass implements emp1
{
// must be public
public String disp2()
{
System.out.println(emp1.super.disp2());
//System.out.println(emp.super.disp2()); not an enclosing class: emp
O/p
in disp1
in disp2 of emp1
in overridden method of MyClass
emp data is 10
emp1 data is hello
MyClass data is hello
Demo 7b
interface emp
{
int k=10; // public static and final
// private int g=40; modifier private not allowed
String disp1();
default String disp2()
{
return "in disp2 of emp";
}
}
interface emp1
{
String k="hello"; //this is String and in parent it is int so it not
override
String disp1();
default String disp2()
{
return "in disp2 of emp1";
}
}
class MyClass implements emp,emp1
{
// must be public
public String disp2()
{
System.out.println(emp1.super.disp2());
//System.out.println(emp.super.disp2()); not an enclosing class: emp
*/
interface emp
{
default String disp2()
{
return "in disp2 of emp";
}
}
class base
{
// must be public
public String disp2()
{
return "in disp2 of base"; //always class wins
}
}
class MyClass extends base implements emp
{
}
public class Demo8
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp2());
}
}
Demo 8_a
package trial;
interface emp
{
default String disp2()
{
return "in disp2 of emp";
}
}
class base
{
// must be public
String disp2()
{
return "in disp2 of base";
}
}
// error : The inherited method base.disp2() cannot hide the public abstract
method in emp
class MyClass extends base implements emp
{
}
public class Demo8_a
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp2());
}
}
Demo 8b
package trial;
interface emp
{
default String disp2()
{
return "in disp2 of emp";
}
}
class base
{
// must be public
String disp2()
{
return "in disp2 of base";
}
}
Demo 9
/*
*/
interface emp
{
default String disp2()
{
return "in disp2 of emp";
}
}
class base
{
// must be public
public String disp2()
{
return "in disp2 of base";
}
}
class MyClass extends base implements emp
{
void perform()
{
System.out.println(emp.super.disp2());
}
}
public class Demo9
{
public static void main(String args[])
{
MyClass m=new MyClass();
System.out.println(m.disp2());
m.perform();
}
}
Demo 10
package trial;
interface emp1
{
default void disp()
{
System.out.println("in emp1 disp");
}
}
interface emp2
{
void disp();
}
// error:
// The default method disp() inherited from emp1 conflicts with another method
inherited from emp2
abstract class EmpImpl implements emp1,emp2 // error he don’t know which disp
is implement
{
}
public class Demo10
{
public static void main(String args[])
{
System.out.println("done");
}
}
Demo 11
//package trial;
interface emp1
{
default void disp()
{
System.out.println("in emp1 disp");
}
}
interface emp2
{
void disp();
}
abstract class EmpImpl implements emp1,emp2
{
public void disp()
{
System.out.println("inside EmpImp disp");
}
}
public class Demo11
{
public static void main(String args[])
{
System.out.println("done");
}
}
Demo 12:
package trial;
interface emp1
{
default void disp()
{
System.out.println("in emp1 disp");
}
}
interface emp2
{
void disp();
}
abstract class EmpImpl implements emp2
{
/*public void disp() // not required
{
}*/
}
public class Demo12
{
public static void main(String args[])
{
System.out.println("done");
}
}
Demo 13:
package trial;
interface A
{
default void fun()
{
System.out.println("A fun");
}
}
interface B
{
default void fun()
{
System.out.println("B fun");
}
}
class C implements A
{
}
// error:
//Duplicate default methods named fun with the parameters () and () are
inherited from the types A and B
class D extends C implements B // error
{
}
public class Demo13
{
public static void main(String args[])
{
D ob=new D();
ob.fun();
}
}
Demo 14:
package trial;
interface A
{
default void fun()
{
System.out.println("A fun");
}
}
interface B
{
default void fun()
{
System.out.println("B fun");
}
}
class C implements A
{
}
class D extends C implements B
{
public void fun()
{
System.out.println("D fun");
}
}
public class Demo14
{
public static void main(String args[])
{
D ob=new D();
ob.fun();
}
}
Demo 15:
package trial;
interface A
{
default void fun()
{
System.out.println("A fun");
}
}
interface B
{
default void fun()
{
System.out.println("B fun");
}
}
class C implements A
{
public void fun()
{
System.out.println("C fun"); //always class wins
}
}
class D extends C implements B
{
}
public class Demo15
{
public static void main(String args[])
{
D ob=new D();
ob.fun(); }}
Static method:
A static method is a method that's associated with the class in which it's defined, rather than with any
object created from that class. Every instance of the class shares the static methods of the class. Java 8
also lets static methods be defined in interfaces where they can assist default methods. For example,
the java.util.Comparator interface defines the following static method:
As well as being directly invocable, comparingDouble() is invoked from this default method of
Comparator:
When you implement an interface that contains a static method, the static method is still part of the
interface and not part of the implementing class. For this reason, you cannot prefix the method with
the class name. Instead, you must prefix the method with the interface name, which I demonstrate in
Listing 3.
Listing 3 Z.java.
interface X
{
static void foo()
{
System.out.println("foo");
}
}
class Y implements X
{
}
public class Z
{
public static void main(String[] args)
{
X.foo(); //direct call like interfacename.method
// Y.foo(); // won't compile
}
}
Expression Y.foo() will not compile because foo() is a static member of interface X and not a
static member of class Y.
javac Z.java
Run the Z application as follows:
java Z
foo
Before Java 8 made it possible to declare static methods in interfaces, it was common practice to place
these methods in companion utility classes. For example, the java.util.Collections class is a
companion to the java.util.Collection interface, and declares static methods that would be
more appropriate in the relevant Java Collections Framework interfaces.
Although these and similar changes will probably never be made to the Java Collections Framework
(too much legacy code depends on the current placement of such methods), you no longer need to
provide your own companion utility classes. Instead, you can place static methods in the appropriate
interfaces, which is a good habit to cultivate.
1. Interface static methods are part of interface, we can’t use it for implementation class objects.
2. Interface static methods are good for providing utility methods, for example null check,
collection sorting etc.
3. Interface static method helps us in providing security by not allowing implementation classes
to override them.
4. We can’t define static methods for Object class methods, we will get compiler error as “This
static method cannot hide the instance method from Object”. This is because it’s not allowed
in java, since Object is the base class for all the classes and we can’t have one class level
static method and another instance method with same signature.
5. We can use static interface methods to remove utility classes such as Collections and move all
of it’s static methods to the corresponding interface, that would be easy to find and use.
In addition to default methods, you can define static methods in interfaces. (A static method is a method that is
associated with the class in which it is defined rather than with any object. Every instance of the class shares its
static methods.) This makes it easier for you to organize helper methods in your libraries; you can keep static
methods specific to an interface in the same interface rather than in a separate class.
Like static methods in classes, you specify that a method definition in an interface is a static method with the
static keyword at the beginning of the method signature. All method declarations in an interface, including
static methods, are implicitly public, so you can omit the public modifier.
Background for static methods inside interface:
When collection API was designed there was a need to define utility methods or algorithms. Utilitiy methods or
algorithms are basically static methods. Entire collection API had only interfaces ( List, Set and Map ) at the top
level , static methods couldn’t be added inside these interfaces. This is because at that time static methods were
not allowed inside interfaces. So founders of Java had to define a separate class Collections and define all static
methods as utility methods or algorithms inside it.
In Java8 when interfaces were modified to add default methods , Java people decided to make one more change
and that is to allow static methods as well inside an interface so that in future if there is a need to define
algorithms or utility methods , they can be easily added inside an interface.
Demo1:
interface emp1
{
static void disp1()
{
System.out.println("in disp1 of emp1");
}
default void disp2()
{
System.out.println("in disp2 of emp1");
}
}
public class Demo1 implements emp1
{
void disp1()
{
emp1.disp1();
System.out.println("in disp1 of Demo1");
}
public void disp2() // must be public
{
emp1.super.disp2();
System.out.println("in disp2 of Demo1");
}
public static void main(String args[])
{
Demo1 d=new Demo1();
d.disp1();//class method call
d.disp2();
Emp.disp1()//to call disp1 of emp
}
}
Demo2:
interface emp1
{
static void disp1()
{
System.out.println("in disp1 of emp1");
}
default void disp2()
{
System.out.println("in disp2 of emp1");
}
}
interface emp2 extends emp1
{
/*static void disp1()
{
System.out.println("in disp1 of emp2");
}*/
}
public class Demo2 implements emp1
{
/*void disp1()
{
emp1.disp1();
System.out.println("in disp1 of Demo2");
}*/
public void disp2() // must be public
{
emp1.super.disp2();
System.out.println("in disp2 of Demo2");
}
public static void main(String args[])
{
Demo2 d=new Demo2();
// d.disp1(); error //static disp1 so not call
emp1.disp1();
d.disp2();
// emp2.disp1(); error bcoz not static
}
}
Demo 2_a:
//static interface methods can be accessed only with the help of same
interface in which they are declared.
interface emp1
{
static void disp1()
{
System.out.println("in disp1 of emp1");
}
default void disp2()
{
System.out.println("in disp2 of emp1");
}
}
interface emp2 extends emp1
{
static void disp1()
{
System.out.println("in disp1 of emp2");
}
}
public class Demo2_a implements emp1
{
void disp1()
{
emp1.disp1();
System.out.println("in disp1 of Demo2");
}
public void disp2() // must be public
{
emp1.super.disp2();
System.out.println("in disp2 of Demo2");
}
public static void main(String args[])
{
Demo2_a d=new Demo2_a();
d.disp1();
emp1.disp1();
d.disp2();
emp2.disp1();
}
}
Scoket:
To connect to another machine, we need a socket connection. By the way, what's a connection? A relationship between two
machines, where two pieces of software know about each other. Those two pieces of software know how to communicate
with each other. In other words, they know how to send bits to each other.
A socket connection means the two machines have information about each other, including network location (IP
address) and TCP port.
There are several different types of socket that determine the structure of the transport layer. The most common types
are stream sockets and datagram sockets.
Stream Sockets
Stream sockets provide reliable two-way communication similar to when we call someone on the phone. One side
initiates the connection to the other, and after the connection is established, either side can communicate to the
other.
In addition, there is immediate confirmation that what we said actually reached its destination.
Stream sockets use a Transmission Control Protocol (TCP), which exists on the transport layer of the Open
Systems Interconnection (OSI) model. The data is usually transmitted in packets. TCP is designed so that the
packets of data will arrive without errors and in sequence.
Webservers, mail servers, and their respective client applications all use TCP and stream socket to communicate.
Datagram Sockets
Communicating with a datagram socket is more like mailing a letter than making a phone call. The connection
is one-way only and unreliable.
If we mail several letters, we can't be sure that they arrive in the same order, or even that they reached their
destination at all. Datagram sockets use User Datagram Protocol (UDP). Actually, it's not a real connection, just
a basic method for sending data from one point to another.
Datagram sockets and UDP are commonly used in networked games and streaming media.
Network layer=refer ppt for info
TCP
UDP
User Datagram Protocol
UDP:
TCP:
NIO Channels:
Channels are similar to Streams in traditional Java I/O API with the exceptions that they can provide
three modes: input, output or bi-directional.
Streams on the other hand, are uni-directional (you were either using InputStream or OutputStream).
Channels interact directly with buffers and the native IO source, that is, file, or socket.
Channel working:
uSing NIO
-buffer register is inside java application and it is fast so works faster.
Channels and nio channels are two different concept related to ip and
output concepts
Nio give implicitly provide demon thread so that thread works the task and
main thread work on other task
In previous channel no only main thread works
In Io only read only or write only ,but in nio we do multiple task at same
time
Asynchronus:
Pending
Path vs Paths and get
Path is an interface and Paths is a final class of java.nio.file API. Path has been introduced in JDK
7. The use of Path is to locate a file in file system. Path has flexibility to locate the file by proving
path in parts. Path provides file path attribute like file name, file system, parent name and root.
Paths are used to get Path instance by its static method. Path returns java.io.File when we call
Path.toFile().The instance of Path can be obtained by FileSystems and Paths class.
If we have a file located in the path as c:\temp\test.txt. We can get this path in many ways.
This is because Paths.get(String first, String more...) can accept one or more than one argument.