Saturday, 15 December 2012

Multiple Inheritance





                      Multiple Inheritance






Java does not support true multiple inheritance - true M.I. requires that a class be able to inherit directly from multiple classes. That is, in M.I., Class A can be a direct child of Class B and C. 

To get much of the functionality and advantages of the M.I. model, Java uses Interfaces. Thus, a Java Class A will be the child of Class B, but can implement several interfaces X, Y, and Z. This attempts to mimic the behavior that in a M.I. language would be given by Class A being a direct child of B, X, Y, and Z. 

The major difference between a M.I. language and a single-inheritance language with interfaces (like Java), is that interfaces can only define the method signatures and constants; they CANNOT provide implementations of those methods in the interface specification. Thus, any class which implements a given interface must provide an actual implementation of the methods that the interface describes. The big downside to this is that it can lead to a large amount of "code copying" - that is, if both Class A and Class B implement an interface X, there is a good chance that most of the methods from X will have the same code implementation in both Class A and B. Sometimes this is unavoidable; however, it is also considered good programming style to use abstract Classes to implement commonly used interfaces, then put that abstract class fairly high is the object heirarchy, allowing large numbers of classes to inherit from that abstract Class (which, in turn, means they have access to the implementation of those interfaces without having to copy the code). The advantage of interfaces is that they can keep things conceptually clean, and also make it simple for classes to decide how they want to implement the method contract the interface describes. 

Practically speaking, if you would like Class A to inherit from Class B and also to inherit from Class C, then, in Java, you would simulate this via interfaces this way: 

public class B { 
// the superclass 
methods_go_here_with_their_implementations; 
} 

public interface C { 
// the Java equivalent of Class C in a M.I. Language 
the_method_signatures_of_C_go_here; 
} 

public class A extends B implements C { 
implementations_of_methods_in_C_go_here; 
}








example


public interface TaggedInterface {
    void foo();
}

public interface XMLElementInterface {
    void bar();
}

public class Tagged implements TaggedInterface {
    // ...
}

public class XMLElement implements XMLElementInterface {
    // ...
}

public class TaggedXmlElement implements TaggedInterface, XMLElementInterface {
    private TaggedInterface tagged;
    private XMLElementInterface xmlElement;

    public TaggedXmlElement(TaggedInterface tagged, XMLElementInterface xmlElement) {
        this.tagged = tagged;
        this.xmlElement = xmlElement;
    }

    public void foo() {
        this.tagged.foo();
    }

    public void bar() {
        this.xmlElement.bar();
    }

    public static void main(String[] args) {
        TaggedXmlElement t = new TaggedXmlElement(new Tagged(), new XMLElement());
        t.foo();
        t.bar();
    }
}
Download Android Codes From SkyBlueAndroid

Saturday, 30 June 2012

JAVA AWT COMPLETE EXAMPLE 1

JAVA AWT COMPLETE EXAMPLE 1




import java.awt.*;
class buttondemo

{
buttondemo()
{
Frame f=new Frame();
f.setLayout(null);
f.setSize(1000,1000);
f.setVisible(true);
Button b=new Button();
b.setBounds(10,100,40,30);
b.setVisible(true);
f.add(b);

Button b1=new Button();
b1.setBounds(80,100,40,30);
b1.setVisible(true);
f.add(b1);

TextField t=new TextField();
t.setVisible(true);
t.setBounds(200,100,40,40);
f.add(t);

Label l1=new Label("demo");
l1.setBounds(100,150,50,25);
l1.setVisible(true);
Label l2=new Label("Label");
l2.setBounds(150,150,50,25);
l2.setVisible(true);
f.add(l1);
f.add(l2);

Checkbox c=new Checkbox("mac os");
Checkbox c1=new Checkbox("mac os1");
Checkbox c2=new Checkbox("mac os2");
c.setBounds(60,105,104,105);
c1.setBounds(261,114,214,115);
c2.setBounds(321,214,114,105);
f.setVisible(true);
c.setVisible(true);c1.setVisible(true);c2.setVisible(true);
f.add(c);f.add(c1);f.add(c2);

Choice c6=new Choice();
c6.setBounds(523,210,235,225);
c6.add("xp");
c6.add("mac");
f.add(c6);


}
public static void main(String args[])
{
new buttondemo();
}
}





Download Android Codes From SkyBlueAndroid

Sunday, 24 June 2012

nested try in exception handling

nested try in exception handling


class nestedtrydemo
{
public static void main(String args[])
{
try
{
int a=1;
int b=22/a;
System.out.println("a"+a);
try
{

int c[]={1};
c[42]=99;
}


catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array exception");
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array exception");
}

}
}


Saturday, 23 June 2012

multiple catch in exception handling in java -example program

multiple catch in exception handling in java -example program



class multicatch
{
public static void main(String args[])
{
try{
int a=0;
System.out.println("a="+a);
int b=42/a;
int c[]={1};
c[42]=99;
}
catch(ArithmeticException e)
{
System.out.println("division by zero"+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array exception");
}
}
}




Arithmetic Exception in java example

Arithmetic Exception in java example



import java.io.*;
class exceptiondemo
{
public static void main(String args[]) throws IOException
{

int d,a;
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

try{
a=Integer.parseInt(br.readLine());
d=Integer.parseInt(br.readLine());

int c=d/0;
System.out.println("this wiil not be printed");
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("division by zero");
}System.out.println("after catch");
}
}

user defined package in java a simple example

user defined package in java a simple example




 packagedemo.java




package p1;
public class packagedemo
{
public void show1()
{
int a=10;
int b=20;
int c=a+b;
System.out.println("Method show1 of Class packagedemo"+c);
}

}


steps


1  compile  the program

2 javac packagedemo.java

3 create the package  by using the next step

4 javac -d . packagedemo.java

5  import to the next calss

demopack.java



import p1.packagedemo; 
class demopack
{
public   void show2(){
System.out.println("Method m3 of Class c3");
}
public static void main(String args[]){
packagedemo obj1 = new packagedemo();
obj1.show1();

}
}

Thursday, 21 June 2012

interface in java with example

interface in java with example


interface area
{
void area();
}
class triangle implements area
{
int length=10,breadth=10,area;
public void area()
{
area=length*breadth;
System.out.println("Area is"+area);
}
}
class square implements area
{
int a=20,sqarea;
public void area()
{
sqarea=a*a;
System.out.println("Area of square is"+sqarea);
}
}
class interfac
{
public static void main(String args[])
{
triangle tri=new triangle();
tri.area();
square sq=new square();
sq.area();
}
}