0% found this document useful (0 votes)
10 views47 pages

Unit1 Apj

The document provides an overview of Object-Oriented Programming (OOP) concepts, highlighting its advantages over Procedure-Oriented Programming (POP). It details key features of OOP such as classes, objects, encapsulation, polymorphism, abstraction, and inheritance, along with Java's characteristics like portability, security, and robustness. Additionally, it covers basic Java syntax, control statements, data types, and the Java class libraries.

Uploaded by

jesudosss
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)
10 views47 pages

Unit1 Apj

The document provides an overview of Object-Oriented Programming (OOP) concepts, highlighting its advantages over Procedure-Oriented Programming (POP). It details key features of OOP such as classes, objects, encapsulation, polymorphism, abstraction, and inheritance, along with Java's characteristics like portability, security, and robustness. Additionally, it covers basic Java syntax, control statements, data types, and the Java class libraries.

Uploaded by

jesudosss
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

Advanced

Programming in Java
Unit -1
OBJECT ORIENTED
PROGRAMMING (OOP)
• the concept of objects that contain data and methods

2
Advantages of Object Oriented
Programming
• Emphasis in own data rather than procedure.

• Data and the functions are wrapped into a single unit called class

• Objects communicate with each other through functions.

3
PROCEDURE-ORIENTED
PROGRAMMING [POP]
• It focuses on process rather than
data

• A program is divided into a number


of functions
Difference between POP and
OOP Object Oriented Procedure Oriented
Programming Programming
Divided Into In POP, program is divided In OOP, program is divided
into small parts called into parts called objects.
functions.
Importance In POP, Importance is not In OOP, Importance is given
given to data but to to the data rather than
functions as well as procedures or functions
sequence of actions to be because it works as a real
done. world.
Access Specifiers POP does not have any OOP has access specifiers
access specifier. named Public, Private,
Protected, etc.
Data Hiding POP does not have any OOP provides Data Hiding
proper way for hiding data so provides more security.
so it is less secure.
Examples Examples of POP are: Examples of OOP are:
C,VB, FORTRAN, and Pascal. C++, JAVA, [Link], C#.NET.
FEATURES OF OBJECT ORIENTED
PROGRAMMING
CONCEPTS

1. Class - Blue print of Object


2. Object - Instance of class
3. Encapsulation - Protecting our data
4. Polymorphism - Different behaviors at different instances
5. Abstraction - Hiding irrelevant data
6. Inheritance - An object acquiring the property of another object
Class
• A class is a collection of similar objects
• It contains data and methods that operate on that data
Class
• Syntax to declare a class:
class <class_name>
•{
field;
method;
}
}
Object
• Object is an instance of a class.
• Any entity that has state and behavior is known as an object
• For example: chair, pen, table, keyboard, bike etc
class_name object_name = new class_name;
(or)
class_name object_name;
object_name = new class_name();
Object
// Define a class
class Car {
// Method to display a message
void show() {
[Link]("This is a car.");
}
}

// Main class
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car();

// Calling the method using the object


[Link]();
}
Encapsulation:

• Wrapping of data and method together into a single unit is known


as Encapsulation

The protection of the data from direct access by the program is called ―data
hiding
Polymorphism:
• The word "poly" means many and "morphs" means forms. So
polymorphism means many forms.

• Polymorphism is a concept by which we can perform a single action by


different ways

• Types of Polymorphism

• Compile time polymorphism / Method Overloading


• Runtime polymorphism / Method Overriding
Function overloading
Method Overriding
. Abstraction
• Abstraction refers to the act of representing essential features
without including the background details or explanations

• Example : CaR
. Abstraction
Inheritance

• The inheritance is the process of acquiring the properties of one


class to another class.
• Inheritance represents the IS-A relationship, also known as parent-
child relationship.

class Subclass-name extends Superclass-name


{
//methods and fields
}
Message Passing
• Objects interact and communicate with each other by sending
messages to each other.

• This information is passed along with the message as parameters.


Java Buzzwords
Simple

• Java is designed to be easy for professional programmers to learn and


use effectively.

• EXAMPLE:
• If you already know object-oriented programming or have experience
with programming languages like C++, learning Java becomes even
easier.
Secure

• Java makes sure that downloaded programs don't harm your


computer by keeping them within a safe environment.

• It stops them from accessing private information making it safer to


run programs from the internet
Portable

• Portability means a Java program can run on any computer or


operating system without needing separate versions
Object-oriented

• Java was designed independently to make object-oriented


programming simple and practical
Robust

• Java is robust because it ensures programs run reliably on different


systems by detecting errors early during both compilation and
runtime
Multithreaded

• Java supports multithreading, enabling programs to perform multiple


tasks at the same time
Architecture-neutral

• Java is considered architecture-neutral because Java programs can


