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

Java Project Final-3

The document is a technical internship project report on Java programming submitted for a Bachelor of Technology degree. It includes an introduction to Java programming, data types, object-oriented programming features, tokens, control statements, methods, classes and objects, access modifiers, constructors, inheritance, abstract classes, interfaces, arrays, packages, exception handling, example projects, advantages and disadvantages of Java, and a conclusion.

Uploaded by

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

Java Project Final-3

The document is a technical internship project report on Java programming submitted for a Bachelor of Technology degree. It includes an introduction to Java programming, data types, object-oriented programming features, tokens, control statements, methods, classes and objects, access modifiers, constructors, inheritance, abstract classes, interfaces, arrays, packages, exception handling, example projects, advantages and disadvantages of Java, and a conclusion.

Uploaded by

Shubham Pradhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

A TECHNICAL INTERNSHIP PROJECT REPORT

ON
“JAVA PROGRAMMING”
SUBMITTED FOR THE FULFILLMENT OF THE
REQUIREMENTS FOR THE DEGREEOF THE BACHELOR
OF TECHNOLOGY UNDER BPUT

Submitted to: Department of Computer Science & Engineering

SUBMITTED BY

NIKHIL KUMAR SETHY


2101109152

PARALA MAHARAJA ENGINEERING COLLEGE

SITALLAPALLI, BERHAMPUR-761003

1
CERTIFICATE

2
ACKNOWLEDGEMENT

I express my deep sense of gratitude indebtedness on the successful completion of my internship work
would have been incomplete without the mention of the people who made it possible ,whose precious
guidance ,encouragement , supervision and helpful discussion made it possible. I would like to convey my
heartfelt gratitude to Dr. Debasish Mohapatra for his tremendous support and assistance in the completion of
my project. I would also like to thank our principal Prof. Ranjana Kumar Swain for providing me with this
wonderful opportunity to work on a project with the topic Food Culture during the Pandemic. The
completion of the project would not have been possible without their help and insights. Lastly I would like
to thanks my parents, my peers and fellow classmates, who supported and help me throughout the course of
internship project.

3
ABSTRACT

The java programming language is currently growing in popularity within the academic community , and a
result this ,a lot of colleges converting their introductory computer science course into java . while java is
very portable and web compatible languages , students often have a hard time mastering it . there have been
some project that were aimed to assist student in grasping java s conceptual frame work , but many of these
involved either simplified java syntax pre constructed module that distance student from the process of
coding . our goal was to design an educational tool that will identify common java programming errors and
misconception and facilitate the learning process while making sure that students are directly with their
code.

4
CONTENTS PAGES
• INTRODUCTION TO JAVA 06
• DATA TYPES 08
• FEATURES OF OOPS 09
• TOKENS IN JAVA 10
• CONTROL STATEMENT IN JAVA 16
• METHODS 18
• CLASS AND OBJECTS 22
• ACCESS MODIFIER 24
• CONSTRUCTER 24
• INHERITANCE 28
• ABSTRACT CLASS 28
• INTERFACE 30
• ARRAY 31
• PACKAGE 32
• EXCEPTION HANDLING 33
• PROJECTS 34
• ADVANTAGES OF JAVA 38
• DISADVANTAGES OF JAVA 38
• CONCLUSION 38

5
INTRODUCTION OF JAVA
Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure
programming language.

.Platform: Any hardware or software environment in which a program runs , is known as a platform. Since
Java has a runtime environment (JRE) and API, it is called a platform.

Java is a class-based, object-oriented programming language that is designed to have as few


implementation dependencies as possible. It is intended to let application developers write once, and run
anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without
the need for recompilation. Java was first released in 1995 and is widely used for developing applications
for desktop, web, and mobile devices. Java is known for its simplicity, robustness, and security features,
making it a popular choice for enterprise-level applications.
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1995 and later acquired by
Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging
programming easy. It helps to create reusable code and modular programs. Java is a class-based, object-
oriented programming language and is designed to have as few implementation dependencies as possible.
A general-purpose programming language made for developers to write once run anywhere that is
compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code
that can run on any Java Virtual Machine.

