Inheritance
      Inheritance can be defined as the process 
where one object acquires the properties of another.
With the use of inheritance the information is made 
manageable in a hierarchical order.
SUPERCLASS
      A superclass allows for a generic interface to 
include specialized functionality through the use of
virtual functions.The superclass mechanism is 
extensively used in object oriented programming due 
to the reusability that can be achieved: common features
are encapsulated in modular objects. Subclasses that 
wish to implement special behavior can do so via virtual
methods, without having to duplicate (reimplement) the
superclass's behavior.
SUBCLASS
     A subclass is a class that inherits some properties 
from its superclass.
BENEFITS OF INHERITANCE
 1. Code reusebility.
 2. Minimise the amount of duplicate code.
 3. Sharing common code amongst several subclasses.
 4. Smaller, simpler and better organisation of code.
 5. Make application code more flexible to change.
OVERRIDING METHODS
     Method Overriding is achieved when a subclass
overrides non-static methods defined in the superclass,
following which the new method implementation in the
subclass that is executed.The new method definition 
must have the same method signature (i.e., method name 
and parameters) and return type. Only parameter types 
and return type are chosen as criteria for matching 
method signature. So if a subclass has its method
parameters as final it doesn’t really matter for
method overriding scenarios as it still holds true. 
The new method definition cannot narrow the accessibility
of the method, but it can widen it. The new method 
definition can only specify all or none, or a subset
of the exception classes (including their subclasses)
specified in the throws clause of the overridden method
in the super class.
FINAL METHODS AND CLASSES
Abstract Class:
Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword.
/* File name : Employee.java */
public abstract class Employee
{
   private String name;
   private String address;
   private int number;
   public Employee(String name, String address, int number)
   {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public double computePay()
   {
     System.out.println("Inside Employee computePay");
     return 0.0;
   }
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + this.name
       + " " + this.address);
   }
   public String toString()
   {
      return name + " " + address + " " + number;
   }
   public String getName()
   {
      return name;
   }
   public String getAddress()
   {
      return address;
   }
   public void setAddress(String newAddress)
  {
      address = newAddress;
  }
  public int getNumber()
  {
     return number;
  }
}
 
