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

An Introduction to Java-1

Uploaded by

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

An Introduction to Java-1

Uploaded by

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

Introduction to Java (Theory Notes)

BS-Islamiat III semester Page |1

What is Object Oriented Programming (OOP)?


Object Oriented Programming (OOP) is a programming paradigm that focuses on the use
of objects to represent and manipulate data. In OOP, data is encapsulated within objects,
and objects are defined by their properties (attributes) and behaviors (methods). OOP
provides several key concepts that enable developers to write modular, reusable, and
maintainable code.

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.

Definition of OOP Concepts in Java


The main ideas behind Java’s Object-Oriented Programming, OOP concepts
include abstraction, encapsulation, inheritance, and polymorphism.
Java defines OOP concepts as follows:

 Abstraction. Abstraction means simple things like objects, classes,


and variables represent more complex basic code and data. This is
important because it avoids to repeating the same work multiple times.
 Encapsulation. The practice of keeping data within a class private, then
providing access to those fields via public methods. Encapsulation is used
to keeps the data and code safe within the class.
 Inheritance. Inheritance is a programming technique that is used to reuse
an existing class to build a new class. The new class inherits all the
behavior of the 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

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester Page |2

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.

How OOP Concepts in Java Work


OOP concepts in Java work by letting programmers create components that
are reusable in different ways while maintaining security.
How Abstraction Works
A programmer can create several different types of objects, which can be variables,
functions or data structures. Programmers can also create different classes of objects as
ways to define the objects. We can achieve abstraction using two main mechanisms:
abstract classes and interfaces.

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.

Abstract class Interface


1) Abstract class can have abstract and non- Interface can have only abstract methods.
abstract methods. Since Java 8, it can have default and static
methods also.
2) Abstract class doesn't support multiple Interface supports multiple inheritance.
inheritance.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation
implementation of interface. of abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
Prof. Usman Ahmad.
Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester Page |3

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();
} }

How Encapsulation Works


Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting
on the data (methods) together as a single unit. In encapsulation, the variables of a class
will be hidden from other classes, and can be accessed only through the methods of their
current class. Encapsulation provides several benefits, including:

1. Data hiding: By hiding the implementation details of a class, encapsulation


protects the data from unauthorized access and manipulation.
2. Modularity: Encapsulation helps to break down complex systems into
smaller, more manageable components.
3. Flexibility: Encapsulation allows for changes to the internal implementation
without affecting the external interface.

Access Modifiers
In Java, encapsulation is implemented using access modifiers, which control the visibility
of variables and methods within a class.

The three access modifiers in Java are:

1. Public: Public variables and methods can be accessed from anywhere,


including outside the class.
2. Private: Private variables and methods can only be accessed within the class
they are defined in.
3. Protected: Protected variables and methods can be accessed within the
same class and its subclasses.

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester Page |4

How Inheritance Works


Inheritance is another OOP concept that works by letting a new class adopt the properties
of another. We call the inheriting class a subclass or a child class. The original class is
often called the parent or the superclass. We use the keyword extends to define a new
class that inherits properties from an old class. The subclass inherits all the public and
protected variables and methods of the superclass, and it can also define its own variables
and methods.

Benefits of Inheritance
Inheritance provides several benefits, including:

1. Reusability: By inheriting from a superclass, a subclass can reuse the code


and functionality already defined in the superclass.
2. Polymorphism: Inheritance allows for polymorphism, where objects of
different subclasses can be treated as objects of the same superclass.
3. Flexibility: Inheritance provides a way to add new features to an existing
class hierarchy without modifying the existing code.

How Polymorphism Works


There are two ways to implement polymorphism in Java. Overriding and Overloading.

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.

Many in-built packages are available in Java,


including util, lang, awt, javax, swing, net, io, sql, etc. We can import all members of a
package using packagename.* statement.

Types of Packages in Java


Built-in 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

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester Page |6

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.

Difference Between final finally and finalize in Java


The main difference between final finally and finalize in Java are shown in table below.

Basis of final finally finalize


Comparison

Definition final is a keyword finally is a block used finalize is a method in


used in Java to in Java to ensure that Java used to perform
restrict the a section of code is cleanup processing on
modification of a always executed, an object before it is
variable, method, or even if an exception garbage collected.
class. is thrown.

Usage final is used to finally is used after a finalize is used to


declare a variable, try or catch block to perform cleanup
method, or class as execute code that operations on an
unchangeable. must be run object, such as closing
regardless of whether files or releasing other
an exception is resources, before it is
thrown or not. garbage collected.