History:

Java’s history is very interesting. It is a programming language created in 1991. James Gosling, Mike
Sheridan, and Patrick Naught on, a team of Sun engineers known as the Green team initiated the Java
language in 1991. Sun Microsystems released its first public implementation in 1996 as Java 1.0. It
provides no-cost -run-times on popular platforms. Java1.0 compiler was re-written in Java by Arthur Van
Hoff to strictly comply with its specifications. With the arrival of Java 2, new versions had multiple
configurations built for different types of platforms.
In 1997, Sun Microsystems approached the ISO standards body and later formalized Java, but it soon
withdrew from the process. At one time, Sun made most of its Java implementations available without
charge, despite their proprietary software status. Sun generated revenue from Java through the selling of
licenses for specialized products such as the Java Enterprise System.
APPLICATION:
According to Sun, 3 billion devices run Java. There are many devices where Java is currently used. Some of
them are as follows:

1. Desktop Applications such as acrobat reader, media player, antivirus, etc.

2. Web Applications.

3. Enterprise Applications such as banking applications.

4. Mobile

5. Embedded System

6. Robotics

7. Games, etc.

6
JVM

JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't physically
exist. It is a specification that provides a runtime environment in which Java byte code can be executed. It can
also run those programs which are written in other languages and compiled to Java byte code.

JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform dependent
because the configuration of each OS is different from each other. However, Java is platform independent.
There are three notions of the JVM: specification, implementation, and instance.

JRE

JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java Runtime
Environment is a set of software tools which are used for developing Java applications. It is used to provide
the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries +
other files that JVM uses at runtime.

The implementation of JVM is also actively released by other companies besides Sun Micro Systems.

JDK

JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development
environment which is used to develop Java applications and applets. It physically exists. It contains JRE +
development tools.

JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:

o Standard Edition Java Platform

o Enterprise Edition Java Platform

o Micro Edition Java Platform

The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (java), a compiler, a jar ,a documentation generator, etc. to complete the development of a
Java Application.

7
DATATYPES IN JAVA
Data types specify the different sizes and values that can be stored in the variable. There are two types of data
types in Java:

Primitive data types:

In Java language, primitive data types are the building blocks of data manipulation. These are the most basic
data types available in Java language.

o There are 8 types of primitive data types:

o Boolean data type

o byte data type

o char data type

o short data type

o integer data type

o long data type

o float data type

o double data type

Non Primitive data types :


The Reference Data Types will contain a memory address of variable values because the reference types
won’t store the variable value directly in memory. They are strings, objects, arrays, etc.

JAVA FEATURES:

The primary objective of Java programming language creation was to make it portable, simple and secure
programming language. Apart from this, there are also some excellent features which play an important role
in the popularity of this language. The features of Java are also known as Java buzzwords.

A list of the most important features of the Java language is given below.

1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
8
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic

FEATURES OF OOPS
Object-Oriented Programming is a paradigm that provides many concepts, such as inheritance, data
binding, polymorphism, etc.

Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.
It simplifies software development and maintenance by providing some concepts:

o Object

o Class

o Inheritance

o Polymorphism

o Abstraction

o Encapsulation

Object

o Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard,
bike, etc. It can be physical or logical.
o An Object can be defined as an instance of a class. An object contains an address and takes up some
space in memory. Objects can communicate without knowing the details of each other's data or code.
The only necessary thing is the type of message accepted and the type of response returned by the
objects.

Example:

o A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging
the tail, barking, eating, etc.

Class

o Collection of objects is called class. It is a logical entity.

9
o A class can also be defined as a blueprint from which you can create an individual object. Class doesn't
consume any space.

Inheritance

When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.
It provides code reusability. It is used to achieve runtime polymorphism.

Polymorphism

If one task is performed in different ways, it is known as polymorphism. For example: to convince the
customer differently, to draw something, for example, shape, triangle, rectangle, etc.