run on any platform without modification.

• This is achieved through the Java Virtual Machine (JVM), which acts
as an intermediary between the compiled Java program and the
underlying hardware
Interpreted

• Java programs are compiled into an intermediate form called


bytecode, which can run on any system with a Java Virtual Machine,
enabling cross-platform functionality.
High Performance

• With the use of Just-In-Time compilers, Java enables high


performance.
• JVM uses JIT compiler to improves the execution time of the program
Distributed

• allows programs to be run across multiple computers or nodes,


connected over a network, to work together as one system.
Dynamic

• Java's dynamic nature allows developers to adapt and modify the


code during runtime..
An Overview of Java
A First Simple Program
An Overview of Java
A First Simple Program
The file must have a .java extension
The file must have a .java extension and should match the name of the class
inside it.
For example, if the class is called "Example," the file should be named
"[Link]."
An Overview of Java
Compiling the Program
To compile a Java program, you use the command
c: \>javac [Link]
This creates a file named [Link] with bytecode, which can't be directly
run.

To run the program, you use the java command with the class name
C:\>java Example
When the program is run, the following output is displayed: This is a simple Java
program .
An Overview of Java
Control Statements
The if Statement

if(condition) statement;

Example

if(num< 100)
[Link]("num is less than 100");
An Overview of Java
Control Statements
The if Statement
/*
Demonstrate the if.
Call this file "[Link]".
*/
class IfSample {
public static void main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x < y) [Link]("x is less than y");
x = x * 2;
if(x == y) [Link]("x now equal to y");
x = x * 2;
if(x > y) [Link]("x now greater than y");
// this won't display anything
if(x == y) [Link]("you won't see this");
}
}
for loop
The simplest form of the for loop is shown here:

for(initialization; condition; iteration) statement;


/* This program generates the following output:
Demonstrate the for loop. This is x: 0
This is x: 1
Call this file "[Link]".
This is x: 2
*/ This is x: 3
class ForTest { This is x: 4
public static void main(String args[]) { This is x: 5
This is x: 6
int x; This is x: 7
for(x = 0; x<10; x = x+1) This is x: 8
[Link]("This is x: " + x); This is x: 9

}
}
Using Blocks of Code
• Java allows two or more statements to be grouped into blocks of
code, also called code blocks.
/*
Demonstrate a block of code. The output generated by this program is shown here:
Call this file "[Link]" This is x: 0
*/ This is y: 20
class BlockTest { This is x: 1
public static void main(String args[]) { This is y: 18
int x, y; This is x: 2
y = 20; This is y: 16
// the target of this loop is a block This is x: 3
for(x = 0; x<5; x++) { This is y: 14
[Link]("This is x: " + x); This is x: 4
[Link]("This is y: " + y); This is y: 12
y = y - 2; This is x: 5
} This is y: 10
}
}
Whitespaces

• Java is called a free-form language because it doesn’t force you to


write the code in a specific layout.

• Spaces, tabs, or newlines are used to separate words or symbols


when needed, but how you arrange them is up to you
Identifiers

• Identifiers are used to name things, such as classes, variables, and


methods.
• Some examples of valid identifiers are
Literals

• A constant value in Java is created by using a literal representation of


it.
Separators
• In Java, there are a few characters that are used as separators.
• The most commonly used separator in Java is the semicolon.
The Java Keywords
• There are 50 keywords currently defined in the Java language
The Java Class Libraries
• Java provides built-in methods like println() and print() through
[Link], where System is a predefined class included in all
programs.
• Java comes with class libraries that handle tasks like input/output,
working with strings, networking, graphics, and creating graphical
user interfaces (GUIs).
• These libraries add powerful features to Java.
• Learning Java also involves understanding how to use these standard
libraries.
Data Types, Variables, and
Arrays
• Java is a strongly typed language

• Every variable and expression has a specific, well-defined type


The Primitive Types


• Primitive Data Type: byte, short, int, long, char, float, double, and
boolean.

• Non-Primitive Data Type: String, Array, etc.


Example Program for Primitive
Data Types:
public class PrimitiveExample {
public static void main(String[] args) { [Link]("Byte: " + smallNumber);
byte smallNumber = 100; [Link]("Short: " + mediumNumber);
[Link]("Int: " + largeNumber);
short mediumNumber = 30000;
[Link]("Long: " + veryLargeNumber);
int largeNumber = 100000; [Link]("Float: " + decimalNumber);
long veryLargeNumber = 10000000000L; [Link]("Double: " + preciseDecimal);
float decimalNumber = 3.14f; [Link]("Char: " + letter);
double preciseDecimal = 3.141592653; [Link]("Boolean: " + isJavaFun);
}
char letter = 'A'; }
boolean isJavaFun = true;

You might also like