Execution final is executed at finally is executed at finalize is executed by


compile-time. runtime. the garbage collector
before an object is
destroyed.

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester Page |7

Define java application and JApplet.

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.

Difference between Application and Applet:

Parameters Java Application Java Applet

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

What is Array in java?


An array is a block of container that can hold data of one single type. The array is a fundamental
construct in Java that allows you to store and access a large number of values conveniently.

Syntax:
dataType[] arrayName;
Example: int[] data;

What is String in java?


Basically, the string is a sequence of characters but it’s not a primitive type. When we create a
string in java, it actually creates an object of type String. The string is an immutable object
which means that it cannot be changed once it is created.
Syntax:
String variable-name= “Value”;
String name = “Pakistan”;

What is Vector class in java?


Java Vector class comes under the java.util package. The vector class implements a growable
array of objects. Like an array, it contains the component that can be accessed using an integer
index.
Vector is very useful if we don’t know the size of an array in advance or we need one that can
change the size over the lifetime of a program.

Java Vector Class Declaration


public class Vector<E>
1) add() — It is used to append the specified element in the given vector.
2) capacity() — It is used to get the current capacity of this vector.
3) clear() — It is used to delete all of the elements from this vector.
4) clone() — It returns a clone of this vector.
5) copyInto() — It is used to copy the components of the vector into the specified array.
6) elementAt() — It is used to get the component at the specified index.

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.

Why Should We Use JDBC


Before JDBC, ODBC API was the database API to connect and execute the query with the
database. But, ODBC API uses ODBC driver which is written in C language (i.e. platform
dependent and unsecured). That is why Java has defined its own API (JDBC API) that uses
JDBC drivers (written in Java language).

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester Page |9

We can use JDBC API to handle database using Java program and can perform the following
activities:

1. Connect to the database


2. Execute queries and update statements to the database
3. Retrieve the result received from the database.

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.

Commonly used methods of Statement interface:


The important methods of Statement interface are as follows:
1. public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the
object of ResultSet.
2. public int executeUpdate(String sql): is used to execute specified query, it may be create, drop,
insert, update, delete etc.
3. public boolean execute(String sql): is used to execute queries that may return Multiple results.
4. public int[] executeBatch(): is used to execute batch of commands.
Java Database Connectivity with 5 Steps
There are 5 steps to connect any java application with the database using JDBC. These steps are as
follows:
o Register the Driver class
o Create connection
o Create statement
o Execute queries
o Close connection
Prof. Usman Ahmad.
Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester P a g e | 10

Example to Connect Java Application with mysql database


1. import java.sql.*;
2. class MysqlCon{
3. public static void main(String args[]){
4. try{
5. Class.forName("com.mysql.jdbc.Driver"); // Register the Driver class
6. Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bsis","root","root");
7. //here bsis is database name, root is username and password
8. Statement stmt=con.createStatement(); //Create statement
9. ResultSet rs=stmt.executeQuery("select * from emp"); //Execute Query and Get the result
10. while(rs.next())
11. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
12. con.close();
13. }catch(Exception e){
14. System.out.println(e);
15. }
16. }
17. }

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.

 Enrich semantic content by including <header> <footer>, <article>, <section> and


<figure> are added.

 Drag and Drop- The user can grab an object and drag it further dropping it to a new
location.

 Geo-location services- It helps to locate the geographical location of a client.


Prof. Usman Ahmad.
Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester P a g e | 11

 Web storage facility which provides web application methods to store data on the web
browser.

 Uses SQL database to store data offline.

 Allows drawing various shapes like triangle, rectangle, circle, etc.

 Capable of handling incorrect syntax.

How HTML5 is different from older HTML?


HTML5 is the next version of HTML. Here, we will get some brand-new features that will make
HTML much easier. These new introducing features make your website layout clearer to both
website designers and users.

List of HTML 5 Tags

Tag Description

<article> This element is used to define an independent piece of content in


that may be a blog, a magazine or a newspaper article.

<audio> It is used to play audio file in HTML.

<canvas> It is used to draw canvas.

<dialog> It defines a window or a dialog box.

<figure> It defines a self-contained content like photos, diagrams etc.

<footer> It defines a footer for a section.

<header> It defines a header for a section.

<mark> It specifies the marked or highlighted content.

<menuitem> It defines a command that the user can invoke from a popup menu.