In Java, we use method overloading and method overriding to achieve polymorphism.

Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.

Abstraction
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't
know the internal processing.

In Java, we use abstract class and interface to achieve abstraction.

Encapsulation

Binding (or wrapping) code and data together into a single unit are known as encapsulation. For
example, a capsule, it is wrapped with different medicines.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data
members are private here.

TOKENS IN JAVA
The tokens are the small building blocks of a Java program that are meaningful to the Java compiler.

In Java, the program contains classes and methods. Further, the methods contain the expressions and

statements required to perform a specific operation. These statements and expressions are made up

of tokens. In other words, we can say that the expression and statement is a set of tokens.

The Java compiler breaks the line of code into text (words) is called Java tokens. These are the smallest

element of the Java program. The Java compiler identified these words as tokens. These tokens are separated

by the delimiters. It is useful for compilers to detect errors. Remember that the delimiters are not part of the

Java tokens.

10
Types of Java Tokens

There are five types of java tokens: keywords , identifiers, literals, operators and separators. The

classification is based on their work type; some are used to define names, and others for arithmetic

operations.

Keywords

Keywords are reserved words in programming languages. These are used to indicate predefined terms and

actions in a program. As a result, these words are not allowed to be used as names of variables or objects. It

is case-sensitive and always written in lowercase. Java has the following keywords:

Identifier

A method name, class name, variable name, or label is an identifier in Java. The user typically defines these.

The identifier names cannot be the same as any reserved keyword.

There are rules for naming identifiers.

⭐ The characters allowed are [A-Z], [a-z], [0-9], _ and $.

⭐ Identifiers are case-sensitive. That is, “ninja” is not the same as “NINJA”.

⭐ Identifier names should not start with a digit.

⭐ Whitespace is not allowed inside an identifier.

⭐ Keywords can’t be used as an identifier.

Literals

Literals represent fixed values in a source code. These are similar to standard variables with the difference

that these are constant. These can be classified as an integer literal, a string literal, a Boolean etc. The user

defines these mainly to define constants.

There are five types of literals in Java:

Integer

Floating Point

Boolean

11
Character

String

Operators

As the name suggests, operators perform operations between different entities. Whenever the compiler sees

an operator, it tokenizes it and proceeds further. Java has other operators based on their functionality. In

total, there are eight types of operators in Java.

There are many types of operators in Java which are given below:

Arithmetic Operators

Assignment Operators

Relational Operators

Logical Operators

Unary Operators

Bitwise Operators

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on variables and data..

Operator Operation

+ Addition

- Subtraction

* Multiplication

12
/ Division

% Modulo Operation (Remainder after division)

Assignment Operators

Assignment operators are used in Java to assign values to variables. a=5; Here = is the assignment operator.

It assigns the value on its right to the variable on its left.

Operator Example

= a = b;

+= a += b;

-= a -= b;

*= a *= b;

/= a /= b;

%= a %= b;

Relational Operators

Relational operators are used to check the relationship between two operands .

Here, < operator is the relational operator. It checks if a is less than b or not.

It returns either true or false.

Operator Description

== Is Equal To

13
!= Not Equal To

> Greater Than

< Less Than

>= Greater Than or Equal To

<= Less Than or Equal To

Logical Operators

Logical operators are used to check whether an expression is true or false. They are used in decision making.
Operator Meaning

true only if both expression and


&& (Logical AND)
expression2 are true

true if either expression1 or


|| (Logical OR)
expression2 is true

true if expression is false and


! (Logical NOT)
vice versa

Unary Operators

Unary operators are used with only one operand. For example, ++ is a unary operator that increases the

value of a variable by 1. That is, ++5 will return 6.

Different types of unary operators are:

Operator Meaning

+ Unary plus: not necessary to use since numbers are positive without using it

- Unary minus: inverts the sign of an expression

++ Increment operator: increments value by 1

-- Decrement operator: decrements value by 1

14
! Logical complement operator: inverts the value of a Boolean.

