Java notes
Java notes
Rollno-22070166
Course –bsc cs hons 4th sem
}
}
}
}
Example of default constructor:-
Types of constructor:- class C{
int a; String b; boolean c;
1 Private Constructor C(){
a=100;
2 Default Constructor b="ankit";
c=true;
3 Parameterized Constructor }
void display(){
4 Copy Constructor System.out.println(a+" "+b+" "+c);
}
}
public class B {
Instance Block }
}
Instance block is similar to method which has no public class InstanceBlock {
name,it can be written inside a class only but not public static void main(String[] args) {
Ib r= new Ib();
inside a method.
}
1 It always gets executed before the constructor. }
3 We write time consuming code inside a instance block Static block is such kind of block in java which
like-JDBC connectivity. gets executed at the time of loading the .class file
into JVM memory.
Syntax:-
Static block gets executed befor instance block.
Class class-name{
Syntax:-
Shristi patel Page 5
Class class-name{ 4 A sub class contains all the features of super class
So we should create the object of sub class.
Static{
//----code
5 Method overriding is only possible through
} inheritance.
}
Syntax:-
Class class-name1 //Super class
{
//---code
}
class class-name2 extends class-name1 //sub class
{
//---code
Inheritance
}
When we construct a new class from existing class
in such a way that the new class access all the
features and properties of existing class is called Types Of Inheritance:-
inheritance.
1 Single Inheritance:-
1 The java extends keyword is used to perform
inheritance.
Super class
2 It provides code reusability.
3 We can’t access private members of the class through
inheritance.
Sub class
//code
//code
}
}
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
}
{
class multi3 extends multi2{ //second sub class
//code 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();
void add() r.multiply();
{ r.division();
Multiple Inheritance {
//code
//code }
}
}
Class C extends A
{ Super Keyword
//code
The super keyword in Java is a reference variable
} which is used to refer immediate parent class object.
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");
}
}
// Subclass Student
void show()
{
System.out.println("Inside show function");
}
Advantages of abstraction:-
Example of anbstract class:-
1 Helps to increase the security of an application or
program as only essential details are provided to the // Abstract class
user. abstract class Sunstar {
abstract void printInfo();
2 The enhancement will necome very easy because without }
affecting end users we can able to perform any type of
changes in out internal system. // Abstraction performed using extends
class Employee extends Sunstar {
void printInfo()
{
Abstraction can be implemented through abstract String name = "avinash";
class and interfaces. int age = 21;
float salary = 222.2F;
System.out.println(name);
Abstract Class System.out.println(age);
A class which contains the abstract keyword I System.out.println(salary);
}
its declaration is called abstract class.
}
1 We can’t create object for abstract class.
// Base class
2 an abstract class may or may not have all abstract class AbstractClasss {
methods.Some of them can be concrete method. public static void main(String args[])
{
3 Constructors are allowed. Sunstar s = new Employee();
s.printInfo();
4 There can be a final method in abstract class but any }
abstract method in class cn not be declared as final }
5 We can define static method in an abstract class.
Abstract Method
Interface
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
Class Interface
}
In an interface, you must
In class, you can
initialize variables as they
Example of interface:- instantiate
are final but you can’t create
//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();
}
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 {
Advantages of packages:-
1 Reusability
2 Security
Packages:-
3 Fast searching
A package arrange number of classes interfaces and
sub-package of same type into a particular group. 4 naming conflicting
5 hiding
Predefined
o Java.lang
o Java.util
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
}
}
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.