0% found this document useful (0 votes)
4 views53 pages

Java Programming Unit 1

The document provides an introduction to Java programming and object-oriented programming (OOP) concepts, including classes, objects, encapsulation, inheritance, abstraction, and polymorphism. It outlines the structure of a Java program, features of Java, and the Java Development Kit (JDK) and Java Runtime Environment (JRE). Additionally, it explains the compilation process and access specifiers in Java.

Uploaded by

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

Java Programming Unit 1

The document provides an introduction to Java programming and object-oriented programming (OOP) concepts, including classes, objects, encapsulation, inheritance, abstraction, and polymorphism. It outlines the structure of a Java program, features of Java, and the Java Development Kit (JDK) and Java Runtime Environment (JRE). Additionally, it explains the compilation process and access specifiers in Java.

Uploaded by

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

JAVA PROGRAMMING

UNIT-I
INTRODUCTION TO OOP AND JAVA
FUNDAMENTALS
UNIT – I

INTRODUCTION TO OOP AND JAVA FUNDAMENTALS

OBJECT ORIENTED PROGRAMMING – ABSTRACTION – OBJECTS AND


CLASSES – ENCAPSULATION – INHERITANCE – POLYMORPHISM – OOP
IN JAVA – CHARACTERISTICS OF JAVA – THE JAVA ENVIRONMENT –
JAVA SOURCE FILE STRUCTURE – COMPILATION – FUNDAMENTAL
PROGRAMMING STRUCTURES IN JAVA
INTRODUCTION TO JAVA

🞆 Java is a programming language and it is platform


independent.
🞆 Java was developed by Sun Microsystems Inc in
1991, later acquired by Oracle Corporation.
🞆 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.
OBJECT ORIENTED PROGRAMMING (OOPS) CONCEPTS
OBJECT ORIENTED PROGRAMMING (OOP) IS A PROGRAMMING
LANGUAGE MODEL ORGANIZED AROUND OBJECTS.

OOPS CONCEPTS

1. CLASS
2. OBJECT
3. ENCAPSULATION
4. INHERITANCE
5. ABSTRACTION
6. POLYMORPHISM
OOPS CONCEPTS
🞆 Class
1. Collection of objects
2. A blueprint that consists of data members and
methods
3. Primary purpose of a class is to hold
data/information
Syntax:
class classname
{
type variable1;
type methodname1(parameter-list)
{
// body of method
}
}
CLASS
🞆 Class is a blueprint / template from which
individual objects can be created
🞆 It defines the attributes and the behaviors that
an object will possess
CAR
Attributes/Instance Color, Wheels, Price
Variables
Behavior/Method/Function Move, Stop, Start
CLASS DECLARATION
class car
{
//Variables
String color;
int wheels;
float price;
//Methods
void start(){}
void stop(){}
void move(){}
}
OBJECT
CREATION

classname objectname= new


classname();
Example:
car maruti = new car();
car iten = new car();
maruti Main Memory
color
wheel
price
Start(){}
Stop(){}
Move(){}
Accessing Class Members using OBJECT
CREATION
objectname. Class member;

maruti
Example: Main Memory
Color white
maruti.color=“White”
Wheel 4
maruti.wheels=4
Price 500000
maruti.price=500000.00 Start(){}
maruti.start() Stop(){}
maruti.stop() Move(){}

maruti.move()
OOPS CONCEPTS
🞆 Abstraction
Abstraction means hiding lower-level details and
exposing only the essential and relevant details to the
users.
Real World Examples:
1.Car, which abstracts the internal details and
exposes to the driver only those details that are
relevant to the interaction of the driver with the Car.
2.Consider an ATM Machine; All are performing
operations on the ATM machine like cash withdrawal,
money transfer, retrieve mini-statement etc. but we
can't know internal details about ATM.

In Java, abstraction is achieved by


