Introducing Object-Oriented Programming (OOP)
Introducing Object-Oriented Programming (OOP)
Programming (OOP)
By A.Hemlathadhevi
Programming Languages
• Programming languages allow programmers
to code software.
• The three major families of languages are:
– Machine languages
– Assembly languages
– High-Level languages
Machine Languages
• Comprised of 1s and 0s
• The “native” language of a computer
• Difficult to program – one misplaced 1 or 0
will cause the program to fail.
• Example of code:
1110100010101 111010101110
10111010110100 10100011110111
Assembly Languages
• Assembly languages are a step towards easier
programming.
• Assembly languages are comprised of a set of
elemental commands which are tied to a
specific processor.
• Assembly language code needs to be
translated to machine language before the
computer processes it.
• Example:
ADD 1001010, 1011010
High-Level Languages
• High-level languages represent a giant leap
towards easier programming.
• The syntax of HL languages is similar to
English.
• Historically, we divide HL languages into two
groups:
– Procedural languages
– Object-Oriented languages (OOP)
Procedural Languages
• Early high-level languages are typically called
procedural languages.
• Procedural languages are characterized by
sequential sets of linear commands. The focus
of such languages is on structure.
• Examples include C, COBOL, Fortran, LISP,
Perl, HTML, VBScript
Object Oriented Programming
• Object – Unique programming entity that has
methods, has attributes and can react to
events.
• Method – Things which an object can do; the
“verbs” of objects. In code, usually can be
identified by an “action” word -- Hide, Show
Object Oriented Programming
• Attribute – Things which describe an object;
the “adjectives” of objects. In code, usually
can be identified by a “descriptive” word –
Enabled, BackColor
• Events – Forces external to an object to which
that object can react. In code, usually
attached to an event procedure
Object Oriented Programming
• Class – Provides a way to create new objects
based on a “meta-definition” of an object
(Example: The automobile class)
• Constructors – Special methods used to
create new instances of a class (Example: A
Honda Civic is an instance of the automobile
class.)
OOP - Encapsulation
• Incorporation into a class of data & operations
in one package
• Data can only be accessed through that
package
• “Information Hiding”
OOP - Inheritance
• Allows programmers to create new classes
based on an existing class
• Methods and attributes from the parent class
are inherited by the newly-created class
• New methods and attributes can be created in
the new class, but don’t affect the parent
class’s definition
OOP - Polymorphism
• Creating methods which describe the way to
do some general function (Example: The
“drive” method in the automobile class)
• Polymorphic methods can adapt to specific
types of objects.
Classes and Objects
• A class is a data type that allows programmers to
create objects. A class provides a definition for an
object, describing an object’s attributes (data)
and methods (operations).
• An object is an instance of a class. With one
class, you can have as many objects as required.
• This is analogous to a variable and a data type,
the class is the data type and the object is the
variable.
Thank you
• Mam, I learnt that, object is a instance of
class, and constructor is also a instance of a
class. How can it be possible mam?. Can you
please explain mam? But the declaration of a
object is different from constructor.(learnt
from c++) Please explain mam
• A class is a kind of data type, just like a string,
integer or list. When we create an object of
that data type, we call it an instance of a class.
• A constructor is a member function of a class
which initializes objects of a class. In C++,
Constructor is automatically called when
object(instance of class) create. It is special
member function of the class.
• How constructors are different from a normal
member function?
• Based on C/C++
• Widespread acceptance
Java Features (1)
• Simple
– fixes some clumsy features of C++
– no pointers
– automatic garbage collection
– rich pre-defined class library http://java.sun.com/j2se/1.4.2/docs/api/
• Object oriented
– focus on the data (objects) and methods manipulating the data
– all functions are associated with objects
– almost all datatypes are objects (files, strings, etc.)
– potentially better code organization and reuse
Java Features (2)
• Interpreted
– java compiler generate byte-codes, not native machine code
– the compiled byte-codes are platform-independent
– java bytecodes are translated on the fly to machine readable
instructions in runtime (Java Virtual Machine)
• Portable
– same application runs on all platforms
– the sizes of the primitive data types are always the same
– the libraries define portable interfaces
Java Features (3)
• Reliable
– extensive compile-time and runtime error checking
– no pointers but real arrays. Memory corruptions or unauthorized
memory accesses are impossible
– automatic garbage collection tracks objects usage over time
• Secure
– usage in networked environments requires more security
– memory allocation model is a major defense
– access restrictions are forced (private, public)
Java Features (4)
• Multithreaded
– multiple concurrent threads of executions can run simultaneously
– utilizes a sophisticated set of synchronization primitives (based on
monitors and condition variables paradigm) to achieve this
• Dynamic
– java is designed to adapt to evolving environment
– libraries can freely add new methods and instance variables without
any effect on their clients
– interfaces promote flexibility and reusability in code by specifying a set
of methods an object can perform, but leaves open how these
methods should be implemented
– can check the class type in runtime
Java Disadvantages