You are on page 1of 30

Object Oriented Programming

Goals
Object Basics Pillars of OOP Basic Java Syntax Compiling / Executing Java Programs

OOP Concepts
Objects Attributes Methods Events Abstraction & Classes Constructors

What is an object?
An object is a unique programming entity that has attributes to describe it and methods to retrieve/set attribute values.

Attributes
Programmers store an objects data in attributes, also called properties. Attributes provide us a way to describe an object, similar to adjectives in grammar. We can read property values or change properties, assigning values.

Methods
Whereas attributes describe an object, methods allow us to access object data. Methods are like verbs in grammar. We can manipulate object data, stored in attributes, using methods.

Events
Object-oriented programming is inherently tied to user interaction. Programs record interaction in the form of events. Events are changes in an objects environment to which it can react.

Abstraction
One of the chief advantages of object-oriented programming is the idea that programmers can essentially focus on the big picture and ignore specific details regarding the innerworkings of an object. This concept is called abstraction.

Classes
How do programmers get by implementing abstraction? They use a programming structure called a class. A class presents a blueprint of an object, its properties and its methods.

Instantiation
To create an object based on a class, we create an instance of that class. This process is called instantiation. In Java and other languages, we use a special method called a constructor method to create an instance of an object.

Encapsulation
Abstraction in OOP is closely related to a concept called encapsulation. Data and the ways to get at that data are wrapped in a single package, a class. The only way to access such data is through that package. This idea translates to information hiding.

Inheritance
Another of the main tenets of OOP is inheritance. Inheritance allows programmers to create new classes from existing ones. A child class inherits its properties and attributes from its parents, which programmers can change.

Polymorphism
Polymorphism describes how programmers write methods to do some general purpose function. Different objects might perform polymorphic methods differently.

General features of OOP


Programming objects are comprised of attributes and methods. Classes provide programmers with blueprints of objects. To create an object from a class, we use constructor methods to create a class instance.

Java
Developed by Sun Microsystems Java lineage C to C++ to Java driven by the nature of the computing environments in which software must be deployed embedded systems. The massive growth of the Internet and the World-Wide Web new ways of development and

secure, high performance, and highly robust applications on multiple platforms in heterogeneous, distributed networks. architecture neutral, portable, and dynamically adaptable.

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

Pillars of OOP
Encapsulation Inheritance Polymorphism

Encapsulation
Mechanism that wraps together code and data that it manipulates Safe from outside interference & misuse Example from real world the car you drive Java classes (data, methods and instances access is carefully controlled public, private)

Inheritance
One objects inherits the properties of another For eg. A Doberman is a dog which in turn is a mammal

Polymorphism
Many forms One interface multiple methods Reduces the complexity For eg. Dogs sense of smell is polymorphic Dogs smells a cat, dog smells food Depending on what, the reaction varies

Getting Started: (1)


(1) Create the source file:
open a text editor, type in the code which defines a class (HelloWorldApp) and then save it in a file (HelloWorldApp.java) file and class name are case sensitive and must be matched exactly (except the .java part)
Example Code: HelloWorldApp.java /** * The HelloWorldApp class implements an application * that displays "Hello World!" to the standard output */ public class HelloWorldApp { public static void main(String[] args) { // Display "Hello World!" System.out.println("Hello World!"); } }

Java is

CASE SENSITIVE!

Getting Started: (2)


(2) Compile the program:
compile HelloWorldApp.java by using the following command:
javac HelloWorldApp.java

javac is not recognized as an internal or


javac: Command not found

it generates a file named HelloWorldApp.class

external command, operable program or hatch file. if you see one of these errors, you have two choices: 1) specify the full path in which the javac program locates every time. For example:
C:\j2sdk1.4.2_09\bin\javac HelloWorldApp.java

2) set the PATH environment variable

Getting Started: (3)


(3) Run the program:
run the code through:
java HelloWorldApp

Note that the command is java, not javac, and you refer to HelloWorldApp, not HelloWorldApp.java or HelloWorldApp.class

Exception in
HelloWorldApp

thread "main" java.lang.NoClassDefFoundError:

if you see this error, you may need to set the environment variable CLASSPATH.

Java platform
The Java Virtual Machine The Java Application Programming Interface (API) JVM - the base for the Java platform and is ported onto various hardwarebased platforms. The API is a large collection of readymade software components that provide many useful capabilities.

You might also like