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

Object Oriented Data Structure: Unit 1

This document provides an overview of object-oriented programming concepts in Java. It discusses the benefits of OOP, key features like encapsulation, abstraction, inheritance and polymorphism. It also compares procedural and object-oriented programming, explains the Java Virtual Machine and bytecode, lists features of Java like being simple, secure, portable etc. Finally, it covers basic Java concepts like variables, data types, operators, program structure and control statements like if-else.

Uploaded by

shivansh
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)
47 views

Object Oriented Data Structure: Unit 1

This document provides an overview of object-oriented programming concepts in Java. It discusses the benefits of OOP, key features like encapsulation, abstraction, inheritance and polymorphism. It also compares procedural and object-oriented programming, explains the Java Virtual Machine and bytecode, lists features of Java like being simple, secure, portable etc. Finally, it covers basic Java concepts like variables, data types, operators, program structure and control statements like if-else.

Uploaded by

shivansh
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/ 40

UNIT 1

Object Oriented Data Structure

CST364
Course Outcomes
Unit-1
Text Books
Introduction
Object-oriented programming (OOP) is a way of thinking
about and organizing code for maximum reusability. With
this type of programming, a program comprises objects
that can interact with the user, other objects, or other
programs. This makes programs more efficient and easier
to understand.
Benefits of OOPs
Troubleshooting is easier with the OOP language
Code Reusability
Productivity
Data Redundancy
Code Flexibility
Solving problems
Security
Features of object-oriented programming

There are four features of object-oriented programming to


know:
1. Encapsulation: Data and methods that interact with that
data are bundled into one unit. This allows you to control
access to the data within each object.
2. Abstraction: When creating an object, the coder reduces
complexity by showing only essential information and "hiding"
everything else, including implementation mechanisms.
3. Inheritance: A programmer can derive a new object with
all or some of the properties of an existing object. For
example, a child class will inherit properties and behaviour
from a parent class.
4. Polymorphism: This allows us to use child and parent
classes in precisely the same way while maintaining each class's
unique attributes.
Difference between Procedural and OO
programming language

S.NO Procedural Oriented Programming Object Oriented Programming


1. Program is divided into small parts Program is divided into parts called objects.
called functions.

2. Importance is not given to data but to Importance is given to the data rather than
functions as well as sequence of procedures or functions because it works
actions to be done. as a real world.

3. Follows Top Down approach. OOP follows Bottom Up approach.


4. It does not have any access specifier. OOP has access specifiers named
Public, Private, Protected, etc.

5. Data can move freely from Objects can move and communicate with
function to function in the system. each other through member functions.
Difference between Procedural and OO
programming language

S.NO Procedural Oriented Programming Object Oriented Programming


6. To add new data and function in POP is OOP provides an easy way to add new data
not so easy. and function.
7. Most function uses Global data for In OOP, data can not move easily from
sharing that can be accessed freely from function to function, it can be kept public or
function to function in the system. private so we can control the access of
data.
8. It does not have any proper way for OOP provides Data Hiding so provides
hiding data so it is less secure. more
security.
9. Overloading is not possible. In OOP, overloading is possible in the form
of Function Overloading and Operator
Overloading.
10. Example of Procedure Oriented Example of Object Oriented Programming
Programming are : C,VB, FORTRAN, are : C++, JAVA,VB.NET, C#.NET.
Pascal.
History of Java

Java is an Object-Oriented Programming language developed by James


Gosling at sun microsystems in the early 1990s.

Java was started as a project called "Oak" by James Gosling in June 1991.
Gosling's goals were to implement a virtual machine and a language that had a
familiar C-like notation but with greater uniformity and simplicity than
C/C++.

The first public implementation was Java 1.0 in 1995. It made the promise of
"Write Once, Run Anywhere", with free runtimes on popular platforms.
What is JVM?
Java Virtual Machine (JVM) is a engine that provides runtime environment to
drive the Java Code or applications. It converts Java bytecode into machines
language. JVM is a part of Java Runtime Environment (JRE). In other programming
languages, the compiler produces machine code for a particular system. However,
Java compiler produces code for a Virtual Machine known as Java Virtual Machine.

How JVM Works?


First, Java code is compiled into
bytecode. This bytecode gets
interpreted on different machines
Between host system and Java
source, Bytecode is an
intermediary language.
JVM in Java is responsible for
allocating memory space.
Features of Java
Features of Java

Simple:
• User friendly Syntax
• There is automatic Garbage collection.
• No complicated features like operator overloading, pointers etc., as in C and C++.

