Showing posts with label java cram. Show all posts
Showing posts with label java cram. Show all posts

Friday, January 02, 2009

OOP Principles

In the process-oriented model, programs are written around "what is happening". This approach characterizes a program as a series of linear steps. This can be thought of as code acting on data.

But, object-oriented programming organizes a program around its data, i.e., objects, and a set of well-defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.

Following are the three mechanisms that are required to implement the object-oriented model:

Encapsulation
  • It is the mechanism that binds together code and the data it manipulates
  • It keeps both code and data safe from outside interference and misuse
  • The basis of encapsulation is the class
  • A class defines the structure and behavior (of code and data) that will be shared by a set of objects
  • Each object of a given class contains the structure and behavior defined by the class
  • A class is a logical construct, whereas an object has physical reality
  • 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
  • The public interface of a class represents everything that external users of the class need to know, or may know
  • The private methods and data can only be accessed by code that is a member of the class
  • Any other code that is not a member of the class cannot access a private method or variable

Ineritance
  • It is the process by which one object acquires the properties of another object
  • It supports the concept of hierarchical classification
  • Without the use of hierarchies, each object would need to define all of its characteristics explicitly
  • However, by use of inheritance, an object need only define those qualities that make it unique within its class
  • It can inherit its general attributes from its parent
  • It is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case
  • If a given class  encapsulates some attributes, then any subclass will have the same attributes plus any that it adds as part of its specialization
  • This lets object-oriented programs grow in complexity linearly rather than geometrically

Polymorphism
  • Polymorphism is a feature that allows one interface to be used for a general class of actions
  • The specific action is determined by the exact nature of the situation
  • It can be expressed by the phrase - one interface, multiple methods
  • 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
  • It is the compiler's job to select the specific action as it applies to each situation
  • There are three distinct forms of polymorphism
    • Method overloading
    • Method overriding through inheritance
    • Method overriding through the Java interface
  • Which overloaded method to call is decided at compile time through the reference type passed
  • The other two forms are part of runtime polymorphism
  • With runtime polymorphism, the decision as to which version of a method will be executed is based on the actual type of object whose reference is stored in the reference variable, and not on the type of the reference variable on which the method is invoked
  • The decision as to which version of the method to invoke cannot be made at compile time
  • That decision must be deferred and made at runtime
  • This is sometimes referred to as late binding or dynamic method binding
Another essential element of object-oriented programming is abstraction.
  • A powerful way to manage abstraction is through the use of hierarchical classifications
  • This allows us to layer the semantics of complex systems, breaking them into more manageable pieces
  • The complicated implementation details are hidden from the user
  • The user is given a simple interface to interact with, whereas inside the actual implementation of the interface there may be many complicated details


(Compiled from Java 2: The Complete Reference)

Monday, December 29, 2008

What is Java?

  • Java generally refers to a combination of three things
    • the Java programming language
    • the Java Virtual machine
    • the Java platform
  • Java the language is a high-level OOP language, influenced in various ways by C, C++ and Smalltalk
  • Java has been around officially since 1996
  • Java has evolved tremendously all through these years and has spawned a number of technologies like
    • Servlet technology
    • component technology like JavaBeans
    • JavaServerFaces
    • and a whole host of other technologies and tools
  • Its syntax was designed to be familiar to those familiar with C-descended "curly brace" languages, but with stronger OO principiles than those found in C++
  • One of Java's more controversial aspects is its incomplete object-orientation - Java primitives such as int, char, boolean etc. are not objects
  • The inclusion of primitives increases Java performance, but at arguably expense of code clarity
  • Java 5.0 introduces an "autoboxing" scheme to eliminate many uses of the wrapper classes, but in some ways it obsures what is really going on
  • Java is a fail early language - because of its syntatic restrictions, many programming failures are simply not possible in Java
  • With no direct access to pointers, pointer-arithmetic errors are non-existent
  • Using an object as a different type than what it was originally declared to be requires an explicit cast, which gives the compiler an opportunity to reject illogical programming, like calling a String method on an Image
  • Java is generally understood to be the most popular general-purpose computing language in use today

  • Java is generally thought of in terms of three platforms
    • Standard Edition(SE)
    • Enterprise Edition (EE)
    • Micro Edition (ME)
  • Each describes the combination of a language version, a set of standard libraries, and a virtual machine to execute the code
  • EE is a superset of SE - any EE application can assume the existence of all of the SE libraries
  • EE's use of the language is identical to SE's
  • Because of the limitations of small devices like phones and set-top boxes, Java ME differs significantly from its siblings
  • It is not a subset of SE
  • ME eliminates some language features, such as the float primitive and Float class, reflecting the computing limitations of the platforms it runs on

