0% found this document useful (0 votes)
34 views33 pages

Java Unit 1

Java is an object-oriented programming language developed by Sun Microsystems. It is a platform independent language that is widely used for both desktop and mobile applications as well as web applications. The key concepts in Java include classes, objects, inheritance, interfaces, packages and encapsulation. Classes define the structure and behavior of objects. Objects are instances of classes. Inheritance allows classes to inherit attributes and behaviors from parent classes. Interfaces define behaviors without implementation. Packages organize related classes. Encapsulation hides implementation details and controls access to objects.

Uploaded by

veyide7506
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views33 pages

Java Unit 1

Java is an object-oriented programming language developed by Sun Microsystems. It is a platform independent language that is widely used for both desktop and mobile applications as well as web applications. The key concepts in Java include classes, objects, inheritance, interfaces, packages and encapsulation. Classes define the structure and behavior of objects. Objects are instances of classes. Inheritance allows classes to inherit attributes and behaviors from parent classes. Interfaces define behaviors without implementation. Packages organize related classes. Encapsulation hides implementation details and controls access to objects.

Uploaded by

veyide7506
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Java - Introduction

What is JAVA

• Java is a programming language and a platform. Java is a high level, robust, object-
oriented and secure programming language.
• Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the
year 1995. James Gosling is known as the father of Java. Before Java, its name
was Oak. Since Oak was already a registered company, so James Gosling and his team
changed the Oak name to Java.
• 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 Applications

According to Sun, 3 billion devices run Java. There are many devices where Java
is currently used. Some of them are as follows:
• Desktop Applications such as acrobat reader, media player, antivirus, etc.
• Web Applications such as irctc.co.in, javatpoint.com, etc.
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games, etc.
Types of Java Applications

1) Standalone Application
Standalone applications are also known as desktop applications or window-based
applications. These are traditional software that we need to install on every machine.
Examples of standalone application are Media player, antivirus, etc. AWT and Swing are
used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is called a
web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc.
technologies are used for creating web applications in Java
Types of Java Applications

3) Enterprise Application
An application that is distributed in nature, such as banking applications, etc. is
called enterprise application. It has advantages of the high-level security, load balancing,
and clustering. In Java, EJB is used for creating enterprise applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile application.
Currently, Android and Java ME are used for creating mobile applications.
Java Platforms/Editions
There are 4 platforms or editions of Java:
1) Java SE (Java Standard Edition)
It is a Java programming platform. It includes Java programming APIs
such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. It
includes core topics like OOPs, String, Regex, Exception, Inner classes,
Multithreading, I/O Stream, Networking, AWT, Swing, Reflection,
Collection, etc.
2) Java EE (Java Enterprise Edition)
It is an enterprise platform which is mainly used to develop web and
enterprise applications. It is built on the top of the Java SE platform. It
includes topics like Servlet, JSP, Web Services, EJB, JPA, etc.
Java Platforms/Editions

3) Java ME (Java Micro Edition)


It is a micro platform which is mainly used to develop mobile
applications.
4) JavaFX
It is used to develop rich internet applications. It uses a light-
weight user interface API.
Features of Java

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.
Features of Java

Simple
Object-
Dynamic
oriented

Distributed Platform

Multithrea Features Portable-


independen
ded
of Java t

High
Performan Secured
ce

Interpreted Robust
Architectur
al Neutral
How to set path in Java

set path=C:\Program Files\Java\jdk1.6.0_23\bin


Object Oriented Programming

If you've never used an object-oriented programming


language before, you'll need to learn a few basic concepts
before you can begin writing any code. This lesson will
introduce you to objects, classes, inheritance, interfaces, and
packages. Each discussion focuses on how these concepts
relate to the real world.
What Is a Class?
A class is a blueprint (It is user defined data types it could be anything) or
prototype from which objects are created. This section defines a class that
models the state and behavior of a real-world object. It intentionally
focuses on the basics, showing how even simple classes can cleanly model
state and behavior.

