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

Module-1_Part-1_P

The document provides an overview of Object-Oriented Programming (OOP) fundamentals, including key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and dynamic binding. It also highlights the benefits of OOP, such as code reusability and security, and discusses the Java programming language's features, including its platform independence and robustness. Additionally, the document covers basic Java syntax, data types, control statements, and input handling through various methods.

Uploaded by

o24558769
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module-1_Part-1_P

The document provides an overview of Object-Oriented Programming (OOP) fundamentals, including key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and dynamic binding. It also highlights the benefits of OOP, such as code reusability and security, and discusses the Java programming language's features, including its platform independence and robustness. Additionally, the document covers basic Java syntax, data types, control statements, and input handling through various methods.

Uploaded by

o24558769
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

Fundamentals of Object

Oriented Programing
Basic Concepts of Object Oriented
Programming
• Objects and Classes
• Data Abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
Class
• A class is an entity that determines how an object will behave and
what the object will contain.
• In other words, it is a blueprint or a set of instruction to build a
specific type of object.
• A class is used to bind data as well as methods together as a single
unit.
➢Attributes - things that the object stores data in, generally variables.
➢Methods - Functions and Procedures attached to an Object and allowing
the object to perform actions
Object
• Objects are the basic units of object-oriented programming.
• An object is a component of a program that knows how to perform
certain actions and how to interact with other elements of the program.
• An object is the instance of the class, which helps programmers to use
variables and methods from inside the class.
Data Abstraction and Encapsulation
• Encapsulation : Wrapping
up data and member
functions (methods) into a
single unit i.e. class

• Data Abstraction:
Abstraction is the process
of hiding out the working
style of an object and
showing only the required
information of the object in
understandable manner.
• Encapsulation is defined as
the wrapping up of data
under a single unit.
• It is the mechanism that
binds together code and the
data it manipulates.
• In a different way,
encapsulation is a protective
shield that prevents the data
from being accessed by the
code outside this shield
Abstraction in OOP
• Objects in an OOP language provide an abstraction that
hides the internal implementation details. Similar to the
coffee machine in your kitchen, you just need to know
which methods of the object are available to call and
which input parameters are needed to trigger a specific
operation. But you don’t need to understand how this
method is implemented and which kinds of actions it has
to perform to create the expected result.
Inheritance
• Creating a new class from an existing class is called Inheritance.
• Advantage of inheritance is reusability of the code.
Polymorphism
• Polymorphism means having more than one form.
• Polymorphism is a concept by which we can perform a single
action by different ways.
• Polymorphism is achieved with the help of overloading and
overriding.
Dynamic Binding
• Dynamic binding means that the code associated with a
given procedure call is not known until the time of the call
at runtime.
• It is associated with the polymorphism and inheritance.
Message Communication
• An object oriented program consists of a set of objects that
communicate with each other.
• Object communicate with one another by sending and receiving
information much the same way as people pass messages to one
another.
• A message for an object is a request for execution of a
procedure(method).
Benefits of OOP
• Through inheritance, we can eliminate redundant code and extend the
use of existing classes.
• We can built programs from standard working modules that
communicate with one another rather than, having to start writing the
code from scratch. This leads to saving of development time and higher
productivity.
• The principle of data hiding helps the programmers to built secure
program that can’t be invaded by code in other parts of the program.
• It is possible to have multiple objects to coexist without any interference.
Benefits of OOP
• It is easy to partition the work in a project based on
objects.
• The data-centered design approach enables us to
capture more details of the model in an
implementable form.
• Object-oriented systems can be easily upgraded from
small to large system
• Message passing technique for communication
between objects make the interface descriptions with
external system much simpler.
• Software complexity can be easily managed.
Applications of OOP
• User interface design such as windows, menu ,…
• Real Time Systems
• Simulation and Modelling
• Object oriented databases
• AI and Expert System
• Neural Networks and parallel programming
• Decision support and office automation system
Overview of Java
Language
The Java Programming Language

There are four platforms of the Java programming language:


• Java Platform, Standard Edition (Java SE)
• Java Platform, Enterprise Edition (Java EE)
• Java Platform, Micro Edition (Java ME)
• JavaFX
Java History
• Java SE 9 (2017)
• Java SE 10 (2018)
Java SE 10 was released to remove primitive data types and move towards 64-bit
addressable arrays to support large data set
• java SE 11 (2019)
bug fixing
Java Features
Java Features
Simple
• Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun Microsystem, Java language is a simple programming
language because:
• Java syntax is based on C++ (so easier for programmers to learn it after C++).
• Java has removed many complicated and rarely-used features, for example, explicit pointers, operator overloading, etc.
Object-oriented
• Basic concepts of OOPs are:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Platform Independent
• Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a
write once, run anywhere language. A platform is the hardware or software environment in which a program runs.
• Java code can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. Java code is compiled by the compiler and
converted into bytecode. This bytecode is a platform-independent code because it can be run on multiple platforms, i.e., Write Once and Run Anywhere
(WORA).
Secured
• Java is best known for its
security. With Java, we can
develop virus-free systems.
Java is secured because:
• No explicit pointer
• Java Programs run inside a
virtual machine sandbox
Object Oriented
• Java is a true object oriented language.
• All program code and data reside within objects and classes.
• Java comes with an extensive set of classes, arranged in
packages, that we can use in our programs by inheritance.
Compiled and Interpreted
1.Java compiler translates source code into bytecode instructions.
2.Bytecodes are not machine instructions therefore java interpreter
generates machine code that can be directly executed by the machine
i.e. running the java program.
Platform Independent
• A program or technology is said to be platform independent if and only
if which can run on all available operating systems with respect to its
development and compilation. (Platform represents O.S).
Architectural Neutral
• Architecture represents processor.
• A Language or Technology is said to be Architectural neutral which can
run on any available processors in the real world without considering
there architecture and vendor (providers) irrespective to its
development and compilation.
Portable and Multithreaded
• Portable
▪ If any language supports platform independent and
architectural neutral feature known as portable.
▪ The languages like C, CPP, Pascal are treated as non-portable
language.
▪ It is a portable language.

• Multithreaded
▪ A flow of control is known as thread.
▪ When any Language execute multiple thread at a time that
language is known as multithreaded Language.
▪ It is multithreaded Language.
Distributed
• Java is designed as distributed language for creating applications on network.
• It has ability to share both data and programs.
• Java applications can open and access remote objects on Internet as easily as they
can do in a local system.
• This enables multiple programmers at multiple remote location to collaborate and
work together on a single project.
Robust and Secure
• Robust
▪ Simply means of Robust is strong.
▪ It is robust or strong Programming Language because of its capability
to handle Run-time Error, automatic garbage collection, lack of
pointer concept, Exception Handling.
▪ All these points makes It robust Language.
• Secure
▪ It is more secured language compare to other language.
▪ In this language all code is covered into byte code after compilation
which is not readable by human.
Dynamic
• It support Dynamic memory allocation due to this memory
wastage is reduce and improve performance of application.
• The process of allocating the memory space to the input of the
program at a run-time is known as dynamic memory allocation.
High performance
• This language uses Bytecode which is more faster than ordinary
code so Performance of this language is high.
• Garbage collector, collect the unused memory space and improve
the performance of application.
• It have no pointers so that using this language we can develop an
application very easily.
• It support multithreading, because of this time consuming process
can be reduced to execute the program.
Constants, variables and
data types
Constants
Variables
A variable is an identifier that denotes a storage location used to store
data value.
Conditions for using variable names:
• They must not begin with a digit.
• Uppercase and Lowercase are distinct.
• It should not be a keyword.
• White space is not allowed.
• Variable names can be of any length.
DIFFERENCE BETWEEN C AND JAVA
Data Types
Integer types
Floating Point type
Type Casting
• The process of converting one data type to another is called casting.
• Casting is often necessary when a method returns a type different than the
one we require.
Syntax : type variable1=(type) variable2;
• Dot operator
▪ The dot operator is used to access the instance variable and
methods of class objects.
▪ E.g. person.age
Basic Java Program

Compilation: javac filename.java


Execution: java classname
Command line Arguments
• The java command-line argument is an argument i.e. passed at the time of running
the java program.
• The arguments passed from the console can be received in the java program and it
can be used as an input.
• So, it provides a convenient way to check the behaviour of the program for the
different values. You can pass N (1,2,3 and so on) numbers of arguments from the
command prompt.
example
class CommandLine
{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}

Execution:
Java CommandLine c c++ java

Output:
C
C++
java
Java program for addition of two number using command line
argument

import java.util.*;
class CommandLine
{
public static void main(String args[])
{
int a,b,c=0;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=a+b;
System.out.println("c= "+c);
}
}
command line argument