Bitwise Operators

Bitwise operators in Java are used to perform operations on individual bits. For example,

Here, ~ is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).

The various bitwise operators present in Java are:

Operator Description

~ Bitwise Complement

<< Left Shift

>> Right Shift

>>> Unsigned Right Shift

& Bitwise AND

^ Bitwise exclusive OR

Separators

These are the special symbols used to separate java tokens. These are sometimes called punctuators. The

separators have special meaning and thus should not be used for anything else.

Brackets []: These are used to define arrays and represent single and multi-dimension subscripts.

Braces {}: These mark the start and end of multi-line code blocks.

Parenthesis (): Used for function calls and parameters.

Comma (,): These separate statements. For example, separate parameters.

Semicolon ( : ): It invokes the initialization list.

Asterisk (*): This is used to generate pointer variables.

Assignment operator (=): It assigns values to variables. For example, a=10.

15
CONTROL STATEMENT IN JAVA:
Java compiler executes the code from top to bottom. The statements in the code are executed according to the
order in which they appear. However, Java provides statements that can be used to control the flow of Java
code. Such statements are called control flow statements. It is one of the fundamental features of Java, which
provides a smooth flow of program.

Java provides three types of control flow statements.

1. Decision Making statements


o if statements
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue statement

1. Decision Making statements:

Decision making structures have one or more conditions to be evaluated or tested by the
program, along with a statement or statements that are to be executed if the condition is determined to be true,
and optionally, other statements to be executed if the condition is determined to be false.

16
Java programming language provides following types of decision making statements.

Sr. Statement & Description


No.

1 if statement

A if statement consists of a Boolean expression followed by one or more statements.

2 if...else statement

A if statement can be followed by an optional else statement, which executes when the Boolean
expression is false.

3 nested if statement

You can use one if or else if statement inside another if or else if statement(s).

4 switch statement

A switch statement allows a variable to be tested for equality against a list of values.

Loop statements:
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages −

Java programming language provides the following types of loop to handle looping requirements.

17
Sr. Loop & Description

No.

1 while loop

Repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.

2 for loop

Execute a sequence of statements multiple times and abbreviates the code that manages the
loop variable.

3 do...while loop

Like a while statement, except that it tests the condition at the end of the loop body.

2. Jump Statements:

Jump statements change execution from its normal sequence. When execution leaves a scope,
all automatic objects that were created in that scope are destroyed.
Java supports the following control statements.

Sr. Jump Statement & Description

No.

1 break statement

Terminates the loop or switch statement and transfers execution to the statement immediately
following the loop or switch.

2 continue statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to
reiterating.

METHODS
Method in Java or Java Method is a collection of statements that perform some specific task and
return the result to the caller. A Java method can perform some specific task without returning
18
anything. Methods in Java allow us to reuse the code without retyping the code. In Java, every method
must be part of some class that is different from languages like C, C++, and Python.
1. A method is like a function i.e. used to expose the behavior of an object.
2. it is a set of codes that perform a particular task.

Method Declaration

The method declaration provides information about method attributes, such as visibility, return-type, name
and arguments .

Types of Method

There are two types of methods in Java:

o Predefined Method
o User-defined Method

Predefined Method

In Java, predefined methods are the method that is already defined in the Java class libraries is
known as predefined methods. It is also known as the standard library method or built-in method. We
can directly use these methods just by calling them in the program at any point. Some pre-defined methods
are length(), equals(), compare To(), sqrt (), etc. When we call any of the predefined methods in our
program, a series of codes related to the corresponding method runs in the background that is already stored
in the library.

User-defined Method

The method written by the user or programmer is known as a user-defined method. These methods are
modified according to the requirement. We can declare two types of user-defined methods in an application.

1. Instance Method
2. Static Method

19
Instance Method in Java

An instance method is used to implement behaviors of each object/instance of the class. Since the
instance method represents behaviors of the objects. Therefore, instance method is linked with an object.
An instance method is also known as non-static method. So, without creating an object of the class, the
methods cannot exist to perform their desired behaviors or task. It is allocated in the heap memory during
the object creation.

