vivarem logo
/ Vivarem / Computers and IT / IT Education / online / tutorial / Java / OOP / 8 /
There are 0 sub sections

You can add Your Link here with New Link
Java Object Oriented Programming - 8

Abstract Classes and Interfaces
In object oriented programming it is necessary to define the skeleton structure first and implement the skelton in some other class concretely. For this there are two mechanisms used 1)Abstract class and 2)Interface

Abstract Classes
an abstract class is an in complete class with atleast one method definition alone without completely implementing the class methods.any class with atleast an abstract method should be declared abstract.an abstract method will not have body block, ie braces will be missing
General Syntax
abstract class MyAbstractClass{
abstract public void myMethod();
}
Example

package com.vivarem.examples;
abstract class Vehicle{
abstract public void show();
}
class Car extends Vehicle{
public void show(){
System.out.println("Hi I am a Car");
}
}
class Bus extends Vehicle{
public void show(){
System.out.println("Hi i am a Bus");
}
}

public class JavaAbstractImpl{
public static void main(String a[]){
Vehicle v= new Car();
v.show();
v= new Bus();
v.show();
}
}


The above example illustrats polymorphism with abstract class, vehicle is an abstract class with classes Car and bus extending the abstract class Vehicle and implementing the abstract method show(). The abstract class may containe data members with required access specifiers, may have its own functionality with well defined member methods other than abstract methods
You may have Anonymous inner classes in a class method by actually implementing the abstract class as inner class as follows

package com.vivarem.examples;
abstract class AnAbstract{ public abstract void show();
}

public class JavaAnonymous{
public static void main(String[] s){
new com.vivarem.examples.AnAbstract(){
public void show(){
System.out.println("I am a Anonymous class" );}}.show();
// the above statement actually creates a class instantiate it and run the method show of AnAbstract.
}}


Interfaces
An Interface is declared with interface keyword, an interface can extend many other interfaces unlike astract classes which supports only single inheritace.All datamemebers of the interface is public and final and should be initialized as it is final.in order to use an interface the interface must be completely implemented by the implementing class. ie. the abstract methods of the interface should be fully defined.
General sytnax
public interface MyInterface{
}
Interface extending other interfaces
public interface Inter1{
final int no=10;
final String id="jhghg1988762";
}
public interface inter2{
public void show();
}
public void interface inter3 extends inter1,inter2{
public void showid();
}
any class that wish to implement the above inter3 interface should implement both methods show of inter2 and showid of inter3
The following example illustrates ploymorphism using interfaces

package com.vivarem.examples;
interface InputDevice{
public String getMake();
public String getManufacturer();
public void setMake(String make);
public void setManufacturer(String Manufacturer);
int CHARACTER_INPUT=1;
int POINT_INPUT=2;
int CONNECTOR=3;
}
interface Connector{
public String getConnector();
}
interface PointingDevice extends InputDevice,Connector{
int getButtons();
}
interface CharacterInputDevice extends InputDevice, Connector{
int getLayout();
int getKeys();
int getMMKeys();
void setLayout(int l);
void setKeys(int k);
void setMMKeys(int mk);
}
class KeyBoard implements CharacterInputDevice{
int layout,keys,mmkeys;
String manufacturer,make,connector;
public String getConnector(){return connector;}
public void setConnector(String connector){this.connector=connector;}
public int getLayout(){ return layout;}
public String getMake(){return make;}
public String getManufacturer(){return manufacturer;}
public int getMMKeys(){return mmkeys;}
public int getKeys(){return keys;}
public void setLayout(int Layout){this.layout=layout;}
public void setKeys(int keys){this.keys=keys;}
public void setMMKeys(int mmkeys){this.mmkeys=mmkeys;}
public void setManufacturer(String manufacturer){this.manufacturer=manufacturer;}
public void setMake(String make){this.make=make;}
public KeyBoard(String manufacturer, String make, int layout, int keys, int mmkeys,String connector){
setManufacturer(manufacturer);
setKeys(keys);
setMake(make);
setMMKeys(mmkeys);
setLayout(layout);
setConnector(connector);
}
}
class Mouse implements PointingDevice{
String manufacturer,connector;
String make;
int buttons;
public Mouse(String manufacturer,String make,int buttons,String connector){
setManufacturer(manufacturer);
setMake(make);
setButtons(buttons);
setConnector(connector);
}
public void setManufacturer(String manufacturer){this.manufacturer=manufacturer;}
public void setMake(String make){this.make=make;}
public void setButtons(int buttons){this.buttons=buttons;}
public String getMake(){return make;}
public String getManufacturer(){return manufacturer;}
public int getButtons(){return buttons;}
public String getConnector(){return connector;}
public void setConnector(String connector){this.connector=connector;}
}

public class JavaInterfaceImpl{
public static void main(String s[]){
KeyBoard kb= new KeyBoard("Logtech","PLEOMAX",10,105,21,"ps/2");
InputDevice id=kb;
System.out.println(id.getManufacturer());
Mouse m =new Mouse("Microsoft","XXKB",3,"USB");
id=m;
System.out.println(id.getManufacturer());

}
}


The above example illustrates multiple inheritance of interfaces by another interface. similarly you can implement multiple interface to a class by seperating interface name with commas. be carefull to implement all methods of interfaces by the implementing class
< Inheritance |
Content |
Exception Handling in java >


You can Add a Classified here with New Classified