• import java.util.*;
• class commnadline {

• // Main driver method


• public static void main(String[] args)
• {
• // Printing the first argument
• System.out.println(args[0]);
• }
•}
Java.util Package in Java
• https://www.geeksforgeeks.org/java-util-package-java/
Using Scanner class for input
import java.util.Scanner;
class ScannerTest
{
public static void main(String args[]) • Output
{ Enter your rollno
Scanner sc=new Scanner(System.in); 111
System.out.println("Enter your rollno"); Enter your name
int rollno=sc.nextInt(); Ratan
System.out.println("Enter your name"); Enter your fee
String name=sc.next(); 450000
System.out.println("Enter your fee"); Rollno:111 name:Ratan fee:450000
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+"
fee:"+fee);
}
}
Using BufferedReader for input
import java.io.*;
public class BufferedReaderExample
{
public static void main(String args[])throws Output:
Exception Enter your name
{ Ramesh
InputStreamReader r=new Enter your Roll number
InputStreamReader(System.in); 1
BufferedReader br=new BufferedReader(r); name: Ramesh
System.out.println("Enter your name"); Roll number: 1
String name=br.readLine();
System.out.println("Enter your Roll number");
int ro_num=Integer.parseInt(br.readLine());
System.out.println("name: "+name);
System.out.println("Roll number: "+ro_num);
}
}
BufferedReader class

• The Java.io.InputStreamReader class is a bridge from byte


streams to character streams.It reads bytes and decodes them
into characters using a specified charset.

• The Java.io.BufferedReader class reads text from a character-


input stream, buffering characters so as to provide for the
efficient reading of characters, arrays, and lines.
Decision making , Branching
and Looping
Forms of if….
• if statement

• if-else statement

• nested if-else statement

• else-if ladder

• switch statement
Simple If statement
Syntax:

if(condition)
{
statements
}
statement_x;
If else statement
Syntax:

if(condition)
{
statement1;
}
else
{
statement2;
}
statement x;
Example
class Test
{
public static void main(String args[])
{
int x = 30;
if( x < 20 )
{
System.out.println("This is if statement");
}
else
{
System.out.println("This is else statement");
}
}
}
Nested if else
Example class Test
{
public static void main(String args[])
{
int x = 30;
int y = 10;
if( x == 30 )
{
if( y == 10 )
{
System.out.println("X = 30 and Y = 10");
}
else
{
System.out.println("something else");
}
}
}
}
Else-if ladder
Syntax:

if(test_condition1)
{
statement 1;
}
else if(test_condition2)
{
statement 2;
}
else if(test_condition3)
{
statement 3;
}
else if(test_condition4)
{
statement 4;
}
else
{
statement x;
}
Example class Test
{
public static void main(String args[])
{
int x = 30;
if( x == 10 )
{ System.out.println("Value of X is 10"); }
else if( x == 20 )
{ System.out.println("Value of X is 20"); }
else if( x == 30 )
{ System.out.println("Value of X is 30"); }
else
{
System.out.println("This is else statement");
}
}
}
Switch statement
Syntax:
switch(expression)
{
case constant-expression : statement(s);
break;
case constant-expression : statement(s);
break;
default : default_block; //optional
}
Example
class Test
{
public static void main(String args[])
{
char grade = 'C';
switch(grade)
{
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
contd….
contd….
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
Types of control statements
• The while statement

• The do-while statement

• The for statement


While loop
Syntax:

Initialize the variable;


while(condition)
{
statements;
increment/decrement;
}
Example
class Test
{
public static void main(String args[])
{
int x = 10;
while( x < 20 )
{
System.out.println("value of x : " + x );
x++;
System.out.println("\n");
}
}
}
Do-while loop
Syntax:

initialize the variable;


do
{
statements;
increment/decrement;
}
while(condition);
Example
class Test
{
public static void main(String args[])
{
int x = 10;
do
{
System.out.println("value of x : " + x );
x++;
System.out.println("\n");
}while( x < 20 );
}
}
For loop
Syntax:

for(initialization; test_condition; increment/decrement)


{
code to be executed;
}
example
class Test
{
public static void main(String args[])
{
for(int x = 10; x < 20; x = x+1)
{
System.out.println("value of x : " + x );
System.out.println("\n");
}
}
}
Jumps in the loops
• Break

• continue

You might also like