Interfaces and Abstract classes.
OOPS CONCEPTS
🞆 Encapsulation
🞆 Encapsulation is a process of wrapping of data
and methods in a single unit is called
encapsulation.
🞆 In OOP, data and methods operating on that data
are combined together to form a single unit,
called Class.
Encapsulation is the mechanism that binds
together code and the data it manipulates
and keeps both safe from outside interference
and misuse.
OOPS CONCEPTS
🞆 Inheritance
🞆 Inheritance is a process of obtaining the data
members and methods from one class to another
class.
🞆 The class whose features are inherited is known
as a superclass (or a base class or a parent
class).
🞆 The class that inherits the other class is known
as a subclass(or a derived class, extended class,
or child class).
OOPS CONCEPTS
🞆 Polymorphism
🞆 The process of taking multiple forms is known
as Polymorphism.
🞆 Polymorphism let us perform a single action in
different ways.
🞆 One method can perform in different ways.
STRUCTURE OF A JAVA PROGRAM
class Simple
{
public static void main(String args[ ])
{
System.out.println("Java World");
}
}

class – to declare a class in java


public – access modifier which represents visibility, it means it is
visible to all
static –. no need to create object to invoke the static method. The
main method doesn't require object to invoke the main method.
So it saves memory
void – the return type of the method, it means it doesn't return any
value
main – represents the starting point of the program.
String[] args – used for command line argument
System.out.println() – print statement
A SAMPLE PROGRAM
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id=5;
String name=“John”;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object
Student s1=new Student();
//Printing values of the object
System.out.println(s1.id);
//accessing member through reference variable
System.out.println(s1.name);
}
FEATURES OF JAVA
The features or characteristics of Java
are:
1.Simple
2.Robust and Secure
3.Platform independent
4.Object oriented
5.Multithreaded
6.Compiled and Interpreted
7.Dynamic
8.Distributed
FEATURES OF JAVA

1.SIMPLE
Easy to learn if the programmers know C/C++
2.ROBUST AND SECURE
1.No pointer concept
2.Automatic garbage collection
3.Use can’t perform memory management
directly
4. Byte code can’t be infected
FEATURES OF JAVA
3.PLATFORM INDEPENDENT
Java programs can execute on any platform

4.OBJECT ORIENTED
Supports Encapsulation, Inheritance, Abstraction,
Polymorphism.

5.MULTITHREADED
Can execute many tasks simultaneously

6.COMPILED AND INTERPRETED


Java compiler converts source code into byte code
Java interpreter converts byte code into machine code
FEATURES OF JAVA
7.DYNAMIC
Java includes many class libraries

8.DISTRIBUTED
Used in networking
Two objects on different computers can
communicate through Remote Method Invocation
(RMI)
THE JAVA RUNTIME ENVIRONMENT
Java Development Kit
JAVA DEVELOPMENT KIT (JDK)

• JDK – Java Development Kit


• A Package which provides the
environment to develop and execute
(run) the Java program.
• JDK is a kit that includes two things
• Development Tools (to provide an
environment to develop your java
programs)
• JRE (to execute your java program).
• JDK = JRE + Development Tools
JAVA RUN TIME ENVIRONMENT
(JRE)
• JRE – Java Runtime Environment
• Installation package which provides
environment to only run (not
develop) the java program(or
application) in your machine.
• JRE is only used to run the Java
Programs.
• JRE is a part of JDK which means that
JDK includes JRE.
JAVA VIRTUAL MACHINE (JVM)
• JVM – Java Virtual machine(JVM)
• It is a very important part of both
JDK and JRE because it is contained
or inbuilt in both.
• Java program goes into JVM and JVM
is responsible for executing the
java program line by line hence it
is also known as interpreter.
THE COMPILATION PROCESS
🞆 Phases of the compilation process

1) Writing of the program is of course done by java