Secured:
• Java is secured as it uses runtime environment of its own with almost no
interaction with System OS.
• The components Classloader, Bytecode Verifier and Security Manager of Java makes
it much more secure.

Independent:
Java is compiled into bytecode(an intermediate format). ● This Bytecode can be run on
any machine with Java Virtual Machine.
Features of Java

Java Program is written once and can be run that java program in any of the operating
systems Windows, Linux etc., without re-compiling.
Hence Java Program is considered as “Write Once, Run Anywhere”
This concept made Java Platform Independent.
Features of Java
Robust:
• Java has automatic Garbage Collector which helps to get rid of objects which are
not being used by a Java application anymore.
• This makes memory management stronger.
• Java emphasizes on compile time error checking and runtime error checking to
eliminate error prone codes.
• Exception handling Mechanism also makes Java Robust.

Portable:
• Java Programs can be on any platform like Windows, Linux, Mac etc.,
• Java Programs, applets can be transferred over world wide web.
• Java Compiler outputs a Byte code which is executed by Java run-time system, Java
Virtual Machine.
• Once, JVM is installed for particular system, any java program can run on it.

Architectural Neutral Language:


Java Byte code is not machine dependent, it can run on any machine with any processor and
with any OS.
Features of Java
Interpreted :
Java program are compiled to generate the byte code. This byte code can be downloaded
and interpreted by the interpreter. .Class file will have byte code instructions and JVM
which contains an interpreter will execute the byte code.

Dynamic :
We can develop programs in java which dynamically change on internet (e.g. Applets).

High Performance :
Along with interpreter there will be JIT( Just in Time) compiler which enhances the speed
of execution.

Multithreaded :
Executing different parts of program simultaneously is called Multithreading. This is an
essential feature to design server side program.

Distributed :
Java is designed for use on network, it has an extensive library which works in agreement
with TCP/IP.
Variables
• What is a variable?
• The name of some location of memory used to hold a data value
• Different types of data require different amounts of memory. The
compiler’s job is to reserve sufficient memory
• Variables need to be declared once
• Variables are assigned values, and these values may be changed later
• Each variable has a type, and operations can only be performed
between compatible types
Variable Names

• Valid Variable Names: These rules apply to all Java names, or identifiers,
including methods and class names
• Starts with: a letter (a-z or A-Z), dollar sign ($), or underscore (_)
• Followed by: zero or more letters, dollar signs, underscores, or digits
(0-9).
• Uppercase and lowercase are different (total ≠ Total ≠ TOTAL)
• Cannot be any of the reserved names. These are special names
(keywords) reserved for the compiler. Examples:

class, float, int, if, then, else, do, public, private, void, …
Data types
1. Primitive Data type
Primitive data are only single values and have no special
capabilities. There are 8 primitive data types. They are depicted
below in tabular format below as follows:
1. Boolean 5. Byte
2. Char 6. long
3. Int 7. float
4. Short 8. double

2. Non-Primitive Data Types or Reference Data Types will contain a


memory address of variable values because the reference types won’t
store the variable value directly in memory. They are strings, objects,
arrays, etc.
Operators
Operators in Java can be classified into 5 types:
1. Arithmetic Operators (+, -, *, /, %)
2. Assignment Operators (=, +=, -=, *=) a+=b🡪 a=a+b
3. Relational Operators (==, !=, >, <, >=, <=)
4. Logical Operators (&, ||, !)
5. Unary Operators (++, --)
6. Bitwise Operators (~, <<, >> , &(bitwise and), ^(bitwise or))
Structure of Java Program
public class MyFirstJavaProgram
{
public static void main(String []args)
{ System.out.println("Hello World");
}
}
Control Statement

• Selection Statements
– If and switch
• Iteration Statements
– While, do-while, for and nested loops
• Jump Statements
– Break, continue and return
if-else
if(conditional_statement){
statement to be executed if conditions becomes
true
}else{
statements to be executed if the above condition
becomes false
}

Example
int a = 3, b = 4;
if (a >b) a =1;
else b = 1
If Else example
public class IfElseIfExample {   else if(marks>=70 && marks<80){  
public static void main(String[] args) {           System.out.println("B grade");  
    int marks=65;  
    }  
      