E.g.
class Demo {
public static void main (String args[]) {
System.out.println("Welcome to Java”);

}
}
What Is an Object?

An object is a software bundle of related state and behavior.


Software objects are often used to model the real-world
objects that you find in everyday life (Object is real world
Entity to represent a physical instance of a Class). A software
object maintains its state in variables and implements its
behavior with methods.

E.g.
What Is a Package?
A Java package is a mechanism for organizing Java classes into
namespaces similar to the modules of Modula. Java packages
can be stored in compressed files called JAR files, allowing
classes to download faster as a group rather than one at a time.
Programmers also typically use packages to organize classes
belonging to the same category or providing similar
functionality.
•A package provides a unique namespace for the types it
contains.
•Classes in the same package can access each other's
package-access members.

E.g:-
import java.lang.*;

import java.util.*;

import java.io.*;

import java.awt.*;
What Is Inheritance?

Inheritance provides a powerful and natural mechanism for


organizing and structuring your software. Now we will
explain how classes inherit state and behavior from their
super classes, and explains how to derive one class from
another using the simple syntax provided by the Java
programming language.
E.g. Single Inheritance
class A
{
//statements;
}
class B extends A
{
public static void main (String ar[])
{
System.out.println ("Welcome to Java Programming");
}
}
E.g. : Multilevel Inheritance
class A
{
//statements;
}
class B extends A
{
//statements;
}
class C extends B
{
//statements;
public static void main(String ar[])
{
//statements
}
}
E.g. Hierarchal Inheritance

class A
{
//statements;
}
class B extends A
{
//statements;
}
class C extends A
{
public static void main(String ar[])
{
//statements;
}
}
What is an Abstraction?

Abstraction is the process of abstraction in Java is used to hide


certain details and only show the essential features of the object.
In other words, it deals with the outside view of an object
(interface).
Abstract class cannot be instantiated; the class does not have
much use unless it is subclass. This is typically how abstract
classes come about during the design phase. A parent class
contains the common functionality of a collection of child
classes, but the parent class itself is too abstract to be used on
its own.
E.g.

abstract class A
{
public abstract void sum(int x, int y);
}
class B extends A
{
public void sum(int x,int y)
{
System.out.println(x+y);
}
public static void main(String ar[])
{
B obj=new B();
obj.sum(2,5);
}
}
What Is an Interface?

An interface is a collection of abstract methods (it means all


methods are only declared in an Interface). A class implements an
interface, thereby inheriting the abstract methods of the interface.
And that class implements interface then you need to defined all
abstract function which is present in an Interface.

An interface is not a class. Writing an interface is similar to writing


a class, but they are two different concepts. A class describes the
attributes and behaviors of an object. An interface contains
behaviors that a class implements.
E.g.
interface A
{
public void sumData(int x, int y);
}
class Demo implements A
{
public void sumData (int x, int y)
{
System.out.println ("Total is "+(x+y));
}
public static void main (String ar[])
{
Demo d=new Demo ();
d.sumData (10, 20);
}
}
What Is An Encapsulation?

Encapsulation is one of the four fundamental OOP concepts.


The other three are inheritance, polymorphism, and
abstraction.
Encapsulation is the technique of making the fields in a class
private and providing access to the fields via public methods.
If a field is declared private, it cannot be accessed by anyone
outside the class, thereby hiding the fields within the class.

For this reason, encapsulation is also referred to as data


hiding.
E.g.
public class EncapTest
{
private String name;
private String idNum;
private int age;
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
public String getIdNum()
{
return idNum;
}
public void setAge( int newAge)
{
age = newAge;
}
public void setName(String newName)
{
name = newName;
}
public void setIdNum( String newId)
{
idNum = newId;
}
}
public class RunEncap
{
public static void main(String args[])
{
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");

System.out.print("Name : " + encap.getName()+" Age


:"+encap.getAge());
}
}
What is Polymorphism?
Method overloading and method overriding uses concept of
Polymorphism in Java where method name remains same in
two classes but actual method called by JVM depends upon
object at run time and done by dynamic binding in Java. Java
supports both overloading and overriding of methods. In case
of overloading method signature changes while in case of
overriding method signature remains same and binding and
invocation of method is decided on runtime based on actual
object.
Method overloading
In Method overloading we have two or more functions with
the same name but different arguments. Arguments must
be changed on the bases of Number, orders and Data types.
E.g.
class A
{
public void f1(int x)
{
System.out.println(x*x);
}
public void f1(int x,int y)
{
System.out.println(x*y);
}
public static void main(String ar[])
{
A a=new A();
a.f1(5);
a.f1(2,3);
}
}
Method Overriding

We have two classes and both classes have a function with the
same name and same Parameters inheritance is necessary.
Eg.

class B
{
public void f1(int x,int y)
{
System.out.println(x+y);
}
}
class A extends B
{
public void f1(int x,int y)
{
System.out.println(x*y);
}

public static void main(String ar[])


{
A a=new A();
a.f1(5,5);
B b=new B();
b.f1(2,3);
}
}
Difference between Procedural Programming and Object Oriented Programming:

Procedural Oriented Programming Object Oriented Programming

In procedural programming, program is In object oriented programming,


divided into small parts program is divided into small parts
called functions. called objects.

Procedural programming follows top Object oriented programming


down approach. follows bottom up approach.

Object oriented programming have


There is no access specifier in
access specifiers like private, public,
procedural programming.
protected etc.

Adding new data and function is not


Adding new data and function is easy.
easy.

Procedural programming does not have


Object oriented programming provides
any proper way for hiding data so it
data hiding so it is more secure.
is less secure.

In procedural programming, Overloading is possible in object


overloading is not possible. oriented programming.

In procedural programming, function is In object oriented programming, data is


more important than data. more important than function.

Procedural programming is based Object oriented programming is based


on unreal world. on real world.

Examples: C, FORTRAN, Pascal, Basic


Examples: C++, Java, Python, C# etc.
etc.

You might also like