programmer.
2) Compilation of program is done by javac
compiler, javac is the primary java compiler
included in java development kit (JDK).
It takes java program as input and generates java
byte code as output.
3) In third phase, JVM executes the byte code
generated by compiler. This is called program run
phase.
🞆 Each operating system has different JVM,
however the output they produce after
execution of bytecode is same across all
operating systems. That is why we call java as
platform independent language.
🞆 The byte code is saved in a .class file by
compiler.
THE COMPILATION PROCESS
//
Java Program to illustrate how to define a class and fie
lds
//Defining a Student class.
class Student{
//defining fields
int id=5;
String name=“John”;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object
Student s1=new Student();
//Printing values of the object
System.out.println(s1.id);
//accessing member through reference variable
System.out.println(s1.name);
}
🞆
A SAMPLE PROGRAM
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student
{
public static void main(String args[])
{
int id=5;
String name=“John”;
System.out.println(id);
System.out.println(name);
}
THE JAVA RUNTIME ENVIRONMENT
THE JAVA RUNTIME ENVIRONMENT
THE JAVA RUNTIME ENVIRONMENT
Java Development Kit
JAVA DEVELOPMENT KIT (JDK)
• JDK – Java Development Kit
• A Package which provides the environment
to develop and execute (run) the Java
program.
• JDK is a kit that includes two things
• Development Tools (to provide an
environment to develop your java
programs)
• JRE (to execute your java program).
• JDK = JRE + Development Tools
JAVA DEVELOPMENT KIT (JDK)
JAVA RUN TIME ENVIRONMENT (JRE)

• JRE – Java Runtime Environment


• Installation package which provides
environment to only run (not
develop) the java program(or
application) in your machine.
• JRE is only used to run the Java
Programs.
• JRE is a part of JDK which means that
JDK includes JRE.
JAVA RUN TIME ENVIRONMENT (JRE)
JAVA VIRTUAL MACHINE (JVM)
• JVM – Java Virtual machine(JVM)
• It is a very important part of both
JDK and JRE because it is contained
or inbuilt in both.
• Java program goes into JVM and JVM
is responsible for executing the
java program line by line hence it
is also known as interpreter.
THE COMPILATION PROCESS
🞆 Phases of the compilation process

1) Writing of the program is of course done by java


programmer.
2) Compilation of program is done by javac
compiler, javac is the primary java compiler
included in java development kit (JDK).
It takes java program as input and generates java
byte code as output.
3) In third phase, JVM executes the byte code
generated by compiler. This is called program run
phase.
THE COMPILATION PROCESS
STRUCTURE OF A JAVA PROGRAM
STRUCTURE OF JAVA PROGRAM
🞆 A package is a collection of classes,
interfaces and sub-packages. java.lang.*
package is imported by default.
🞆 Class is keyword used for developing user
defined data type.
🞆 ClassName represent a java valid variable
name treated as a name of the class.
🞆 Data member represents the attributes of
the class.
🞆 User-defined methods are meant for
performing the operations.
STRUCTURE OF JAVA PROGRAM

public static void main()


🞆 Public – It means that main can be used outside of the
class as well.
🞆 Static – It means that the method can be accessed
without objects.
🞆 Void – Indicates that main does not return any value.
🞆 Each and every java program starts execution from the
main() method. And hence main() method is known as
program driver.
🞆 String[] args – It is an array where each element is a
string, which is named as args.
🞆 If you run the Java code through a console, you can pass
the input parameter. The main() takes it as an input.
STRUCTURE OF JAVA PROGRAM

public static void main()


STRUCTURE OF JAVA PROGRAM
System.out.println()
🞆 lang. System class in Java. Among the facilities provided
by the System class are standard input, standard output
and error output streams.
🞆 out is an object of the PrintWriter class.
🞆 The method println is a method of PrintStream class prints
the text on the screen with a new line.
🞆 class PrintStream
{
println();
{}
}
class PrintWriter extends PrintStream
{
}
🞆 ‘out’ is the object of PrintWriter class
‘println’ is the method of PrintStream class
ACCESS SPECIFIERS (ACCESS MODIFIERS)
🞆 The access modifiers in Java specifies the
accessibility or scope of a field, method,
constructor, or class.
ACCESS SPECIFIERS (ACCESS MODIFIERS)
🞆 Private:
🞆 Private members of class in not accessible anywhere in program
🞆 These are only accessible within the class. Private are also called class level
access modifiers.
ACCESS SPECIFIERS (ACCESS MODIFIERS)
🞆 Public
🞆 Public members of any class are accessible anywhere in the program in the
same class and outside of class, within the same package and outside of the
package. Public are also called universal access modifiers.
ACCESS SPECIFIERS (ACCESS MODIFIERS)
🞆 Default:
Default members of the class are accessible only within the same
class and another class of the same package. The default are also
called package level access modifiers.
ACCESS SPECIFIERS (ACCESS MODIFIERS)
🞆 Protected:
🞆 Protected members of the class are accessible within the same class and
another class of the same package and also accessible in inherited class of
another package. Protected are also called derived level access modifiers.
ACCESS SPECIFIERS (ACCESS MODIFIERS)
A SAMPLE PROGRAM
//Java Program to illustrate how to define a class and fields

class Student
{
public static void main(String args[])
{
int id=5;
String name=“John”;
System.out.println(id);
}

You might also like