    if(marks<50){       else if(marks>=80 && marks<90){ 
        System.out.println("fail");    
    }           System.out.println("A grade");  
    else if(marks>=50 && marks<60){       }else if(marks>=90 && marks<100
        System.out.println("D grade");   ){  
    }           System.out.println("A+ grade"); 
    else if(marks>=60 && marks<70){    
        System.out.println("C grade");  
    }else{  
    }  
             System.out.println("Invalid!");  
    }  
}  
}  
Switch
switch(byte/short/int){
case expression:
statements
case expression:
statements
default:
statement
}
while - loop
while(condition_statement🡪true){
Statements to be executed when the condition
becomes true and execute them repeatedly until
condition becomes false.
}
E.g.
int x =2;
while(x>5){
system.out.println(“value of x:”+x);
x++;
}
do while - loop
do{
statements to be executed at least once without
looking at the condition.
The statements will be exeucted until the condition
becomes true.
}while(condition_statement);
for - loop

for(initialization; condition; increment/decrement){


statements to be executed until the condition
becomes false
}
E.g:
for(int x=0; x<10;x++){
System.out.println(“value of x:”+x);
}
break

Break is used in the loops and when executed, the


control of the execution will come out of the loop.

for(int i=0;i<50;i++){
if(i%13==0){
break;
}
System.out.println(“Value of i:”+i);
}
return
return statement can be used to cause execution
to branch back to the caller of the method.
Access Specifiers
In Java, access modifiers are used to set the accessibility (visibility) of classes,
interfaces, variables, methods, constructors, data members, and the setter
methods.
class Animal
{
public void method1()
{…}
private void method2()
{...}
}

In the above example, we have declared 2 methods: method1() and method2().


Here,
method1 is public - This means it can be accessed by other classes.
method2 is private - This means it can not be accessed by other classes.
Note the keyword public and private. These are access modifiers in Java. They
are also known as visibility modifiers.
Types of Access Modifier

There are four access modifiers keywords in Java and they are:
Arrays in Java
Creating, initializing, and accessing an Array
One-Dimensional Arrays:
type var-name[];
OR
type[] var-name;

Instantiating an Array in Java


var-name = new type [size];

Example: 
int intArray[]; //declaring arrayintArray = new int[20]; // allocating memory to array  
OR 
int[] intArray = new int[20]; // combining both statements in one
Example
Example
class GFG {
    public static void main(String[] args)
    {
        // declares an Array of integers.
        int[] arr;
        // allocating memory for 5 integers.
        arr = new int[5];
        // initialize the first elements of the array
        arr[0] = 10;
        // initialize the second elements of the array
        arr[1] = 20;
        // so on...
        arr[2] = 30;
        arr[3] = 40;
        arr[4] = 50;
        // accessing the elements of the specified array
        for (int i = 0; i < arr.length; i++)
            System.out.println("Element at index " + i
                               + " : " + arr[i]);
Two-Dimensional Array

In Java, a two-dimensional array is stored in the form of rows and columns and is
represented in the form of a matrix.
The general declaration of a two-dimensional array is,
data_type [] [] array_name;

data_type = data type of elements that will be stored in an array.


array_name = name of the two-dimensional array.

OR

data_type [] [] array_name = new data_type[row_size][column_size];


Example
public class Main
{
    public static void main(String[] args) {
        int[][] myarray = new int[2][2];
        myarray[0][0] = 1;
        myarray[0][1] =  myarray[1][0] =  0;
        myarray[1][1] = 1;
        System.out.println("Array elements are:");
        System.out.println(myarray[0][0] + " " +myarray[0][1]);
        System.out.println(myarray[1][0] + " " +myarray[1][1]);
    }
Naming Convention
All Java components require names. Names used for classes, variables
and methods are called identifiers. In Java, there are several points to
remember about identifiers. They are as follows -
All identifiers should begin with a letter (A to Z or a to z), currency
character ($) or an underscore (_).
After the first character, identifiers can have any combination of
characters.
A keyword cannot be used as an identifier.
Most importantly, identifiers are case sensitive.
Examples of legal identifiers: age, $salary, _value, __1_value.
Examples of illegal identifiers: 123abc, -salary.
A class name should start from a capital case letter and long names
should use camel casing. For example: TaxationDepartment
Object name should start from lower case letter and long names
should use camel casing. For example: taxationDepartment
Packages
A java package is a group of similar types of classes,
interfaces and sub-packages.
Package in java can be categorized in two form, built-in
package and user-defined package.
There are many built-in packages such as java, lang, awt,
javax, swing, net, io, util, sql etc.
Packages
Advantage of Java Package
1) Java package is used to categorize the classes and
interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
Packages

There are three ways to access the package from outside the package.
import package.*;
import package.classname;
fully qualified name.

You might also like