Pages

Dec 9, 2010

Example for interface in java

/*
* Java Interface Examples
/
// Declaring the interface with a method declaration
interface intDemo
{
public void  sayWelcome();

}
 
 // create class and  implements the interface  using the keyword "implements"
public class DemoInterface implements intDemo

{
// defining the methods which is declared in the interface
public void  sayWelcome()
{
System.out.println("Welcome here");
}
public static void main(String[] args)
{
// create object for the class 

DemoInterface demointerface=new DemoInterface(); 
//invoke the sayWelcome() .which is declared in intDemo interface
demointerface.sayWelcome();
}

}

Output:::

Welcome here

Dec 8, 2010

Example of Encapsulation in java

/* File name : EncapTest.java */
public class EncapTest{
//Declaring the variables as private
private String name;
   private String idNum;
   private int age;

   public int getAge(){
      return age;
   }

   public String getName(){
      return name;
   }

   public String getIdNum(){
      return idNum;
   }

   public void setAge( int newAge){
      age = newAge;
   }

   public void setName(String newName){
      name = newName;
   }

   public void setIdNum( String newId){
      idNum = newId;
   }
}
 
/* File name : RunEncap.java */
 
public class RunEncap{

   public static void main(String args[]){
 
//here invoke the class of EncapTest.java
EncapTest encap = new EncapTest();
      encap.setName("James");
      encap.setAge(20);
      encap.setIdNum("12343ms");

      System.out.print("Name : " + encap.getName()+ 
                             " Age : "+ encap.getAge());
    }
}
Output:
Name : James Age : 20

Example of inheritance in java

    public class Bicycle {
       
        // the Bicycle class has three fields
        public int cadence;
        public int gear;
        public int speed;
       
        // the Bicycle class has one constructor
        public Bicycle(int startCadence, int startSpeed, int startGear) {
            gear = startGear;
            cadence = startCadence;
            speed = startSpeed;
        }
       
        // the Bicycle class has four methods
        public void setCadence(int newValue) {
            cadence = newValue;
        }
       
        public void setGear(int newValue) {
            gear = newValue;
        }
       
        public void applyBrake(int decrement) {
            speed -= decrement;
        }
       
        public void speedUp(int increment) {
            speed += increment;
        }
       
    }



    public class MountainBike extends Bicycle {
       
        // the MountainBike subclass adds one field
        public int seatHeight;

        // the MountainBike subclass has one constructor
        public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
            super(startCadence, startSpeed, startGear);
            seatHeight = startHeight;
        }   
       
        // the MountainBike subclass adds one method
        public void setHeight(int newValue) {
            seatHeight = newValue;
        }   

    }


/*Here MountainBike class inherit all the fields and methods from bi-cycle class.
And also MountainBike class has new field and method seatheight*/

Dec 7, 2010

Example of abstract class in java

    // A Simple demonstration of abstract.

    abstract class A
    {
    // abstract method in abstract class
    abstract void callme();
    // concrete methods are  allowed in abstract classes
    void callmetoo()
    {
    System.out.println("This is a concrete method.");
    }
    }
    // class B extends the absract class A
    class B extends A {
    void callme() {
    System.out.println("B's implementation of callme.");
    }
    }

    class AbstractDemo {
    public static void main(String args[]) {
    B b = new B();
    b.callme();
    b.callmetoo();
    }
    }

Example for absract