Static Method in Java

When you declare any method with a static modifier, it is called static method in java. A static
method is linked with class.
Therefore, it is also known as a class method. It is used to implement the behavior of the class itself. Static
methods load into the memory during class loading and before object creation.

Method Overloading in Java

If a class has multiple methods having same name but different in parameters, it is known as Method
Overloading.

If we have to perform only one operation, having same name of the methods increases the readability of
the program.

Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you
write the method such as a (int,int) for two parameters, and b (int,int,int) for three parameters then it may be
difficult for you as well as other programmers to understand the behavior of the method because its name
differs.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

1. By changing number of arguments

2. By changing the data type

By changing no. of arguments

In this example, we have created two methods, first add () method performs addition of two numbers and
second add method performs addition of three numbers.

In this example, we are creating static methods so that we don't need to create instance for calling methods.
20
static int add(int a,int b)

static int add(int a,int b,int c)

By changing data type of arguments

In this example, we have created two methods that differs in data type. The first add method receives two
integer arguments and second add method receives two double arguments.

static int add(int a, int b)

static double add(double a, double b)


Method Overriding in Java

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding
in Java.

In other words, If a subclass provides the specific implementation of the method that has been declared by one
of its parent class, it is known as method overriding.

Usage of Java Method Overriding

o Method overriding is used to provide the specific implementation of a method which is already

provided by its super class.

o Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

1. The method must have the same name as in the parent class

2. The method must have the same parameter as in the parent class.

3. There must be an IS-A relationship (inheritance).

Method Overriding Example

class Vehicle{

void run(){System.out.print("Vehicle is running");}

21
}

class Bike2 extends Vehicle{

void run(){

System.out.println("Bike is running safely");}

public static void main(String args[]){

Bike2 obj = new Bike2();

obj.run();

}
}

CLASS AND OBJECT


Classes and Objects are basic concepts of Object Oriented Programming that revolve around real life

entities.

Class:
1. Class is a set of object which shares common characteristics/ behavior and common properties/

attributes.

2. Class is not a real world entity. It is just a template or blueprint or prototype from which objects are

created.

3. Class does not occupy memory.

4. Class is a group of variables of different data types and group of methods.

A class in java can contain:

• data member

• method

• constructor

• nested class and

22
• interface

Object:
It is a basic unit of Object-Oriented Programming and represents real-life entities. A typical Java

program creates many objects, which as you know, interact by invoking methods.

An object consists of :

1. State: It is represented by attributes of an object. It also reflects the properties of an object.

2. Behavior: It is represented by the methods of an object. It also reflects the response of an object

with other objects.

3. Identity: It gives a unique name to an object and enables one object to interact with other objects.

Object and Class Example:

//Java Program to demonstrate having the main method in

//another class

//Creating Student class.

