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

UNIT 1 OOPS Self

Uploaded by

Aditi Goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

UNIT 1 OOPS Self

Uploaded by

Aditi Goel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT – I

Introduction to Object Oriented Programming


 Object-oriented programming (OOP) is a programming paradigm based on the concept
of "objects", which can contain data and code: data in the form of fields (often known as
attributes or properties), and code, in the form of procedures (often known as
methods).
 A feature of objects is that an object's own procedures can access and often modify the
data fields of itself. In OOP, computer programs are designed by making them out of
objects that interact with one another.
 OOP languages are diverse, but the most popular ones are class-based, meaning that
objects are instances of classes, which also determine their types.
 Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and
Mike Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first
working version. This language was initially called “Oak,” but was renamed “Java” in
1995. Between the initial implementation of Oak in the fall of 1992 and the public
announcement of Java in the spring of 1995, many more people contributed to the
design and evolution of the language.

Evolution of Java
 Researchers were thinking of a programming language which could be securable,
reusable and support all platforms and also acts as a class-based, object-oriented with
specifically designed dependencies to fix the drawbacks of existing languages like C and
C++. Where C is not secured and C++ does not have the advanced characteristic. It
intended to develop an application program which can be "write once and run
anywhere" (WORA), which means the compiled Java code can run on all platforms that
support Java.

History of Java
 Java is a high level general purpose object oriented language. Though it is associated
with the world of web, it is older than the origin of web. It was only developed keeping
in mind the consumer electronics and communications equipment’s.
 It came into existence as a web application, web services and a platform independent
programming language in 1990. In 1990 James Gosling, Bill Joy and their team began
developing a language called OAK. They wanted it mainly to control microprocessor
embedded in consumer items such as cable set up boxes, VCRs, toasters, PDA etc. To
serve these goals, OAK needed to be platform independent, extremely reliable and
compact. However, as of 1993, interactive TV and PDA markets had failed to take off.
Then the internet and web explosion began and Sun shifted the target market to
internet applications and changed the name of the project to java.
 By 1994 Sun’s HotJava browser appeared to locate and run applet programs on internet.
HotJava demonstrate the power of new language and makes java popular among the
internet. Sun release the first public implementation as java 1.0 in 1995. It promised
write once run anywhere (WORA) providing no run time cost on popular platform.
 On 13 November 2006, Sun released most of java as free and open source software
under the term of General Public license (GPL).
 On 8 May 2007, sun finished the process, making all of Java’s core code free and open-
source, aside from a small portion of code to which Sun did not hold the copyright.
 Earlier C++ was widely used to develop OOPs code but it was not platform independent
and needed to be recompiled for different CPUs. Java became very popular because of
its Write once Run Anywhere concept. Java is influenced by many programming
languages like C, C++, Smalltalk etc. and its borrowed some advance features from some
other programming languages.

Java is case sensitive or not?


Yes, Java is case-sensitive language. It is this way because of its heritage from C language. To
keep the language more familiar to what people were used to "in the day", they left it as
casesensitive. There is an added advantage since Java identifiers can be almost any Unicode
character in the programs. Unicode is a universal international standard character encoding
that is capable of representing most of the world's written languages such as American
Standard Code for Information Interchange (ASCII) for the United States, ISO 8859-1 for
Western European Language, GB18030 and BIG-5 for Chinese and KOI-8 for Russian and so on.

Objects and Classes


Languages that support object-oriented programming (OOP) typically use inheritance for code
reuse and extensibility in the form of either classes or prototypes. Those that use classes
support two main concepts:

 Classes – the definitions for the data format and available procedures for a given type or class
of object; may also contain data and procedures (known as class methods) themselves, i.e.
classes contain the data members and member functions
 Objects – instances of classes Objects sometimes correspond to things found in the real
world. For example, a graphics program may have objects such as "circle", "square", "menu".
An online shopping system might have objects such as "shopping cart", "customer", and
"product". Sometimes objects represent more abstract entities, like an object that represents
an open file, or an object that provides the service of translating measurements from U.S.
customary to metric. Each object is said to be an instance of a particular class (for example, an
object with its name field set to "Mary" might be an instance of class Employee). Procedures in
object-oriented programming are known as methods; variables are also known as fields,
members, attributes, or properties. This leads to the following terms:  Class variables – belong
to the class as a whole; there is only one copy of each one

 Instance variables or attributes – data that belongs to individual objects; every object has its
own copy of each one

 Member variables – refers to both the class and instance variables that are defined by a
particular class

 Class methods – belong to the class as a whole and have access only to class variables and
