Principles of Oops.: Procedure Oriented Programming
Principles of Oops.: Procedure Oriented Programming
Principles of Oops.: Procedure Oriented Programming
Programming (OOP)
Procedure Oriented Programming Object Oriented Programming
Divided Into In POP, program is divided into small parts In OOP, program is divided into parts called objects.
called functions.
Importance In POP,Importance is not given to data but to functions In OOP, Importance is given to the data rather than
as well as sequence of actions to be done. procedures or functions because it works as a real world.
Approach POP follows Top Down approach. OOP follows Bottom Up approach.
Access POP does not have any access specifier. OOP has access specifiers named Public, Private, Protected,
Specifiers etc.
Data Moving In POP, Data can move freely from function to function In OOP, objects can move and communicate with each other
in the system. through member functions.
Expansion To add new data and function in POP is not so easy. OOP provides an easy way to add new data and function.
Data Access In POP, Most function uses Global data for sharing that In OOP, data can not move easily from function to
can be accessed freely from function to function in the function,it can be kept public or private so we can control
system. the access of data.
Data Hiding POP does not have any proper way for hiding data so it OOP provides Data Hiding so provides more security.
is less secure.
Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the form of Function
Overloading and Operator Overloading.
Examples Example of POP are : C, VB, FORTRAN, Pascal. Example of OOP are : C++, JAVA, VB.NET, C#.NET.
PRINCIPLES OF OOPS.
1. Encapsulation:
Encapsulation means that the internal representation of an object is
generally hidden from view outside of the object’s definition. Typically, only
the object’s own methods can directly inspect or manipulate its fields.
Encapsulation is the hiding of data implementation by restricting access to
accessors and mutators.
An accessor is a method that is used to ask an object about itself. In OOP,
these are usually in the form of properties, which have a get method, which
is an accessor method. However, accessor methods are not restricted to
properties and can be any public method that gives information about the
state of the object.
A Mutator is public method that is used to modify the state of an object,
while hiding the implementation of exactly how the data gets modified. It’s
the set method that lets the caller modify the member data behind the
scenes.
Hiding the internals of the object protects its integrity by preventing users
from setting the internal data of the component into an invalid or
inconsistent state. This type of data protection and implementation
protection is called Encapsulation.
A benefit of encapsulation is that it can reduce system complexity.
2. Abstraction
Data abstraction and encapuslation are closely tied together, because a
simple definition of data abstraction is the development of classes, objects,
types in terms of their interfaces and functionality, instead of their
implementation details. Abstraction denotes a model, a view, or some other
focused representation for an actual item.
“An abstraction denotes the essential characteristics of an object that
distinguish it from all other kinds of object and thus provide crisply defined
conceptual boundaries, relative to the perspective of the viewer.” — G.
Booch
In short, data abstraction is nothing more than the implementation of an
object that contains the same essential properties and actions we can find
in the original object we are representing.
3. Inheritance
Inheritance is a way to reuse code of existing objects, or to establish a
subtype from an existing object, or both, depending upon programming
language support. In classical inheritance where objects are defined by
classes, classes can inherit attributes and behavior from pre-existing
classes called base classes, superclasses, parent classes or ancestor
classes. The resulting classes are known as derived classes, subclasses or
child classes. The relationships of classes through inheritance gives rise to
a hierarchy.
Subclasses and Superclasses A subclass is a modular, derivative class
that inherits one or more properties from another class (called the
superclass). The properties commonly include class data variables,
properties, and methods or functions. The superclass establishes a
common interface and foundational functionality, which specialized
subclasses can inherit, modify, and supplement. The software inherited by
a subclass is considered reused in the subclass. In some cases, a subclass
may customize or redefine a method inherited from the superclass. A
superclass method which can be redefined in this way is called a virtual
method.
4. Polymorphism
Polymorphism means one name, many forms. Polymorphism manifests
itself by having multiple methods all with the same name, but slightly
different functionality.
There are 2 basic types of polymorphism. Overridding, also called run-time
polymorphism. For method overloading, the compiler determines which
method will be executed, and this decision is made when the code gets
compiled. Overloading, which is referred to as compile-time polymorphism.
Method will be used for method overriding is determined at runtime based
on the dynamic type of an object.
class B extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside B's m1 method");
}
}
class C extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside C's m1 method");
}
}
// Driver class
class Dispatch
{
public static void main(String args[])
{
// object of type A
A a = new A();
// object of type B
B b = new B();
// object of type C
C c = new C();
Explanation :
The above program creates one superclass called A and it’s two subclasses B and C. These subclasses
overrides m1( ) method.
1. Inside the main() method in Dispatch class, initially objects of type A, B, and C are declared.
2. A a = new A(); // object of type A
7. Now we are assigning a reference to each type of object (either A’s or B’s or C’s) to ref, one-
by-one, and uses that reference to invoke m1( ). As the output shows, the version of m1( )
executed is determined by the type of object being referred to at the time of the
call.
8. ref = a; // r refers to an A object
// class A
class A
{
int x = 10;
}
// class B
class B extends A
{
int x = 20;
}
// Driver class
public class Test
{
public static void main(String args[])
{
A a = new B(); // object of type B
10
Explanation : In above program, both the class A(super class) and B(sub class) have a common
variable ‘x’. Now we make object of class B, referred by ‘a’ which is of type of class A. Since variables
are not overridden, so the statement “a.x” will always refer to data member of super class.
Static binding is done during compile-time while dynamic binding is done during run-time.
private, final and static methods and variables uses static binding and bonded by compiler while
overridden methods are bonded during runtime based upon type of runtime object
Abstraction and Encapsulation are two important Object Oriented Programming (OOPS) concepts.
Encapsulation and Abstraction both are interrelated terms.
Encapsulate means to hide. Encapsulation is also called data hiding.You can think Encapsulation like
a capsule (medicine tablet) which hides medicine inside it. Encapsulation is wrapping, just hiding
properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the
data from the outside the world. Class is the best example of encapsulation.
Abstraction refers to showing only the necessary details to the intended user. As the name suggests,
abstraction is the "abstract form of anything". We use abstraction in programming languages to make
abstract class. Abstract class represents abstract view of methods and properties of class.
1. Abstraction is implemented using interface and abstract class while Encapsulation is implemented
using private and protected access modifier.
2. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used
in an appropriate manner) by preventing programmers from accessing data in a non-intended manner.
Through encapsulation, only a predetermined group of functions can access the data. The collective
term for datatypes and operations (methods) bundled together with access restrictions (public/private,
etc.) is a class.
3. Example of Encapsulation
Class Encapsulation
}
}
4. Example of Abstraction
//Implement it
Comparison Chart
BASIS FOR
INHERITANCE POLYMORPHISM
COMPARISON
existing class.
BASIS FOR
INHERITANCE POLYMORPHISM
COMPARISON
function/methods.
time(overriding).
hierarchical inheritance
Example The class 'table' can The class 'study_table' can also
Bytecode is platform-independent, bytecodes compiled by a compiler running in windows will still run
in linux/unix/mac. Machine code is platform-specific, if it is compiled in windows, it will run ONLY in
windows.
JAVA DOES NOT SUPPORT DESTRUCTORS- Java does not support destructor in
the way that c++ provides. C++ has direct destrcors that we can write in our code and it
will clear object once object goes out of scope. But that is not case in Java, you can’t
simply predict when object is going out of scope and when to destroy object, to do this
java has concept called Garbage Collection. Garbage Collector takes care of clearing up
memory allocated to object. It has finalize method which is entirely dicretion of Garbage
Collector. So as in c++ ‘delete’ or ‘free()’ is used to release allocated memory in java that
work is done by finalize and Garbage Collector.So since Java is Garbage Collector
language it does not have destructor.
Java Variables
Java Variables or variables in any other programming language are containers, which
hold some value. Because Java is a strongly typed language, so every variable must be
declared and initialized or assigned before it is used. A variable, in the simplest way, is
declared by placing a valid type followed by the variable name. And so, don't forget to
place a semicolon at the end of every statement because it completes the statement.
Throughout the tutorial we will use terms 'variable' and 'field' interchangeably as they
refer to the same thing and there should be no confusion regarding that. Following
statements demonstrate variable declaration in Java
/* Book.java */
class Book
{
//instance variables
private String title;
private String publisher;
private int numOfPages;
/* DynamicInitializationDemo.java */
public class DynamicInitializationDemo
{
public static void main(String[] args)
{
//dynSqrt will be initialized when Math.sqrt
//will be executed at run time
double dynSqrt = Math.sqrt (16);
System.out.println("sqrt of 16 is : " + dynSqrt);
}
}
OUTPUT
======
sqrt of 16 is : 4.0
OUTPUT
======
Average age is : 86
In addition to the elevation of byte to int, Java defines several type promotion rules
that apply to expressions. First, all byte and short values are promoted to int, as just
described. Then, if one operand is a long, the whole expression is promoted to long. If
one operand is a float, the entire expression is promoted to float. If any of the
operands is double, the result is double.
Java Variables - Type Conversion and Type Casting
When value of one variable is assigned to another or a literal is assigned to a variable
then either automatic type conversion takes place or we have to type cast explicitly.
Automatic type conversion is done when either both sides (left and right) are of same
type or the left hand (destination) side is larger in size. The latter is called widening.
For example, assigning byte to short or int to long.
If we wish to assign incompatible types then we will have to type cast at our own and
according to the problem for example, int to short conversion will not be done
automatically, because short is smaller than int. In this case we will have to explicitly
cast it and this conversion is called narrowing.
Java is Statically Typed Language
Note that Java is a statically-typed language that means the type of a variable must be
known at compile time, as type checking is done during compilation of the code. The
main advantage static typing offers is that type checking is done by the compiler before
the program is executed. The compiler can therefore deduce whether or not a given
variable is capable of performing actions requested of it. For example, the following
piece of code will result into compilation error.
/* StaticTypedDemo.java */
public class StaticTypedDemo
{
public static void main(String[] args)
{
byte s = 90;
s = s + 2; // Type mismatch: cannot convert from int to byte
System.out.println(s);
}
}
ASCII defines 128 characters, which map to the numbers 0–127. Unicode defines (less
than) 221characters, which, similarly, map to numbers 0–221 (though not all numbers are
currently assigned, and some are reserved).
Unicode is a superset of ASCII, and the numbers 0–128 have the same meaning in
ASCII as they have in Unicode. For example, the number 65 means "Latin capital 'A'".
Because Unicode characters don't generally fit into one 8-bit byte, there are numerous
ways of storing Unicode characters in byte sequences, such as UTF-32 and UTF-8.
Unicode vs ASCII
ASCII and Unicode are two character encodings. Basically, they are
standards on how to represent difference characters in binary so that
they can be written, stored, transmitted, and read in digital media.
The main difference between the two is in the way they encode the
character and the number of bits that they use for each. ASCII
originally used seven bits to encode each character. This was later
increased to eight with Extended ASCII to address the apparent
inadequacy of the original. In contrast, Unicode uses a variable
bit encoding program where you can choose between 32, 16, and 8-
bit encodings. Using more bits lets you use more characters at the
expense of larger files while fewer bits give you a limited choice but
you save a lot of space. Using fewer bits (i.e. UTF-8 or ASCII) would
probably be best if you are encoding a large document in English.
One of the main reasons why Unicode was the problem arose from
the many non-standard extended ASCII programs. Unless you are
using the prevalent page, which is used by Microsoft and most
other software companies, then you are likely to encounter problems
with your characters appearing as boxes. Unicode virtually
eliminates this problem as all the character code points were
standardized.
Another major advantage of Unicode is that at its maximum it can
accommodate a huge number of characters. Because of this, Unicode
currently contains most written languages and still has room for even
more. This includes typical left-to-right scripts like English and even
right-to-left scripts like Arabic. Chinese, Japanese, and the many
other variants are also represented within Unicode. So Unicode won’t
be replaced anytime soon.
Summary:
1.ASCII uses an 8-bit encoding while Unicode uses a variable bit
encoding.
2.Unicode is standardized while ASCII isn’t.
3.Unicode represents most written languages in the world while
ASCII does not.
4.ASCII has its equivalent within Unicode
A symbolic constant is an "variable" whose value does not change during the
entire lifetime of the program. There are no symbolic constants in java. Final
variables serve as symbolic constants. A final variable
declaration is qualified with the reserved word final. The
variable is set to a value in the declaration and cannot be
reset. Any such attempt is caught at compile time.
They can’t be declared inside a method . they should be used only as class data
members in the beginning of the class.
Cast Operator:
1. Cast operator is used to convert numeric values from one numeric type to another or
to change an object reference to a compatible type.
2. Used to enable conversions that would normally be disallowed by the compiler. a
reference of any object can be cast to a reference of type Object
3. a reference to an object can be cast into a reference of type ClassName if the actual
class of the object, when it was created, is a subclass of ClassName.
4. a reference to an object can be cast into a reference of type InterfaceName if the class
of the object implements Interface, if the object is a subinterface of InterfaceName or if
the object is an array type and InterfaceName is the Cloneable interface.
The difference is that the short circuit operator doesn't evaluate the second operand if the
first operand is true, which the logical OR without short circuitalways evaluates both
operands.
{ {
{ {
} }
int sum(int a) }
} {
print num ;