Java Runtime Environment
  • Java Runtime Environment is a subset of JDK
  • It is an implementation of the JVM which actually executes Java programs
  • JRE is a plug-in needed for running Java programs
  • JRE includes JVM, core libraries and other additional components to run applications and applets written in Java


Java Virtual Machine
  • One of the strong points in favor of Java is "Write Once, Run Anywhere"
  • Java is platform-independent
  • Before Java, programmers would have to write the program for the Windows OS and again for the Mac OS
  • Java eliminates this problem with the use of JVM
  • Java Virtual Machine as its name suggests is a "virtual" computer that resides in the real computer as a software process
  • The JVM essentially runs between the computer and the Java program
  • JVM gives Java the flexibility of platform independence
  • At some point, Java source needs to become platform-native executable code
  • This typically requires a two step process - the developer compiles the source into Java bytecode, and then a JVM converts this into native code for the host platform
  • Java code is written in a ".java" file
  • This code contains one or more Java language attributes like Class, Methods, Variables, Objects etc.
  • Javac is used to compile this code and to generate a ".class" file
  • Class file is also known as byte code
  • java byte code is an input to JVM
  • JVM reads this code and interprets it and executes the program
  • JVM helps the JRE to run Java applications
  • JVM operates on Java bytecode, which is normally generated from Java source code
  • A JVM can also be used to implement programming languages other than Java
  • For example, Ada source code can be compiled to Java bytecode, which may then be executed by a JVM
  • JVMs can also be released by other companies besides Sun - JVMs using the "Java" trademark may be developed by other companies as long as they adhere to the JVM specification published by Sun



Components of JVM
  • JVM is divided into several components like the stack, the garbage-collected heap, the registers and the method area
  • Stack in JVM stores various method arguments as well as the local variables of any method
  • Stack also keeps track of each and every method invocation - called the Stack Frame
  • There are three registers that helps in stack manipulation - vars, frame and optop
  • These registers point to different parts of current stack
  • There are three sections in Java stack frame
    • Local Variables - contains all the local variables being used by the current method invocation. It is pointed to by the vars register
    • Execution Environment - used to maintain the operations of the stack itself. It is pointed to by the frame register
    • Operand Stack - used as a work space by bytecode instructions. It is here that the parameters for bytecode instructions are placed, and the results of bytecode instructions are found. The top of the operand stack is pointed to by the optop register
  • Method Area - is the area where bytecode resides
  • The program counter points to some byte in the method area
  • It always keeps track of the current instruction which is being executed
  • After execution of an instruction, the JVM sets the PC to next instruction
  • Method area is shared among all the threads of a process
  • If more than one thread is accessing any specific method or instruction, synchronization is required
  • Synchronization in JVM is achieved through Monitors
  • Garbage-Collected Heap - is where the objects in Java programs are stored
  • Whenever an object is created using the new operator, the heap comes into picture and memory is allocated from there
  • unlike C++, Java doesn't have free operator to free any previously allocated memory
  • Java does this automatically using Garbage collection mechanism
  • "Mark and Sweep" algorithm is used as garbage collection logic
  • The local object reference resides on Stack, but the actual object resides in Heap
  • Arrays in Java are objects, hence they also reside in Garbage-Collected Heap


(Compiled from http://viralpatel.net/blogs/2008/12/java-virtual-machine-an-inside-story.html, Beyond Java and Widipedia)