This document provides an overview of object-oriented programming fundamentals in Java, including basic concepts like objects, classes, inheritance, polymorphism, and encapsulation. It discusses Java features, the Java program structure, variables and data types, operators, expressions, arrays, and control statements. Object-oriented programming concepts like abstraction, encapsulation, inheritance and polymorphism are explained. The benefits and applications of OOP are also highlighted.
3. Features of OBJECT ORIENTED
PARADIGMS
1. Data
2. Objects
3. Data structures
4. Methods
5. Hidden data
6. Communication
7. Addition of new data
8. Bottom up approach
4. Basic concepts of OBJECT ORIENTED
PROGRAMMING
I. OBJECTS & CLASSES
II. DATA ABSTRACTION & ENCAPSULATION
III. INHERITANCE
IV. POLYMORPHISM
V. DYNAMIC BINDING
VI. MESSAGE COMMUNICATION
5. I.OBJECTS & CLASSES
• Object-Basic runtime entity
• Class- Collection of objects of similar type
6. II.DATA ABSTRACTION &
ENCAPSULATION
• Encapsulation–Wrapping up of data and
methods into single unit(called class).
• Data hiding –Insulation of data from direct
access by program.
• Data abstraction Representing essential
features only.
9. V.DYNAMIC BINDING
• Linking of procedure call to code.
• Linking of method call to its corresponding
method definition takes place only during
runtime.
11. BENEFITS OF OOP
• Inheritance
• Time saving and high productivity.
• Data hiding
• Multiple objects
• Mapping of objects
• Easy to upgrade
• Communication through messages
• Can manage software complexity easily
12. APPLICATIONS OF OOP
• Real time systems.
• Simulation & Modelling.
• Object oriented DB
• AI
• Neural networks
13. JAVA FEATURES
Introduction:-
• Java is an object oriented language developed
by Sun Microsystems.
• Original name was OAK . Later renamed to
JAVA.
• Platform neutral language(platform
independent.)(Java Virtual Machine).
• Java is both compiled and interpreted
language.
16. JAVA FEATURES
1. Java is both a compiled and an interpreted language.
2. Platform independent and portable-Write Once Run
Anywhere.
3. Object Oriented.
4. Robust and Secure.
5. Distributed.
6. Automatic Memory Management.
7. Dynamic binding
8. High performance.
9. Multithreading.
17. Some features in C/C++ are eliminated in java.
They are listed below.
• No Pointers
• No Preprocessor Directives
• No Multiple Inheritance in Classes
• No operator overloading.
• No global variables.
18. Simple Java Program
class Welcome
{
public static void main(String args[])
{
System.out.println(“Welcome to Java world “);
}
}
20. Simple Java Program
/* * To change this template, choose Tools | Templates and open the template in the
editor. */
package college;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql;
class sum
{
public static void main(String args[])
{
int i=10,j=25;
k=i+j;
System.out.println(“THE SUM IS “+k);
}
}
21. Download JDK and install in
Windows10- Use the following
YouTube link.
https://video.search.yahoo.com/search/video?fr
=mcafee&ei=UTF-
8&p=download+jdk&type=E211US1045G0#id=4
&vid=3aa873b105c9f56e6a73b619da6222ba&a
ction=click
Compilation code- javac filename.java
Execution code- java filename
22. JAVA TOKENS
• In Java every statement is made up of smallest
individual elements called tokens. Tokens in java
include the following:-
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators
23. 1. Keywords
• Keywords cannot be redefined by a
programmer
• Keywords cannot be used as identifier name.
Eg: Abstract, Assert, Boolean, Break, Byte, Case.
• Java language has reserved 50 words as
keywords.
24. 2. Identifiers
• Programmer designed tokens. Used for naming
variables, methods, classes etc.
• An identifier can contain alphanumeric
characters, but it should begin with an alphabet.
• Identifiers are case sensitive.
• Underscore and dollar sign are two special
character allowed.
• Eg:A23C
• Bob@gmail - Wrong
25. 3. Literals
• LITERAL is a program element that represents
an exact (fixed) value of a certain type. Java
language supports the following type of
literals.
1. Integer Literals. (Ex. 123)
2. Floating point Literals. (Ex. 88.223)
3. Character Literals. (Ex. ‘.’ , ‘a’ , ‘n’)
4. String Literals. (Ex. “Hello World”)
5. Boolean Literals. (Ex. True,false)
26. 4. Separators
• Symbols used to indicate where group of code
are divided and arranged.
• ()
• {}
• []
• ;
• ,
• .
27. 5. Operators
• Tells the computer to perform certain
mathematical or logical manipulations on data
and variables. Different types are:-
1. Arithmetic Operators.
2. Unary Operators.
3. Assignment Operators.
4. Equality and Relational Operators.
5. Logical and Conditional Operators.
6. Bitwise and Bit Shift Operators.
7. Special operators
29. 3. Assignment Operators
Eg:a=b+c;
a=a+1; and a+=1; same meaning.
a+=1 is shorthand assignment operator.
4. Equality and Relational Operators
Eg: a==b;
a!=b;
• Value of relational expression is either true or false.
• It is used in decision statements.
Eg: a<b;
a>b;
a<=b;
a>=b;
30. 5. Logical and Conditional Operators.
• Used to form compound conditions by combining 2 or more
relations.
&& -logical AND
|| -logical OR.
! – logical compliment.
Eg: a>b && x==10;
?: - Conditional ternary opeartor(shorthand for if-then-else
statement).
Eg: x=(a>b)?a:b;
31. 6. Bitwise and Bit Shift Operators
~ Unary bitwise complement
<< Signed left shift
>> Signed right sift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
32. 7. Special Operators
1. Instanceof operator
Eg: person instanceof student.
• Checks whether object person belongs to class student or not.
• Returns either true or false
2. Dot operator /member selection operator
• Used to access variables and methods of a class object.
Eg: Person1.age;
Person1.salary();
Where person1 is an object .
Age is a variable
Salary() is a method.
35. EXPRESSIONS
• Java expressions are used to implement some computation.
• Syntax: Variable=Exprerssion;
• Simple Expression : x=a+b;
• Complicated expression : a*(b+c)/(m+n)*f;
In this example,
1. (b+c) or (m+n) executed first since it is written inside
brackets.(From left to right or right to left. Refer operator
precedence table in Unit1Part1.pdf).
2. a*(b+c), or (m+n)*f executes multiplication operation secondly.
3. Finally division operation takes place.
36. COMMAND LINE ARGUMENTS
• Command line arguments are parameters that are supplied to the
application program at the time of invoking it for execution.
• Eg:- public static void main(String args[]).
• Eg: javac hello.java
java hello 123 abc 12.88
Then,
• args[0] contains 123
• args[1] contains abc
• args[2] contains 12.88
• Wrapper classes are used to convert the string to corresponding
simpler data type.
• ie, static int parseInt(String s)
• For example: int value= Integer.parseInt(“123”)
• Int total=args.length;
• Converts stirng 10 to integer data type.
37. Fibonacci series
class fib{
public static void main(String args[])
{
int n;
int a=0,b=1,c;
n=Integer.parseInt(args[0]);
for(int i=0;i<n;i++)
{
c=a+b;
System.out.println("The next number is "+c);
a=b;
b=c;
}
}
}
38. VARIABLES
• Variables refers to names of storage location
which is used to store data vales.
Eg: int i;
int i=3,j=5;
• Can take different values at different times during
execution of the program.
• Similar as identifiers.
• Assignment method
• readline() method.(Type casting)
39. Variables example- readLine()
import java.io.Console;
class exampleread
{
public static void main(String args[])
{
String str;
Console con = System.console();
if(con == null)
{
System.out.print("No console available");
return;
}
str = con.readLine("Enter your name: ");
con.printf("Here is your name: %sn", str);
}
}
40. VARIABLES(readLine() & Type casting
using wrapper classes)
import java.io.Console;
class examplereadline
{
try{
public static void main(String args[])
{
int age=0;
Console con = System.console();
age= Integer.parseInt(con.readLine("Enter your age") );
con.printf("Here is your age:%dn", age);
}
}
Catch{
}
}
41. Scope of variables
• Instance variables - Created when objects are instantiated and
takes different values for each object.
Eg: student s1=new student();
• Each object has its own copy of instance variables.
• Class variables - Available to all methods in a class.
• Local variables – Local to the method inside it is declared.
42. Scope of variables-Example
// Demonstrate block scope.
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}
43. CONSTANTS
• Constants refer to values that do not change
during the execution of the program.
• Java supports following types of constants.
44. • Integer constant. Eg:2021.
• Real/Floating point constant. Eg: 32.567.
• String constant. Eg: “Hello World”.
• Character constant. Eg: ‘a’.
• Final float pi=3.142;
• Apart from these types of constants java also
supports backslash character constants.
BACKSLASH CONSTANT PURPOSE
b Backspace
f Form feed
n New Line
r Carriage return
t Horizontal tab
Backslash
46. Data types
Integer
char a = 'G';
int i=89;
byte b = 4;
short s = 56;
float f = 4.7333434f;
double d = 4.355453532d;
47. Data types- Example
class datatypes{
public static void main(String args[]){
char a = 'G';
int i=89;
byte b = 4;
short s = 3;
double d = 4.355453532d;
float f = 4.7333434f;
System.out.println("char: " + a);
System.out.println("integer: " + i);
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("float: " + f);
System.out.println("double: " + d);
}
}
48. ARRAYS
• An array is a group of contiguous/related data
items that share a common name.
• Syntax: Data type Arrayname[];
• Eg: int salary[9] or int[] salary;
• 3 steps to create the array.
• 1) Declare the array->Only a reference of array
created.
• 2) Create the memory allocation->
salary=new int[9];
• 3)Putting values to memory location.
49. Array representation:-
Int a=Arrayname.length; // To find length of the array.
One dimensional Arrays.
Array Literal
In a situation, where the size of the array and variables of
array are already known, array literals can be used.
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
50. class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
51. Arrays as Parameters
An entire array can be passed as a parameter to a
method
Like any other object, the reference to the array is
passed, making the formal and actual parameters aliases
of each other
Changing an array element within the method changes
the original
An array element can be passed to a method as well, and
follows the parameter passing rules of that element's
type
52. class Testarray3{
//creating a method which receives an array as a parameter
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i])
min=arr[i];
System.out.println(min);
}
public static void main(String args[]){
int a[]={33,3,4,5};//declaring and initializing an array
min(a);//passing array to method
}}
54. Array-Assigning values can be done directly or
using loops.
public static void main(String args[])
{
// declaring and initializing 2D array
int arr[][] = { {2,7,9},{3,6,1},{7,4,2} };
// printing 2D array
for (int i=0; i< 3 ; i++)
{
for (int j=0; j < 3 ; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
55. Multi Dimensional Arrays
• int[][][] intArray = new int[10][20][10]; //a 3D
array .
• Known as Jagged Arrays.
• Strings- Character Array or String Objects.
56. STRINGS
• It is the most common part of many java
programs.
• String represent a sequence of characters.
• The simplest way to represent a sequence of
characters in java is by using a character array.
• Example:
char charArray[ ] = new char[4];
charArray[0] = ‘J’;
charArray[1] = ‘a’;
58. STRING ARRAYS
• Array can be created and used that contain
strings.
• Above statement create following effects:
59. In Java, strings are class objects and implemented
using two classes,namely, String and StringBuffer.
A java string is an instantiated object of the String
class.
Java string as compared to C strings, are more
reliable and predicable.
A java string is not a character array and is not
NULL terminated.
60. STRING METHODS
String class defines a number of methods that
allow us to accomplish a variety of string
manipulation tasks.
Eg: 1) s2=s1.toLowercase;
2) s2=s1.toUppercase;
3) s1.compareTo(s2) //To sort array of
strings.
61. STRING BUFFER CLASS
StringBuffer is a peer class of String.
While String creates strings of fixed_length,
StringBuffer creates strings of flexible length that
can be modified in term of both length and content.
We can insert characters and substrings in the
middle of a string to the end.