An Introduction to Java-1
An Introduction to Java-1
Class:
Class is a user-defined datatype that contains its own data members and member
functions. The member functions and data members can be accessed with the help of
objects. It is the primary concept of object-oriented programming. A class is used to
organize information or data so a programmer can reuse the elements in multiple
instances.
Object:
An object is an instance of a class. An object in OOP is a component that consists of
properties to make a particular data useful. For example, let’s consider a class Student.
We can access various student details using some common property attributes
like student name, roll number, etc.
class that inherits the properties and function of an existing class is known
as subclass, derived class or child class.
Polymorphism. Poly means many and morphism means forms. In OOP,
polymorphism is the ability of objects to behave in multiple ways. One
form of polymorphism is method overloading. That’s when the values of
the supplied variables imply different meanings. The other form is method
overriding. That’s when the code itself implies different meanings.
1. Abstract Classes: An abstract class is a class that you can’t instantiate and
can only extend by subclasses. Abstract classes can have both abstract and
non-abstract methods. Abstract methods do not have a body and you must
implement them by any subclass that extends the abstract class. Non-
abstract methods have a body and you can directly call them.
2. Interfaces: An interface is a collection of methods. You can use it to define a
set of behaviors that a class should implement. A class can implement
multiple interfaces, and all the methods defined in an interface.
6) An abstract class can extend another Java An interface can extend another Java
class and implement multiple Java interfaces. interface only.
7) An abstract class can be extended using An interface can be implemented using
keyword "extends". keyword "implements".
8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Access Modifiers
In Java, encapsulation is implemented using access modifiers, which control the visibility
of variables and methods within a class.
Benefits of Inheritance
Inheritance provides several benefits, including:
Method overriding: The process of declaring members function in derived class with
same name and same parameters as in parent class is known as method overriding.
Method overloading: The process of declaring members function with same name but
different parameters is known as method overloading.
Benefits of Polymorphism
Polymorphism provides several benefits, including:
1. Flexibility: It allows for more flexible and adaptable code by enabling objects
of different classes to be treated as if they are of the same class.
2. Code reuse: Code is reused by allowing classes to inherit functionality from
other classes and to share common methods and properties.
3. Simplification: Polymorphism simplifies code by enabling the use of generic
code that can handle different types of objects.
Prof. Usman Ahmad.
Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester Page |5
Packages in java
A Java package is a collection of similar types of sub-packages, interfaces, and classes. In
Java, there are two types of packages: built-in packages and user-defined packages.
The package keyword is used in Java to create Java packages.
When we install Java into a personal computer or laptop, many packages get installed
automatically. They all are unique on their own and have the capabilities to handle
multiple tasks. Due to this, we don’t have to start building everything from scratch. Some
of the examples of built-in packages are listed below.
Java.lang
o lang is one of the built-in packages provided by Java that contains classes
and interfaces fundamental to the design of the Java programming language.
The lang packages contain classes such as Boolean, Integer, Math, etc., that
are considered the building blocks of a Java program.
Java.io
o Java I/O (Input and Output) is used to process the input and produce the
output. Java uses the concept of a stream to make I/O operation fast. The
java.io package contains all the classes required for input and output.
Java.util
o It contains various utility classes and interfaces. It contains Java's collections
framework, date & time utilities, string-tokenizer, event-model utilities, etc.
Java.applet
o Java.applet is a built-in package that contains to create a java applet.
Applet is a special type of program that is embedded in the webpage. It runs
inside the browser and works at client side.
Java.awt
o AWT stands for Abstract window toolkit is an Application programming
interface (API) for creating Graphical User Interface (GUI) in Java. It allows
Java programmers to develop window-based applications. AWT provides
various components like button, label, checkbox, etc. used as objects inside a
Java Program.
Java.net
o Java.net is a package that provides a set of classes as well as interfaces for
networking in Java. Some of the classes are URL class, URLConnection
class, Socket class, ServerSocket class etc.
User-defined packages
User-defined packages are those that developers create to add different needs of
applications. In simple terms, User-defined packages are those that the users define.
Inside a package, you can have Java files like classes, interfaces, and a package.
Java Application is just like a Java program that runs on a basic operating system with the
support of a virtual machine. It is also known as an application program. The graphical user
interface is not necessary to execute the java applications, it can be run with or without it.
Java Applet is a Java program that can be embedded into a web page. It runs inside the web
browser and works on the client-side. An applet is embedded in an HTML page using
the APPLET or OBJECT tag and hosted on a web server. Applets are used to make the
website more dynamic and entertaining.
Definition Applications are just like a Java Applets are small Java programs that are
program that can be executed designed to be included with the HTML
independently without using the web document. They require a Java-
web browser. enabled web browser for execution.
main () The application program requires The applet does not require the main()
method a main() method for its execution. method for its execution instead init()
method is required.
Compilation The “javac” command is used to Applet programs are compiled with the
compile application programs, “javac” command and run using either the
which are then executed using the “appletviewer” command or the web
“java” command. browser.
File access Java application programs have Applets don’t have local disk and network
full access to the local file system access.
and network.
Access level Applications can access all kinds Applets can only access browser-specific
of resources available on the services. They don’t have access to the
system. local system.
Run It cannot run on its own; it needs It cannot start on its own, but it can be
JRE to execute. executed using a Java-enabled web
browser.
What is JRE?
The Java Runtime Environment, or JRE, is a software layer that runs on top of a computer's
operating system software and provides the class libraries and other resources that a specific
Java program needs to run.
Prof. Usman Ahmad.
Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester Page |8
Syntax:
dataType[] arrayName;
Example: int[] data;
JDBC
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the
query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC
drivers to connect with the database.
We can use JDBC API to handle database using Java program and can perform the following
activities:
What is API
API (Application programming interface) is a document that contains a description of all
the features of a product or software. It represents classes and interfaces that software
programs can follow to communicate with each other. An API can be created for applications,
libraries, operating systems, etc.
Connection interface
A Connection is a session between a Java application and a database. It helps to establish a connection
with the database. The Connection interface provide many methods for transaction management like
commit (), rollback(), setAutoCommit(), setTransactionIsolation(), etc.
Statement interface
The Statement interface provides methods to execute queries with the database. The statement
interface is a factory of ResultSet i.e. it provides factory method to get the object of ResultSet.
Introduction to HTML5:
HTML 5 is the fifth and current version of HTML. HTML5 is not only a new version of HTML
language enriched with new elements and attributes, but a set of technologies for building
more powerful and diverse web sites.
Features:
It has introduced new multimedia features which supports both audio and video controls
by using <audio> and <video> tags.
There are new graphics elements including vector graphics and tags.
Drag and Drop- The user can grab an object and drag it further dropping it to a new
location.
Web storage facility which provides web application methods to store data on the web
browser.
Tag Description
<menuitem> It defines a command that the user can invoke from a popup menu.
HTML5 Storage:
The two storages are session storage and local storage and they would be used to
handle different situations.
Session Storage
It is used to store data on the client-side. Data in the SessionStorage exist till the
current tab is open. If we close the current tab then our data will also erase automatically from
the SessionStorage.
SessionStorage.setItem(“key”,”value”);
SessionStorage.getItem(“key”);
Local Storage
It is used to store data on the client-side. It has no expiration time, so the data in the
LocalStorage exists always till the user manually deletes it.
LocalStorage.setItem(“key” , “values”); // for storing data in web storage.
LocalStorage.getItem(“key” ); // for getting data from web storage.
Define Tool:
A tool is a physical item that can be used to achieve a goal. This is used to describe a
procedure with a specific purpose. A tool can be a physical object such as a machine tool
including a technical object such as a web authoring tool or software program.
Text editors such as Notepad++, TextWrangler, and Sublime are examples of tools. Another
tool Dreamweaver can also be used to speed up your development process.
Define Technology:
The methods by which computers communicate with each other through the use of markup
languages and multimedia packages is known as web technology.
What is JSP?
Java Server Pages (JSP) is a server-side programming technology that enables the
creation of a dynamic, platform-independent method for building Web-based applications. JSP
has access to the entire family of Java APIs, including the JDBC API to access enterprise
databases.
As mentioned before, JSP is one of the most widely used language over the web. Following
are the useful application of JSP.
JSP vs. Active Server Pages (ASP)
The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic
or other MS specific language, so it is more powerful and easier to use. Second, it is portable
to other operating systems and non-Microsoft Web servers.
JSP vs. Pure Servlets
It is more convenient to write (and to modify!) regular HTML than to have plenty of println
statements that generate the HTML.
JSP vs. JavaScript
JavaScript can generate HTML dynamically on the client but can hardly interact with the web
server to perform complex tasks like database access and image processing etc.
JSP vs. Static HTML
Regular HTML, of course, cannot contain dynamic information.
What is Java and what are the different Classes in java?
Java is released in 1995 and one of the most proficient and widespread programming
languages that has been around for a long time. It is a robust back-end technology. It has
many perks and benefits that help developers easily solve difficult real-time problems.
Java not only makes feature-rich and adaptable web applications, but you can also use Java
for mobile devices and microcontroller software development. Popular application in Python
are:
NASA WorldWind
Jenkins
Hadoop
Prof. Usman Ahmad.
Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester P a g e | 16
The static keyword is used to create a static class and helps in memory management.
Static class in Java is a nested class and it doesn't need the reference of the outer class.
Static class can access only the static members of its outer class.
Static class cannot access the non-static member of the outer classes.
Inner classes can access the static and the non-static members of the outer class.
We can create an instance of the static nested class without creating an instance of the
outer class.
original class. The existing class that is reused to create a new class is known
as supper class, base class or parent class. The existing class that inherits the
properties and function of an existing class is known as subclass, derived
class or child class.
Single inheritance
Single inheritance is the simplest type of inheritance in java. In this, a
class inherits the properties from a single class.
Multi-level inheritance
In Multi-Level Inheritance in Java, a class extends to another class that
is already extended from another class. For example, if there is a class A that
extends class B and class B extends from another class C, then this scenario is
known to follow Multi-level Inheritance.
Multiple inheritance
Multiple inheritance in java is the capability of creating a single class
with multiple super classes. Unlike some other popular object oriented
programming languages like C++, java doesn't provide support for multiple
inheritance in classes.
Hybrid inheritance
Hybrid Inheritance in Java is a combination of inheritance. In this type
of Inheritance, more than one kind of inheritance is observed. For example, if
we have class A and class B that extend class C and then there is another class
D that extends class A, then this type of Inheritance is known as Hybrid
Inheritance.