class Student{

int id;

String name;

//Creating another class TestStudent1 which contains the main method

class TestStudent1{

public static void main(String args[]){

Student s1=new Student();

System.out.println(s1.id);

System.out.println(s1.name);

23
ACCESS MODIFIER
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We

can change the access level of fields, constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:

1. Private: The access level of a private modifier is only within the class. It cannot be accessed from

outside the class.

2. Default: The access level of a default modifier is only within the package. It cannot be accessed from

outside the package. If you do not specify any access level, it will be the default.

3. Protected: The access level of a protected modifier is within the package and outside the package

through child class. If you do not make the child class, it cannot be accessed from outside the package.

4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class,

outside the class, within the package and outside the package.

CONSTRUCTER

Java constructors is a terminology used to construct something in our programs. A constructor in Java
is a special method that is used to initialize objects. The constructor is called when an object of a class
is created. It can be used to set initial values for object attributes.
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class
is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is
a special type of method which is used to initialize the object. Every time an object is created using the
new() keyword, at least one constructor is called.

Types of Java constructors

There are two types of constructors in Java:

1. Default constructor (no-arg constructor)

2. Parameterized constructor

Java Default Constructor


24
A constructor is called "Default Constructor" when it doesn't have any parameter.

Example of default constructor

In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of

object creation.

//Java Program to create and call a default constructor

class Bike1{

//creating a default constructor

Bike1(){System.out.println("Bike is created");}

//main method

public static void main(String args[]){

//calling a default constructor

Bike1 b=new Bike1();

Java Parameterized Constructor

A constructor which has a specific number of parameters is called a parameterized constructor.

Example of parameterized constructor

In this example, we have created the constructor of Student class that have two parameters. We can have any
number of parameters in the constructor.

Java Program to demonstrate the use of the parameterized constructor.

class Student4{

int id;

String name;

//creating a parameterized constructor

25
Student4(int i,String n){

id = i;

name = n;

//method to display the values

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

//creating objects and passing values

Student4 s1 = new Student4(111,"Karan");

Student4 s2 = new Student4(222,"Aryan");

//calling method to display the values of object

s1.display();

s2.display();

Constructor Overloading in Java

In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.

Constructor overloading in Java is a technique of having more than one constructor with different parameter
lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the
compiler by the number of parameters in the list and their types.

Example of Constructor Overloading

//Java program to overload constructors

class Student5{

int id;

String name;
26
int age;

//creating two arg constructor

Student5(int i,String n){

id = i;

name = n;

//creating three arg constructor

Student5(int i,String n,int a){

id = i;

name = n;

age=a;

void display(){System.out.println(id+" "+name+" "+age);}

public static void main(String args[]){

Student5 s1 = new Student5 (111,"Karan");

Student5 s2 = new Student5 (222,"Aryan",25);

s1.display ();

s2.display ();

INHERITANCE

27
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent
object. It is an important part of OOPs (Object Oriented programming system).
In the terminology of Java, a class which is inherited is called a parent or super class, and the new class is
called child or subclass

The syntax of Java Inheritance


class Subclass-name extends Super class-name
{
//methods and fields
}

The extends keyword indicates that you are making a new class that derives from an existing class. The
meaning of "extends" is to increase the functionality.

Types of Inheritance in Java


Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves
as a base class for the derived class B.
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class
also acts as the base class for other classes. In the below image, class A serves as a base class for the
derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly
access the grandparent’s members.

3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a super class (base class) for more than one subclass. In the
below image, class A serves as a base class for the derived classes B, C, and D.
4. Multiple Inheritance (Through Interfaces)
In Multiple inheritances, one class can have more than one super class and inherit features from all parent
classes. Please note that Java does not support multiple inheritances with classes. In java, we can achieve
multiple inheritances only through Interfaces. In the image below, Class C is derived from interfaces A
and B.

5. Hybrid Inheritance(Through Interfaces)


It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritances
with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance
only through Interfaces.

ABSTRACT CLASS
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract
methods. It needs to be extended and its method implemented. It cannot be instantiated.

28
Points to Remember

o An abstract class must be declared with an abstract keyword.

o It can have abstract and non-abstract methods.

o It cannot be instantiated.

o It can have constructors and static methods also.

o It can have final methods which will force the subclass not to change the body of the method .

Example of an abstract class:

abstract class Sunstar {

abstract void printInfo();

class Employee extends Sunstar {

void printInfo() {

String name = "avinash";

int age = 21;

float salary = 222.2F;

System.out.println(name);

System.out.println(age);

System.out.println(salary);

class Base {

public static void main(String args[]) {

Sunstar s = new Employee();

s.printInfo();

29
}

INTERFACE
An Interface in Java programming language is defined as an abstract type used to specify the behavior
of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants
and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the
Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in Java. In
other words, you can say that interfaces can have abstract methods and variables. It cannot have a method
body.

Why do we use an Interface?

• It is used to achieve total abstraction.


• Since java does not support multiple inheritances in the case of class, by using an interface it can
achieve multiple inheritances.
• Any class can extend only 1 class but can any class implement infinite number of interface.
• It is also used to achieve loose coupling.
• Interfaces are used to implement abstraction. So the question arises why use interfaces when we
have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas variables in the interface are
final, public and static.

// Java program to show the interfaces

interface In1

final int a = 10;

default void display()

System.out.println ("hello");

// A class that implements the interface.

class TestClass implements In1

public static void main (String[] args)

30
{

TestClass t = new TestClass();

t.display();

ARRAY
Java array is a collection of similar type of elements which has contiguous memory location.

Java array is an object which contains elements of a similar data type. Additionally, The elements of an array
are stored in a contiguous memory location. It is a data structure where we store similar elements. We can
store only a fixed set of elements in a Java array.

Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on
1st index and so on.

Types of Array in java

There are two types of array.

o Single Dimensional Array

o Multidimensional Array

Single Dimensional Array in Java

//Java Program to illustrate how to declare, instantiate, initalize //and traverse the Java array.

class Testarray{

public static void main(String args[]){

int a[]=new int[5];//declaration and instantiation

a[0]=10;//initialization

a[1]=20;

a[2]=70;

a[3]=40;

a[4]=50;
31
//traversing array

for(int i=0;i<a.length;i++)//length is the property of array

System.out.println(a[i]);

}}

Multidimensional Array

In such case, data is stored in row and column based index (also known as matrix form).

Example to Multidimensional Array

//Java Program to illustrate the use of multidimensional array

class Testarray3{

public static void main(String args[]){

//declaring and initializing 2D array

int arr[][]={{1,2,3},{2,4,5},{4,4,5}};

//printing 2D array

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

System.out.print(arr[i][j]+" "); }

System.out.println(); } }}

JAVA PACKAGES
A java package is a group of similar types of classes, interfaces and sub-packages..

The package keyword is used to create a package in java.

package mypack;

public class Simple{


public static void main(String args[]){
System.out.println("Welcome to package");
32
} }

Types of packages:

Advantage of Java Package

1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

EXCEPTION HANDLING
Exception Handling in Java is one of the effective means to handle the runtime errors so that the regular
flow of the application can be preserved. Java Exception Handling is a mechanism to handle runtime
errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc .

Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. An exception
normally disrupts the normal flow of the application; that is why we need to handle exceptions.

Types of Java Exceptions

There are mainly two types of exceptions: checked and unchecked. An error is considered as the unchecked
exception. However, according to Oracle, there are three types of exceptions namely:

1. Checked Exception

2. Unchecked Exception

3. Error

Try catch block

33
public class JavaExceptionExample{

public static void main(String args[]){


try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
}
}
PROJECT
//BANK MANAGEMENT SYSTEM
import java.util.Scanner;

public class BankApplication {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter your 'Name' ,'mobile No','acc no',and 'IFSE' code to access


your Bank account:");

String name=sc.nextLine();

String mobileNo =sc.nextLine();

String accNo=sc.nextLine();

String IFSEcode =sc.nextLine();

BankAccount obj1=new BankAccount(name,mobileNo,accNo,IFSEcode);

obj1.menu(); }}

class BankAccount{

double bal, prevTrans;

String customerName, mobileNo,accNo,IFSEcode;

BankAccount(String customerName,String mobileNo,String accNo,String IFSEcode){

this.customerName=customerName;

this.mobileNo=mobileNo;

34
this.accNo=accNo;

this.IFSEcode=IFSEcode;

void deposit(double amount){

if(amount!=0){

bal+=amount;

prevTrans=amount;

}}

void withdraw(double amt){

if(amt!=0 && bal>=amt){

bal-=amt;

prevTrans=-amt;

else if(bal<amt){

System.out.println("Bank balance insufficient"); }}

void getPreviousTrans(){

if(prevTrans>0){

System.out.println("Deposited: "+prevTrans); }

else if(prevTrans<0)

{ System.out.println("Withdrawn: "+Math.abs(prevTrans)); }

else{ System.out.println("No transaction occured") }}

void menu1(){

System.out.println("a) Check Balance");

System.out.println("b) Deposit Amount");

System.out.println("c) Withdraw Amount");

System.out.println("d) Previous Transaction");

System.out.println("e) Exit");

void menu(){
35
char option;

Scanner sc=new Scanner(System.in);

System.out.println("hello "+customerName);

System.out.println(" what do you want ?");

menu1();

do{

System.out.println("Choose an option");

option=sc.next().charAt(0);

switch (option){

case 'a':

System.out.println("Balance ="+bal);

menu1();

break;

case 'b':

System.out.println("Enter a amount to deposit :");

double amt=sc.nextDouble();

deposit(amt);

menu1();

break;

case 'c':

System.out.println("Enter a amount to Withdraw :");

double amtW=sc.nextDouble();

withdraw(amtW);

menu1();

break;

case 'd':

System.out.println("Previous Transaction:");

getPreviousTrans();

menu1();
36
break;

case 'e':

break;

default:

System.out.println("Choose a correct option to proceed");

break; }

}while(option!='e');

System.out.println("Thank you for using our banking services");

}}

BANK MANAGEMENT SYSTEM


OUTPUT
Enter your 'Name' ,'mobile No','acc no',and 'IFSE' code to access your Bank account:
nikhil
8249502549
36977743998
SBIN0112345
hello nikhil
what do you want ?
a) Check Balance
b) Deposit Amount
c) Withdraw Amount
d) Previous Transaction
e) Exit
Choose an option
b
Enter a amount to deposit :
12000
a) Check Balance
b) Deposit Amount
c) Withdraw Amount
d) Previous Transaction
e) Exit
Choose an option
c
Enter a amount to Withdraw :
5000
a) Check Balance
b) Deposit Amount
c) Withdraw Amountd) Previous Transaction
e) Exit
Choose an option
a
Balance =7000.0
a) Check Balance
37
b) Deposit Amount
c) Withdraw Amount
d) Previous Transaction
e) Exit
Choose an option
e
Thank you for using our banking services

