Module-1_Part-1_P
Module-1_Part-1_P
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
• 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
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 {
• 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
• continue