UNIT 1
PROCEDURAL ORIENTED OBJECT ORIENTED
PROGRAMMING PROGRAMMING
In procedural programming, program In object oriented programming,
is divided into small parts program is divided into small parts
called functions. called objects.
Procedural programming follows top Object oriented programming follows
down approach. bottom up approach.
Procedural programming does not have Object oriented programming
any proper way for hiding data so it provides data hiding so it is more
is less secure. secure.
Procedural programming is based on Object oriented programming is
unreal world. based on real world.
Examples: C, FORTRAN, Pascal, Examples: C++, Java, Python, C#
Basic etc. etc.
• Object oriented programming (OOP) is a concept that
combines both the data and the functions that operate on
that data into a single unit called the object.
• SIMULA I (1962-65) and SIMULA 67 (1967) were
the first two object-oriented [Link] follows
bottom-up designtechnique.
• Class is a template that represents a
group of objects which share common
properties and relationships.
The following are the Concepts
OOP’s:
Object means a real word entity such as pen, chair,
table etc. Any entity that has state and behavior is
known as an object. Object can be defined as an
instance of a class.
A class is a collection of objects that have
identical properties, common behavior and shared
relationship.
Once class is defined, any number of objects of that class is
created.
Data refers to the process of
Abstraction
representing essential features without
including background details or explanations.
Data Encapsulation The wrapping of data and
functions into a single unit (class) is called data
encapsulation.
Inheritance is the process by which one object can acquire and
use the properties of another object.
The existing class is known as base class or super class.
The new class is known as derived class or sub class.
The derived class shares some of the properties of the base
class. Therefore a code from a base class can be reused by a
derived class.
Overloading allows objects to have different
depending upon context.
meaning
🞂 There are two types of overloading
🞂 Operator Overloading Function
🞂 Overloading
Polymorphism:
The ability of an operator and function to take multiple forms
Dynamic binding:
◦ Binding is the process of connecting one program
to another.
◦ Dynamic binding is the process of linking the
procedure call to a specific sequence of code or
function at run time or during the execution of the
program.
Message Passing:
◦ Message passing involves specifying the name of
the object, the name of the function (message) and
the information to be sent.
Computer graphics applications.
CAD/CAM software
Object-oriented database.
Real-time systems.
Artificial intelligence and expert systems.
Client-Server Systems.
Simple
Secure
Portable
Object Oriented
Robust-Strong
Multithread
Platform Independent
Distributed
Dynamic
Part-1 Package Declaration
Ex: package package_name;
• Part-2 Import Declaration Ex: import
[Link].*;
• Part-3 Top level class and interface declaration
Ex: import [Link].*;
Part-1:
◦ The package statement should be the first line in the source file.
◦ There can be only one package statement in each source file.
◦ It is a good practice to use names of packages with lower case to
avoid any conflicts.
Part-2:
◦ To import a single class, we specify the name of the class.
◦ To import all classes, we specify *.
Compile:
◦ javac [Link]
Run:
◦ java ExampleProgram
Single Line Comment (//)
Multi Line Comment (/* . . .. */)
Documentation Comment(/** …….*/)
Public – Any one can access
Private – Only inside can access
Protected – Allows access by all subclasses of the
class in a program
Default Access Specifier - Allows access by other
code in the same package
Class Variables
◦ Member Variable
◦ Member Function
◦ Constructor
Creating Object
Variable declaration:
◦ Data_type variable[= values][=value];
Types of variables:
◦ Local variable
◦ Instance Variable
◦ Static variable
Local variables are declared inside the methods,
constructors, or blocks
Local variables are created when the method,
constructor, or block is entered
Local variable will be destroyed once it exits the
methods, constructors
Access Specifiers can not be used
A variable declared inside the class but outside the method
is called instance variable.
Instance variables are created when an object is created with
the use of the new keyword
Access Specifiers can be used
Instance variable have default variables
Values can be assigned during the declaration or within the
constructor.
Instance variables cannot be declared as static
Class variable also known as static variables are
declared with the static keyword.
Only one copy of each static variable is created.
An array is a group of like-typed variables that are referred
to by a common name.
Following are some important point about Java arrays.
◦ In Java all arrays are dynamically allocated
◦ The variables in the array are ordered and each have an
index beginning from 0.
◦ The size of an array must be specified by an int value and
not long or short.
dataType[ arr; (or)
] (or)
dataType []arr;
dataType arr[];
Defining of an Array in java
arrayRefVar=new
datatype[size];
Ex. int marks[]=new int[5];
class Testarray {
public static void main(String args[]){
int a[]=new int[5];//declaration and
instantiation
a[0]=10;// Output: 10
initialization
a[1]=20; 20
70
a[2]=70;
40
a[3]=40; 50
a[4]=50;
for(int i=0;i<[Link];i+
+)
[Link](a[i]);
}}
In such case, data is stored in row and column based
index (also known as matrix form).
Syntax to Declare Multidimensional Array in java
dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar;
(or)
dataType arrayRefVar[][]; (or) dataType []arrayRefVar[];
Example to instantiate Multidimensional Array in java
class Testarray3{
public static void main(String args[])
{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
{ [Link](arr[i][j]+" "); Output:1 2 3
} 24 5
[Link](); 44 5
}
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 or existing packages
◦ User-defined package.
🞄 Programmers can define their own packages to bundle group of
classes/interfaces.
🞄 It is a good practice to group related classes implemented by you.
Built-in packages:
◦ java
◦ lang
◦ awt
◦ javax
◦ swing
◦ net
◦ io
◦ util
◦ sql
Advantages of using a package:
◦ Reusability
◦ Better Organization
◦ Name Conflicts
Name for the package and include a package statement
along with that name at the top of every source file.
The package statement should be the first line in the
source file.
There can be only one package statement in each source
file.
User Defined Package: package
letmecalculate; public class Calculator
{
public int add(int a, int b)
{
return a+b;
}
public static void main(String args[])
{
Calculator obj = new Calculator();
[Link]([Link](10, 20));
}
}
There are three ways to access the package from
outside the package.
◦ import package.*;
◦ import [Link];
◦ fully qualified name.
◦ Static import
Use this package in another
program:
import [Link];
public class Demo
{
public static void main(String args[])
{
Calculator obj = new Calculator();
[Link]([Link](100, 200));
}
}
Inheritance – Super classes- sub classes –Protected members
–constructors in sub classes- the Object class – abstract
classes and methods- final methods and classes – Interfaces
–defining an interface, implementing interface, differences
between classes and interfaces and extending interfaces -
Object cloning -inner classes, Array Lists – Strings.
It is a mechanism in which one object acquires all the
properties and behaviours of a parent object.
You can create new classes that are built upon existing
classes.
When you inherit from an existing class, you can reuse
methods and fields of the parent class.
Moreover, you can add new methods and fields in your
current class also.
Inheritance represents the IS-A relationship which is also
known as a parent-child relationship.
Super Class/Parent Class:
◦ Superclass is the class from where a subclass inherits the
features.
◦ It is also called a base class or a parent class.
Sub Class/Child Class:
◦ Subclass is a class which inherits the other class.
◦ It is also called a derived class, extended class, or child class.
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making
a new class that derives from an existing class.
super() if present, must always be the first statement
executed inside a subclass constructor.
It clearly tells you the order of invocation of
constructors in a class hierarchy.
Constructors are invoked in the order of their
derivation
When we invoke a super() statement from within a
subclass constructor, we are invoking the immediate
super class’ constructor
This holds good even in a multi level hierarchy
Remember, super( ) can only be given as the first
statement within a constructor
The Object class is the parent class of all the classes
in java by default.
In other words, it is the topmost class of java.
A class that is declared as abstract keyword is known as
abstract class.
We cannot instantiate object for the abstract class.
Create a superclass that only defines a generalized form
of method that will be shared by all of its subclasses
Leaving it to each subclass to provide for its own specific
implementations
The importance of abstract classes:
◦ They define a generalized form (possibly some
generalized methods with no implementations) that will
be shared by
all of its subclasses.
◦ So that each subclass can provide specific
implementations of such methods.
Abstract method – It’s a method declaration with no
definition
A mechanism which shall ensure that a subclass must
compulsorily override such methods.
Abstract method in a superclass has to be overridden by
all its subclasses.
To use an abstract method, use this general form:
abstract type name(parameter-list);
area method of Figure class made Abstract.
public abstract int area();
Any class that contains one or more abstract methods , class must
also be declared abstract
It is perfectly acceptable for an abstract class to implement a
concrete method
That is, an abstract class cannot be instantiated with the new
keyword
Any subclass of an abstract class must either implement all of the
abstract methods in the superclass, or be itself declared
abstract.
If subclass (child class) has the same method as declared in the
parent class, it is known as method overriding in Java.
Usage of Java Method Overriding:
◦ Method overriding is used to provide the specific
implementation of a method which is already provided by its
superclass.
◦ Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
◦ The method must have the same name as in the parent
class
◦ The method must have the same parameter as in the
parent class.
◦ There must be an IS-A relationship (inheritance).
The final keyword used in context of behavioural
restriction on:
◦ Variables
◦ Methods
◦ Classes
Using final on variables to make them behave as constants
which we have seen in earlier module.
When a variable is made final – it can be initialized only
once either by
◦ Declaration and initialization
Ex: final int x=10;
◦ Using constructor
Using final to Prevent Overriding
◦ While method overriding is one of the most powerful
feature of object oriented design, there may be times when
you will
want to prevent certain critical methods in a superclass from
being overridden by its subclasses.
◦ This can be achieved by declaring such critical methods as
final.
Using final to Prevent Inheritance
◦ Sometimes you will want to prevent a class from being
inherited.
◦ This can be achieved by preceding the class declaration with
final.
◦ Declaring a class as final implicitly declares all of its methods
as final too.
◦ It is illegal to declare a class as both abstract and final
An interface is a named collection of method declarations
(without implementations)
◦ An interface can also include constant declarations
◦ An interface is syntactically similar to an abstract class
◦ An interface is a collection of abstract methods and
final variables
◦ A class implements an interface using the implements
clause
Interfaces allow you to implement common behaviours in
different classes that are not related to each other.
Java does not support multiple inheritance
This is a constraint in class design, as a class cannot
achieve the functionality of two or more classes at a time
Interfaces help us make up for this loss as a class can
implement more than one interface at a time
All the methods that are declared within an interface are
always, by default, public and abstract
Any variable declared within an interface is always, by
default, public static and final
File [Link]:
modifier interface InterfaceName
{
constants declarations; methods signatures;
}
Modifier is public or not used.
File [Link]:
modifier Class ClassName implements
InterfaceName
{
methods implementation;
}
Internal addition by the compiler
Understanding relationship between
classes and interfaces
To declare a class that implements an interface,
you include an implements keyword in the class
declaration.
Your class can implement more than one interface,
so the implements keyword is followed by a comma-
separated list of the interfaces implemented by the
class.
An interface can extend another interface
in the same way that a class can extend
another class.
The extends keyword is used to extend an
interface.
The object cloning is a way to create exact copy of
an object.
The clone() method is defined in the Object class.
The clone() method of Object class is used to clone
an object.
Syntax of the clone() method is as follows:
Java Cloneable interface and clone() method
◦ Every language which supports cloning of objects has its own
rules and so does java.
◦ In java, if a class needs to support cloning it has to do following
things:
🞄 You must implement Cloneable interface.
🞄 You must override clone() method from Object class.
Types of clone:
◦ Java Shallow Copy
◦ Java Deep Copy
◦ Lazy Copy
• Java Shallow Copy:
🞄 Generally clone() method of an object, creates a new instance of the
same class and copies all the fields to the new instance and returns it.
🞄 Object class provides a clone method and provides support for the
shallow copy.
• Java Deep Copy:
🞄 Deep copy of an object will have exact copy of all the
fields of original object just like shallow copy.
🞄 But in additional, if original object has any references
to other objects as fields, then copy of those objects are
also created by calling clone() method on them.
🞄 That means clone object and original object will be
100% disjoint.
Lazy Copy:
◦ combination of both shallow copy and deep
copy.
Java inner class or nested class is a class which is
declared inside the class or interface.
We use inner classes to logically group classes and
interfaces in one place so that it can be more readable and
maintainable.
Additionally, it can access all the members of outer class
including private data members and methods.