<time> It is used to define a date/time.

<video> It is used to play video file in HTML.

<wbr> It defines a possible line break.

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester P a g e | 12

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.

HTML5 Geo Location Service Method:


Var geolocation = navigator.geolocation;
getCurrentPosition(): This method retrives the current geographic location.
watchPosition(): This method retrieves periodic update about the current geographic location
of the device.
clearWatch(): This method cancels an ongoing watchPosition call.

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester P a g e | 13

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.

Front-end web technologies:


Front-end is a term that involves the building of webpages and user interfaces for web-
applications. It implements the structure, design, behavior, and animation of everything you
see on the screen when you open up websites, web applications, or mobile apps. The core 3
technologies that all modern front-end web developers work to master are HTML, CSS
(Cascading Style Sheets), and JavaScript are examples of web technologies to develop
static www.
HTML (Initial release -1993)
HTML or Hypertext Markup Language is necessary for designing the front-end of a website
using a markup language. HTML is a combination of markup language and hypertext.
Hypertext defines the links between two or more pages. The markup language is necessary for
defining the text documentation within the tag, which in turn defines the web page’s structure.
HTML is used for building the foundation of our website. Popular application in HTML are:
 LinkedIn
 Google
 Docs
CSS (Initial release – 1996)
Cascading Style Sheets or CSS is a simply designed programming language that helps to
simplify the process of making web pages presentable. With CSS, you can apply a style to
web pages. Moreover, you can use CSS independently without relying on HTML that makes
up each web page. Popular application in CSS are:
 Facebook
 Google
 Slides

JavaScript (Initial release -1995)


JavaScript is essential for front-end development. But, the possibilities of JavaScript are
endless. It is one of the most multidimensional programming languages that helps you develop
web, desktop, and mobile apps.
Prof. Usman Ahmad.
Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester P a g e | 14

If we want to become a front-end developer, JavaScript is a prerequisite. With JavaScript, we


can make the site interactive for the website user. The programming language enhances the
functionality of a website for running web-based software and cool games.

Popular application in javascript are:


 Netflix
 Candy Crush
 Facebook

Back-end web technologies:


The back end refers to parts of a computer application or a program's code that allow it to
operate and that cannot be accessed by a user. Most data and operating syntax are stored and
accessed in the back end of a computer system. Typically the code is comprised of one or more
programming languages. Leading back-end technologies are Java, JavaScript, PHP (Personal
Home Page), Python, JSP.net and Node.JS are examples of web technologies and are used
to develop dynamic www.
Java (Initial release -1995)
Java is 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

JavaScript (Initial release -1995)


JavaScript is essential for front-end development, but the programming language is equally
essential for back-end development. It is one of the most multidimensional programming languages that
helps you streamline application development and improve user experience.
Popular application in JavaScript are:
 Netflix
 Candy Crush
 Facebook
PHP (Initial release – 1995)
PHP is a server-side technology and general-purpose scripting language that is easy to use
and amend information in the databases. The inclusion of multiple modern frameworks, robust
code base, massive community, and easy deployment adds excellent value to this web
technology. The technology is cost-effective since it is in a free and open-source community.
Popular application in PHP are:
 WordPress
 Wikipedia
 Yahoo!

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester P a g e | 15

Python (Initial release -1991)


Python deserves to be included in the best back-end web development technologies available.
The technology delivers multiple standard libraries that simplify the process of web
development. Popular applications in Python are:
 Instagram
 Google
 Spotify

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.

Why JSP is more useful than other server-side technologies?

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

Different Classes in Java:


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.
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.
Interface Class:
 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.
Final Class in Java:
 The final class is a class that is declared with the final keyword.
 We can restrict class inheritance by making use of the final class.
 Final classes cannot be extended or inherited.
 If we try to inherit a final class, then the compiler throws an error during compilation.
 We can simply define a final class using the final keyword and can write the class body
code according to our needs.
 As the final class is immutable and can't be inherited, it has some advantages such as
immutability and security.
Static Class in java:

 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.

What is inheritance? How many types of inheritance are there in Java?


Inheritance is a programming technique that is used to reuse an existing
class to build a new class. The new class inherits all the behavior of the
Prof. Usman Ahmad.
Govt. Islamia Graduate College, Gujranwala.
Introduction to Java (Theory Notes)
BS-Islamiat III semester P a g e | 17

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.

Prof. Usman Ahmad.


Govt. Islamia Graduate College, Gujranwala.

You might also like