OOP Unit1
OOP Unit1
• C is a successful and useful language, you might ask why a need for something else
existed.
• The answer is complexity.
• To solve this problem, a new way to program was invented, called object-oriented
programming (OOP).
• C++ was invented by Bjarne Stroustrup in 1979, while he was working at Bell
Laboratories in Murray Hill, New Jersey.
• Initially called the language “C with Classes.”
• However, in 1983, the name was changed to C++.
• C++ extends C by adding object-oriented features.
• By the end of the 1980s and the early 1990s, object-oriented programming using C++
took hold.
• Within a few years, the World Wide Web and the Internet would reach critical mass.
• This event would precipitate another revolution in programming.
• 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.
• Primary motivation was the need for a platform independent (that is,
architecture-neutral) language that could be used to create software to be embedded in
various consumer electronic devices, such as microwave ovens and remote controls.
• The problem is that compilers are expensive and time-consuming to create.
• Solution, a portable, platform-independent language
• The second force was, of course, the World Wide Web.
• The emergence of the World Wide Web, Java was propelled to the forefront of computer
language design, because the Web, too, demanded portable programs.
• The Internet ultimately led to Java’s large-scale success
• Java is simply the “Internet version of C++.”
• The key that allows Java to solve both the security and the portability, the output of a Java
compiler is not executable code. Rather, it is bytecode.
• Bytecode is a highly optimized set of instructions designed to be executed by the Java
run-time system, which is called the Java Virtual Machine (JVM).
• Although the fundamental forces that necessitated the invention of Java are portability
and security, other factors also played an important role in molding the final form of the
language.
1. Simple - Because Java inherits the C/C++ syntax and many of the object oriented
features of C++, most programmers have little trouble learning Java.
2. Secure - Because the JVM is in control, it can contain the program and prevent it
from generating side effects outside of the system.
3.Portable - WORA
4. Object-oriented - Problems and their solutions are packaged in terms of classes.
The information in a class is the data.
The functionality in a class is the method.
Class provides the framework for building objects
• Two Paradigms
– All computer programs consist of two elements: code and data.
• Furthermore, a program can be conceptually organized around its code or around its data.
• That is, some programs are written around “what is happening” and others are written
around “who is being affected.”
• These are the two paradigms that govern how a program is constructed.
1. Encapsulation
2. Inheritance
3. Polymorphism
• Encapsulation is the mechanism that binds together code and the data it manipulates,
and keeps both safe from outside interference and misuse.
• One way to think about encapsulation is as a protective wrapper that prevents the code
and data from being arbitrarily accessed by other code defined outside the wrapper.
• In Java, the basis of encapsulation is the class.
• A class defines the structure and behavior (data and code) that will be shared by a set of
objects.
• Objects are sometimes referred to as instances of a class.
• Thus, a class is a logical construct; an object has physical reality
• When you create a class, you will specify the code and data that constitute that class.
Collectively, these elements are called members of the class.
• Specifically, the data defined by the class are referred to as variables. The code that
operates on that data is referred to as Methods.
• Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding
the complexity of the implementation inside the class.
• Each method or variable in a class may be marked private or public
• When you create a class, you will specify the code and data that constitute
that class. Collectively, these elements are called members of the class.
• Inheritance is the process by which one object acquires the properties of
another object. This is important because it supports the concept of hierarchical
classification.
• Inheritance, an object need only define those qualities that make it unique within its class.
It can inherit its general attributes from its parent.
• Thus, it is the inheritance mechanism that makes it possible for one object to be a specific
instance of a more general case.
• A deeply inherited subclass inherits all of the attributes from each of its ancestors in the
class hierarchy.
• Polymorphism (from Greek, meaning “many forms”) is a feature that allows one
interface to be used for a general class of actions.
• Ex:- Consider 3 stack (which is a last-in, first-out list).
• One stack is used for integer values, one for floating point values, and one for characters.
The algorithm that implements each stack is the same, even though the data being stored
differs.
• The concept of polymorphism is often expressed by the phrase “one interface, multiple
methods.”
• This means that it is possible to design a generic interface to a group of related activities.
• This helps reduce complexity by allowing the same interface to be used to specify a
general class of action.
BYTE
Width : 8 bits
Range : -128 to 127
• Class is the logical construct upon which the entire Java language is built because it
defines the shape and nature of an object.
• A class, defines a new data type.
• Once defined, this new type can be used to create objects of that type.
• Thus, a class is a template for an object, and an object is an instance of a class.
• Because an object is an instance of a class, the two words object and instance used
interchangeably.
• A class is declared by use of the class keyword.
• The data, or variables, defined within a class are called instance variables.
• The code is contained within methods.
• Collectively, the methods and variables defined within a class are called members of the
class.
• Variables defined within a class are called instance variables because each instance of the
class (that is, each object of the class) contains its own copy of these variables. Thus, the
data for one object is separate and unique from the data for another
OUTPUT :
▪ Garbage collection is a mechanism to remove objects from memory when they are no
longer needed.
▪ Garbage collection is carried out by the garbage collector:
1) The garbage collector keeps track of how many references an object has.
2) It removes an object from memory when it has no longer any references.
3) Thereafter, the memory occupied by the object can be allocated again.
4) The garbage collector invokes the finalize method.
▪ Keyword ‘this’ allows a method to refer to the object that invoked it.
▪ It can be used inside any method to refer to the current object:
Example:
Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
▪ In Java, it is possible to define two or more methods within the same class that share the
same name, as long as their parameter declaration is different
▪ When this is the case, methods are said to be overloaded and the process is referred to as
method overloading
▪ Method overloading is one of the ways java supports polymorphism.
▪ When an overloaded method is invoked, Java uses the type or number of arguments as its
guide to determine which version of the overloaded method to call
▪ Thus overloaded method must differ in type or number or sequence of their parameters.