inputs from the procedure call

 Instance methods – belong to individual objects, and have access to instance variables for the
specific object they are called on, inputs, and class variables.

Objects are accessed somewhat like variables with complex internal structure, and in many
languages are effectively pointers, serving as actual references to a single instance of said
object in memory within a heap or stack. They provide a layer of abstraction which can be used
to separate internal from external code. External code can use an object by calling a specific
instance method with a certain set of input parameters, read an instance variable, or write to
an instance variable. Objects are created by calling a special type of method in the class known
as a constructor. A program may create many instances of the same class as it runs, which
operate independently. This is an easy way for the same procedures to be used on different
sets of data.

Abstraction
 Abstraction is the concept of object-oriented programming that "shows" only essential
attributes and "hides" unnecessary information.
 The main purpose of abstraction is hiding the unnecessary details from the users. Abstraction is
selecting data from a larger pool to show only relevant details of the object to the user.
 It helps in reducing programming complexity and efforts. It is one of the most important
concepts of OOPs.
 For example, Invention of flight is based on the flying mechanism learnt from birds. So flights are
derived from base class birds

Encapsulation
 Encapsulation is an object-oriented programming concept that binds together the data and
functions that manipulate the data, and that keeps both safe from outside interference and
misuse.
 Data encapsulation led to the important OOP concept of data hiding.
 This concept is also often used to hide the internal representation, or state, of an object from
the outside. This is called information hiding.
 The general idea of this mechanism is simple. If you have an attribute that is not visible from the
outside of an object, and bundle it with methods that provide read or write access to it, then
you can hide specific information and control access to the internal state of the object.

Difference between Abstraction and Encapsulation

Abstraction Encapsulation
Abstraction in Object Oriented Programming Encapsulation solves it implementation level.
solves the issues at the design level.
Abstraction in Programming is about hiding Encapsulation means binding the code and
unwanted details while showing most data into a single unit.
essential information.
Data Abstraction in Java allows focusing on Encapsulation means binding the code and
what the information object must contain. data into a single unit.

Inheritance
 The importance of inheritance should never be treated lightly because the aim of
inheritance is “reusability.”
 What inheritance does, as the name would imply, is that it allows the code written in a
class to be extended into another class. So, in inheritance, there exists a base class; the
class in which the code is written is to be reused. The next class that we would create
would have to be inherited from this base class to use all the functions and variables
associated with the base class. Whenever a class takes the properties of another class
(or inherits from a different class), all the members who are present in the base class
become a member of this new derived class.
 For example, Human heredity

Polymorphism
 The basic definition of the word polymorphism means having many forms. This
definition holds very accurately in explaining polymorphism in the context of
programming. In this paradigm, polymorphism takes the meaning of one function but
many forms.
 Polymorphism actually happens at compile time. Polymorphism at compile time is only
possible because of the concept of overloading, and at run time, the feature of
overriding makes polymorphism a reality. Overloading requires the code that you write
or the class’s function to be written more than once with different parameters but
having the same return type.
 It means that the arguments that you pass into the function can be different, and just by
looking at the final values which are passed into the function at run time, which form of
the function is to be called is decided.
 Generally, we see the class constructor be the most overloaded function.

Differences between Inheritance and Polymorphism


1. Inheritance is essentially making a class, and then having other classes in your program get
their feature form the already existing base class. However, polymorphism is an interface, and
because of being an interface, it can take different shapes and forms.

2. Inheritance is a property pertaining to just classes whereas; polymorphism extends itself into
any method and/or function.

3. Inheritance allows the derived class to use all the functions and variables declared in the
base class without explicitly defining them again. That is why we say that inheritance increases
the code reusability and reduces the length of code, which we would have to write if the
inheritance was absent. Whereas , polymorphism allows for the same function name to have
two very different codes. So, in a sense, instead of reducing the length of the code which we
would have to write, polymorphism is extending it further.

4. There are many forms that inheritance can take; you can be really creative with inheritance.
However, polymorphism can only be accomplished by two means, i.e., overloading and
overriding. You can still go very crazy while using polymorphism, but you are restricted to just
the two ways of implementing it into your writing code.
Characteristics of Java
The key characteristics were summed up by the Java team in the following list of buzzwords:

1. Simple

Java was designed to be easy for the professional programmer to learn and use effectively. If
you already understand the basic concepts of object oriented programming, learning Java will
be even easier. Best of all, if you are an experienced C++ programmer, moving to Java will
require very little effort. Because Java inherits the C/C++ syntax and many of the object-
oriented features of C++, most programmers have little trouble learning Java.