ADVANTAGES OF JAVA

1. Platform independent: Java code can run on any platform that has a Java Virtual Machine (JVM)
installed, which means that applications can be written once and run on any device.
2. Object-Oriented: Java is an object-oriented programming language, which means that it follows the
principles of encapsulation, inheritance, and polymorphism.
3. Security: Java has built-in security features that make it a secure platform for developing
applications, such as automatic memory management and type checking.
4. Large community: Java has a large and active community of developers, which means that there is a
lot of support available for learning and using the language.
5. Enterprise-level applications: Java is widely used for developing enterprise-level applications, such
as web applications, e-commerce systems, and database systems .

DISADVANTAGES OF JAVA

1. Performance: Java can be slower compared to other programming languages, such as C++, due to its
use of a virtual machine and automatic memory management.
2. Memory management: Java’s automatic memory management can lead to slower performance and
increased memory usage, which can be a drawback for some applications.

CONCLUSION
Java offers the real possibility that most programs can be written in a type-safe language. However,
for Java to be broadly useful, it needs to have more expressive power than it does at present. This paper
addresses one of the areas where more power is needed. It extends Java with a mechanism for parametric
polymorphism, which allows the definition and implementation of generic abstractions. The paper gives a
complete design for the extended language. The proposed extension is small and conservative and the paper
discusses the rationale for many of our decisions. The extension does have some impact on other parts of
Java, especially Java arrays, and the Java class library. The paper also explains how to implement the
extensions. We first sketched two designs that do not change the JVM, but sacrifice some space or time
performance. Our implementation avoids these performance problems. We had three main goals: to allow all
instantiations to share the same bytecodes (avoiding code blowup), to have good performance when using
parameterized code, and to have little impact on the performance of code that does not use parameterization.
The implementation discussed in Section 3 meets these goals. In that section, we described some small
extensions to the virtual machine specification that are needed to support parameterized abstractions; we
also described the designs of the bytecode verifier and interpreter, and the runtime

38
39

You might also like