Java Notes
Java Notes
Rollno-22070166
Course –bsc cs hons
4th sem
//constructor Syntax:-
A(){
a=8; Class A{
name="shristi";
} A(){ // no parameter
void show(){
System.out.println(a+" "+ name); //variable intitialization
}
}
} }
public class ConstructorExample {
public static void main(String[] args){
A ref=new A(); Note:- if we do not add default constructor then
ref.show(); compiler by itself add default constructor.
}
}
}
Instance Block
}
Instance block is similar to method which has no public class InstanceBlock {
public static void main(String[] args) {
name,it can be written inside a class only but not
Ib r= new Ib();
inside a method. }
}
1 It always gets executed before the constructor.
2 We can use variable only inside the instance block
Static Block
not method.
Static block is such kind of block in java which
3 We write time consuming code inside a instance block
gets executed at the time of loading the .class file
like-JDBC connectivity.
into JVM memory.
}
Syntax:-
Class class-name1 //Super class
{
//---code
}
class class-name2 extends class-name1 //sub class
{
//---code
Inheritance }
}
Syntax:- class multi2 extends multi // first sub class
{
Class super void multiply()
{
{ a=56;
b=86;
//code c=a*b;
System.out.println("multiplication of a and b
} is :-"+c);
}
Class sub1 extends super
}
{
//code class multi3 extends multi2{ //second sub class
void division()
} {
a=200;
Class sub2 extends sub1 b=86;
c=a/b;
{ System.out.println("division of two number:-"+c);
}
//code }
}
public class MultilevelInheritance {
Example of multilevel inheritance:- public static void main(String[] args) {
multi3 r= new multi3();
class multi{ //Super class
r.add();
int a,b,c;
r.sub();
Multiple Inheritance {
//code
void display()
{
// print maxSpeed of base class (vehicle)
System.out.println("Maximum Speed: "
+ super.maxSpeed);
}
}
// Driver Program
class SuperVariable {
public static void main(String[] args)
{
Car1 small = new Car1();
small.display();
}
}
// superclass Person
class Person {
void message()
{
System.out.println("This is person class\n");
}
void show()
{
System.out.println("Inside show function");
}
public static void main(String[] args) When there are multiple functions with the same name
{ but different parameters then these functions are said
This object = new This(); to be overloaded. Functions can be overloaded by
} changes in the number of arguments or/and a change in
} the type of arguments.
Polymorphism Syntax:-
System.out.println("Enter salary:-");
1 Interface methods are by default public and abstract. sal=sc.nextDouble();
}
2 Interface variables are by default public ,static and public void output()
final {
System.out.println(name+ " "+sal);
3 Interface method must be overridden inside the }
implementing classes.
public static void main(String[] args) {
4 Interface is nothing but deals between client and client c=new Raju();
developer. c.input();
c.output();
}
Syntax:- }
Interface interface-name
{ public class Interface {
}
//declare constants fields
//Abstract methods
}
//code
Syntax to achieve multiple through only interfaces:-
}
Class class-name implements Interface1,Interface2
Interface Interface-name
{
{
//code
//code
}
}
Interface interface-name2 extends interface-name
Example of multiple inheritance:-
{
interface A1
{ //code
void show();
} }
interface B1
{
void show(); Example :-
void disp();
}
class MITI }
{
public static void main(String[] args) {
Raj r=new ankit1(); Final class:-
r.add();
r.sub(); Whenever we declare a class as a final it can’t be
} extended or inherited to sub class. Used to prevent
} Inheritance.
Final keyword
Syntax:-
Final is a non access modifier which provides
restriction. In java we can use final in three ways:- Final class A
1 variable 2 method 3 class {
New Keyword:-
New keyword created new objects and it is used to
allocate dynamic memory at run time.
It is used to create the object.
It allocates the memory atr un time
All objects occupy memory in the heap area.
It invokes object constructor
It requires a single ,postfix argument to call the
constructor.
Syntax:-
Class-name reference-variable= new class-name();
Advantages of packages:-
1 Reusability
2 Security
Packages:- 3 Fast searching
A package arrange number of classes interfaces and 4 naming conflicting
sub-package of same type into a particular group.
5 hiding
Major reasons why an exception Occurs Example of try and catch block:-
Invalid user input
Device failure
Loss of network connection
Physical limitations (out-of-disk memory)
public class ExceptionHandling {
Code errors public static void main(String[] args) {
Out of bound System.out.println("main method started");
Null reference int a=10,b=0,c;
Type mismatch try{
Opening an unavailable file c=a/b;
Database errors System.out.println(c);
Arithmetic errors }
catch(Exception e)
{
System.out.println("Cant divide number by 0");
}
Exception Handling:- System.out.println("main method ended");
}
In exception handling ,we should have an alternate
source through which we can handle the exception. }
Exception Handling is one of the effective means to
handle runtime error so that the regular flow of the Exception hierarchy
application can be preserved.Java exception handling is
Built in Exception:-
Built in Exceptions are the exceptions that are
available in java librarires .These exceptions are
suitable to explain certain error situations.
They are:-
Checked Exceptions:-
Checked excpetions are called compile-time
exceptions beacause these exceptions are
Types of exception:- checked at compile –time by the compiler.
Unchecked Exceptions:-
The unchecked exceptions are just opposite
to the checked exceptions. The compiler
will not check these exceptions at compile
time. In simple words, if a program throws
an unchecked exception, and even if we
didn’t handle or declare it ,the program
would not give a compilation error.
class NPE{ }
public static void main(String[] args) { }
String str=null;
try
{ Output:-
System.out.println(str.toUpperCase());
} java.lang.NullPointerException: Cannot invoke
catch(NullPointerException n) "String.toUpperCase()" because "str" is null
{
System.out.println("NUll can't be casted"); at NPE.main(NullPointerException.java:6)
}
Process finished with exit code 0
}
}
4 Throws keryword
Program:-
They are defined in
They are defined in
java.lang.Exception // Java program that demonstrates the use of throw
java.lang.Error package. class ThrowDemo {
package
static void fun()
{
Examples : Checked try {
Exceptions : throw new NullPointerException("demo");
Examples : }
SQLException, IOException
java.lang.StackOverflowE catch (NullPointerException e) {
Unchecked Exceptions :
rror, System.out.println("Caught inside fun().");
ArrayIndexOutOfBoundExcep
java.lang.OutOfMemoryErr throw e; // rethrowing the exception
tion, }
or
NullPointerException, }
ArithmeticException.
Multithreading
User-defined exception handling