2. Object-Oriented

Java is true object oriented language. Almost “Everything is an Object” paradigm is followed. All
program code and data reside within objects and classes. The object model in Java is simple and
easy to extend. Java comes with an extensive set of classes, arranged in packages that can be
used in our programs through inheritance.

3. Distributed

Java is designed for distributed environment of the Internet. It is used for creating applications
on networks. Java applications can access remote objects on Internet as easily as they can do in
local system. Java enables multiple programmers at multiple remote locations to collaborate
and work together on a single project.

4. Compiled and Interpreted

Usually a computer language is either compiled or interpreted. Java combines both this
approach and makes it a two-stage system. Compiled: Java enables creation of a cross platform
programs by compiling into an intermediate representation called Java Byte code.

Interpreted: Byte code is then interpreted, which generates machine code that can be directly
executed by the machine that provides a Java Virtual machine.

5. Robust

It provides many features that make the program execute reliably in variety of environments.
Java is a strictly typed language. It checks code both at compile time and runtime. Java takes
care of all memory management problems with garbage-collection. Java, with the help of
exception handling captures all types of serious errors and eliminates any risk of crashing the
system.
6. Secure

Java provides a “firewall” between a networked application and your computer. When a Java
Compatible Web browser is used, downloading can be done safely without fear of viral
infection or malicious intent. Java achieves this protection by confining a Java program to the
java execution environment and not allowing it to access other parts of the computer.

7. Architecture Neutral

Java language and Java Virtual Machine helped in achieving the goal of “write once; run
anywhere, anytime, forever.” Changes and upgrades in operating systems, processors and
system resources will not force any changes in Java Programs.

8. Portable

Java provides a way to download programs dynamically to all the various types of platforms
connected to the Internet. It helps in generating Portable executable code.

9. High Performance

Java performance is high because of the use of byte code. The byte code was used, so that it
was easily translated into native machine code.

10. Multithreaded

Multi-threaded Programs handled multiple tasks simultaneously, which was helpful in creating
interactive, networked programs. Java run-time system comes with tools that support multi
process synchronization used to construct smoothly interactive systems.

11. Dynamic

Java is capable of linking in new class libraries, methods, and objects. It can also link native
methods (the functions written in other languages such as C and C++).

12. Interpreted

The interpreted feature in C++ is very slow because it is interpreted line by line, and makes the
system a slower way of running a program than one that has been compiled.

Components of Java
 Java Compiler
The Compiler is used to systemize the program and check against language syntax rules. It converts the
source code to byte code and then compiles them.

The Java compiler generates an architecture-neutral object file format (the compiled code – source
code and byte code) to be executable on many processors.

Source code:

 Collection of computer instructions written in human readable format.


 Write in java class files

Byte code:

 Computer object code, also called as instruction set of Java Virtual Machine.
 Writes the output in class file

 Java Virtual Machine (JVM)

 Java Virtual Machine – enables the computer to run Java program. It reads and interprets .class
files and executes the program’s instruction into the native hardware platform.
 JVM is platform independent (obtainable for many hardware and software platforms).
 In the Java virtual machine specification, the behavior of a virtual machine instance is described
in terms of subsystems, memory areas, data types and instructions. These components describe
an abstract inner architecture for the abstract Java virtual machine.
 The purpose of these components is not so much to dictate an inner architecture for
implementations. It is more to provide a way to strictly define the external behavior of
implementations. The specification defines the required behavior of any Java virtual machine
implementation regarding these abstract components and their interactions.
 What does JVM performs?
o It is used to load the code.
o It Verifies and executes the code.
o And it provides run time environment.
Components of JVM:

Following are the components of JVM:

o Stack
Temporary values are stored in frames of a stack. It contains the state of Java method
invocation. While invoking a method, JVM pushes a new frame onto that thread’s Java stack.
After completion of a single method, the virtual machine pops and discards the frame for that
method.

o Heap
The Heap consists of objects. The JVM has a heap that is the runtime data area from which
memory for all class instances and arrays are allocated. It is created at the JVM start-up. Heap
Memory – Store Java objects Non-heap memory – Store Meta data and other loaded classes.

o Constant Pool
Constant pool consist of all constant data. They comprise of Numbers Strings Symbolic names of
classes, interfaces and fields
o Code segment

o Program counter

Tracking of current instruction execution at any moment is done by Program Counter (PC)
register. It is similar to that of pointer which keeps in track of current instruction in sequence.
Every time a new thread is created, a PC register is also created simultaneously. PC keeps a
pointer to the current statement that is being executed in its thread.

You might also like