Notice that nothing is different in this Employee class. The class is now abstract, but it still has three fields, seven methods, and one constructor.
Now if you would try as follows:
/* File name : AbstractDemo.java */
public class AbstractDemo
{
   public static void main(String [] args)
   {
   
      /* Following is not allowed and would raise error */
      Employee e = new Employee("George W.", "Houston, TX", 43);
      System.out.println("\n Call mailCheck using 
                                   Employee reference--");
      e.mailCheck();
    }
}
 
When you would compile above class then you would get following error:
Employee.java:46: Employee is abstract; cannot be instantiated
      Employee e = new Employee("George W.", "Houston, TX", 43);
                   ^
1 error1
 
Extending Abstract Class:
We can extend Employee class in normal way as follows:
/* File name : Salary.java */
public class Salary extends Employee
{
   private double salary; //Annual salary
   public Salary(String name, String address, int number, double
      salary)
   {
       super(name, address, number);
       setSalary(salary);
   }
   public void mailCheck()
  {
       System.out.println("Within mailCheck of Salary class ");
       System.out.println("Mailing check to " + getName()
       + " with salary " + salary);
   }
   public double getSalary()
   {
       return salary;
   }
   public void setSalary(double newSalary)
   {
       if(newSalary >= 0.0)
       {
          salary = newSalary;
       }
   }
   public double computePay()
   {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }
}
 
Here we cannot instantiate a new Employee, but if we instantiate a new Salary object, the Salary object will inherit the three fields and seven methods from Employee.
/* File name : AbstractDemo.java */
public class AbstractDemo
{
   public static void main(String [] args)
   {
      Salary s = new Salary("Mohd Mohtashim", "Ambehta,  UP",
                                 3, 3600.00);
      Salary e = new Salary("John Adams", "Boston, MA",
                                 2, 2400.00);
      System.out.println("Call mailCheck using
                                   Salary reference --");
      s.mailCheck();
      System.out.println("\n Call mailCheck using
                                   Employee reference--");
      e.mailCheck();
    }
}
 
This would produce following result:
Constructing an Employee
Constructing an Employee
Call mailCheck using  Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0
Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.
 
POLYMORPHISM
     Polymorphism is the ability of an 
object to take on many forms. The most 
common use of polymorphism in OOP occurs
when a parent class reference is used 
to refer to a child class object.Any java
object that can pass more than on IS-A 
test is considered to be polymorphic. 
In Java, all java objects are polymorphic 
since any object will pass the IS-A test 
for their own type and for the class Object.
It is important to know that the only possible
way to access an object is through a reference
variable. A reference variable can be of only 
one type. Once declared the type of a reference
variable cannot be changed.The reference variable
can be reassigned to other objects provided that
it is not declared final. The type of the reference 
variable would determine the methods that it can 
invoke on the object.A reference variable can 
refer to any object of its declared type or 
any subtype of its declared type. A reference 
variable can be declared as a class or interface type.
INTERFACE
An interface is a collection of abstract methods. 
Aclass implements an interface, thereby inheriting 
the abstract methods of the interface.
An interface is not a class. Writing an interface 
is similar to writing a class, but they are two 
different concepts. A class describes the attributes
 and behaviors of an object. An interface contains
 behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
-An interface is similar to a class in the following ways:
-An interface can contain any number of methods.
-An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
-The bytecode of an interface appears in a .class file.
-Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
However, an interface is different from a class in several ways, including:
-You cannot instantiate an interface. 
-An interface does not contain any constructors.
-All of the methods in an interface are abstract.
-An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
-An interface is not extended by a class; it is implemented by a class.
-An interface can extend multiple interfaces.
Thursday, April 22, 2010
INHERITANCE, POLYMORPHISM, INTERFACE
Posted by + leah jane + at 2:26 AM 0 comments
Monday, April 19, 2010
Pass-by-value
    The actual parameter (or argument expression) 
    is fully evaluated and the resulting value is copied 
    into a location being used to hold the formal parameter's 
    value during method/function execution. That location is
    typically a chunk of memory on the runtime stack for the
    application (which is how Java handles it), but other
    languages could choose parameter storage differently.
Pass-by-reference
    The formal parameter merely acts as an alias for
    the actual parameter. Anytime the method/function 
    uses the formal parameter (for reading or writing), 
    it is actually using the actual parameter.
Posted by + leah jane + at 7:23 PM 0 comments
Working w/ the Java
Object-oriented programming (OOP) is a programming paradigm
                 that uses "objects" –   data structures 
                 consisting of datafields and methods together 
                 with their interactions – to design applications
                 and computer programs. Programming techniques may 
                 include features such as data abstraction, encapsulation,   
                 modularity, polymorphism, and inheritance. It was not
                 commonly used in mainstream software application 
                 development until the early 1990s.
Encapsulation is the technique of making the fields in a 
             class private and providing access to the fields
             via public methods. If a field is declared private,
             it cannot be accessed by anyone outside the class, 
             thereby hiding the fields within the class. For this
             reason, encapsulation is also referred to as data 
             hiding.Encapsulation can be described as a protective 
             barrier that prevents the code and data being randomly 
             accessed by other code defined outside the class. Access
             to the data and code is tightly controlled by an interface. 
             The main benefit of encapsulation is the ability to modify 
             our implemented code without breaking the code of others who 
             use our code. 
Object - Objects have states and behaviors. 
         Example: A dog has states-color, name, breed as
         well as behaviors -wagging, barking, eating. An object 
         is an instance of a class. 
Class - A class can be defined as a template/ blue print
        that describe the behaviors/states that object of 
        its type support.
Class Variables
  When a number of objects are created from the same
        class blueprint, they each have their own distinct 
        copies of instance variables. In the case of the 
        Bicycle class, the instance variables are cadence,
        gear, and speed. Each Bicycle object has its own 
        values for these variables, stored in different memory 
        locations. 
  Sometimes, you want to have variables that are common 
        to all objects. This is accomplished with the static
        modifier. Fields that have the static modifier in their
        declaration are called static fields or class variables.
        They are associated with the class, rather than with any 
        object. Every instance of the class shares a class variable, 
        which is in one fixed location in memory. Any object can change
        the value of a class variable, but class variables can also be
        manipulated without creating an instance of the class. 
Class  Instantiation
       In programming, instantiation is the creation of a real 
       instance or particular realization of an abstraction or 
       template such as a class of objects or a computer process.  
       To instantiate is to create such an instance by, for example, 
       defining one particular variation of object within a class, 
       giving it a name, and locating it in some physical place
The Method Declaration 
          A method's declaration provides a lot of information about 
          the method to the compiler, the runtime system and to other 
          classes and objects. Besides the name of the method, the method 
          declaration carries information such as the return type of the 
          method, the number and type of the arguments required by the 
          method, and what other classes and objects can call the method. 
          While this may sound like writing a novel rather than simply 
          declaring a  method for a class, most method attributes can be 
          declared implicitly. The only two required elements of a method
          declaration are the method name and the data type returned by 
          the method. For example, the following declares a method named 
          isEmpty() in the Stack class that returns a boolean value 
          (true or false): 
class Stack 
{
    . . .
    boolean isEmpty() {
        . . .
    }
}
 
STATIC VARIABLES 
       A static variable is associated with the class as a whole 
       rather than with specific instances of a class. Each object 
       will share a common copy of the static variables i.e. there
       is only one copy per class, no matter how many objects are 
       created from it. Class variables or static variables are 
       declared with the static keyword in a class. These are declared 
       outside a class and stored in static memory. Class variables are
       mostly used for constants. Static variables are always called by
       the class name. This variable is created when the program starts 
       and gets destroyed when the programs stops. The scope of the class
       variable is same an instance variable. Its initial value is same as
      instance variable and gets a default value when its not initialized  
      corresponding to the data type. Similarly, a static method is a method 
      that belongs to the class rather than any object of the class and 
      doesn’t apply to an object or even require that any objects of the
      class have been instantiated.
      Static methods are implicitly final, because overriding is done based 
      on the type of the object, and static methods are attached to a class, 
      not an object. A static method in a superclass can be shadowed by another
      static method in a subclass, as long as the original method was not 
      declared final. However, you can’t override a static method with 
      a non-static method. In other words, you can’t change a static method
      into an instance method in a subclass.
Posted by + leah jane + at 2:19 AM 0 comments
Thursday, April 15, 2010
JAVA PROGRAMMING
Posted by + leah jane + at 6:47 PM 0 comments
INTRODUCTION TO JAVA PROGRAMMING
Java was created in 1991 by James Gosling et al. of Sun Microsystems. Initially calledOak, in honor of the tree outside Gosling's window, its name was changed to Javabecause there was already a language called Oak.The original motivation for Java was the need for platform independent language thatcould be embedded in various consumer electronic products like toasters andrefrigerators. One of the first projects developed using Java was a personal hand-heldremote control named Star 7.At about the same time, the World Wide Web and the Internet were gaining popularity.Gosling et. al. realized that Java could be used for Internet programming.
B. WHAT IS JAVA TECHNOLOGY?
1 . A programming languageAs a programming language,
2. A development environmentAs a development
3. An application environmentJava technology
4. A deployment environmentThere are two main
C. SOME FEATURES OF JAVA
2. Garbage CollectionMany programming
3. Code SecurityCode security is attained in Java
D. PHASES OF A JAVA PROGRAM
2. Compile the program
3. Run the program
1. Any text editor
2. Java Compiler
3. Java Interpreter
1. File with .java extension2. File with .class
E. DIFFERENCE BETWEEN JAVA APPLICATIONS AND JAVA APPLETS
- Java applets are java programs that run in a web browser, it is dependent on web browsers.
 -
- Java applications are stand alone java programs. It can be run in a console window or a graphical user interface.
F. WHAT MAKES JAVA AN OBJECT ORIENTED LANGUAGE
- Java is considered as an object oriented programming language because it uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Java includes features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance.
Posted by + leah jane + at 6:23 PM 0 comments
