Java                                                          Home : www.sharetechnote.com

 

 

 

 

Java - Class - Interface

 

 

 

Single Inheritance (Single Interface)

 

 

InterfaceEx01.java

interface NumberProperty {

        public double Magnitude();

};

 

class Vector2D implements NumberProperty{

        private double ex1,ey1,ex2,ey2;

        

        Vector2D(double x1, double y1, double x2, double y2)

        {

                ex1 = x1;

                ey1 = y1;

                ex2 = x2;

                ey2 = y2;

        };

        

        public double Magnitude()

        {

            double mag;

            mag = Math.sqrt((ex2-ex1)*(ex2-ex1) + (ey2-ey1)*(ey2-ey1));

            

            return mag; 

        };

};

 

class Vector3D implements NumberProperty{

        private double ex1,ey1,ez1,ex2,ey2,ez2;

        

        Vector3D(double x1, double y1, double z1, double x2, double y2, double z2)

        {

                ex1 = x1;

                ey1 = y1;

                ez1 = z1;

                ex2 = x2;

                ey2 = y2;

                ez2 = z2;

        };

        

        public double Magnitude()

        {

            double mag;

            mag = Math.sqrt((ex2-ex1)*(ex2-ex1) + (ey2-ey1)*(ey2-ey1) + (ez2-ez1)*(ez2-ez1));

            

            return mag; 

        };

};

 

public class InterfaceEx01 {

 

    public static void main(String[] args) {

        Vector2D v2 = new Vector2D(0.0,0.0,1.0,1.0);

        Vector3D v3 = new Vector3D(0.0,0.0,0.0,1.0,1.0,1.0);

        System.out.println(v2.Magnitude());

        System.out.println(v3.Magnitude());

    }

 

}

 

 

    C:\temp>java InterfaceEx01

     

    1.4142135623730951

    1.7320508075688772

 

 

 

Multiple Inheritance (Multiple Interface)

 

 

InterfaceEx02.java

interface NumberProperty {

        public double Magnitude();

};

 

interface ObjectProperty {

        public String ObjectType();

};

 

class Vector2D implements NumberProperty, ObjectProperty {

        private double ex1,ey1,ex2,ey2;

        

        Vector2D(double x1, double y1, double x2, double y2)

        {

                ex1 = x1;

                ey1 = y1;

                ex2 = x2;

                ey2 = y2;

        };

        

        public double Magnitude()

        {

            double mag;

            mag = Math.sqrt((ex2-ex1)*(ex2-ex1) + (ey2-ey1)*(ey2-ey1));

            

            return mag; 

        };

        

        public String ObjectType()

        {

            return "Vector2D";          

        }

};

 

 

class Vector3D implements NumberProperty, ObjectProperty{

        private double ex1,ey1,ez1,ex2,ey2,ez2;

        

        Vector3D(double x1, double y1, double z1, double x2, double y2, double z2)

        {

                ex1 = x1;

                ey1 = y1;

                ez1 = z1;

                ex2 = x2;

                ey2 = y2;

                ez2 = z2;

        };

        

        public double Magnitude()

        {

            double mag;

            mag = Math.sqrt((ex2-ex1)*(ex2-ex1) + (ey2-ey1)*(ey2-ey1) + (ez2-ez1)*(ez2-ez1));

            

            return mag; 

        };

        

        public String ObjectType()

        {

            return "Vector3D";          

        }

};

 

public class InterfaceEx02 {

 

    public static void main(String[] args) {

        Vector2D v2 = new Vector2D(0.0,0.0,1.0,1.0);

        Vector3D v3 = new Vector3D(0.0,0.0,0.0,1.0,1.0,1.0);

        System.out.println(v2.ObjectType());

        System.out.println(v2.Magnitude());

        System.out.println(v3.ObjectType());

        System.out.println(v3.Magnitude());

    }

 

}

 

 

    C:\temp>java InterfaceEx02

     

    Vector2D

    1.4142135623730951

    Vector3D

    1.7320508075688772

 

 

 

Packaging Multiple  Interfaces (An Interface inheriting Multiple Interfaces)

 

 

InterfaceEx03.java

interface NumberProperty {

        public double Magnitude();

};

 

interface ObjectProperty {

        public String ObjectType();

};

 

interface CommonInterfaces extends NumberProperty, ObjectProperty {

    

};

 

class Vector2D implements CommonInterfaces {

        private double ex1,ey1,ex2,ey2;

        

        Vector2D(double x1, double y1, double x2, double y2)

        {

                ex1 = x1;

                ey1 = y1;

                ex2 = x2;

                ey2 = y2;

        };

        

        public double Magnitude()

        {

            double mag;

            mag = Math.sqrt((ex2-ex1)*(ex2-ex1) + (ey2-ey1)*(ey2-ey1));

            

            return mag; 

        };

        

        public String ObjectType()

        {

            return "Vector2D";          

        }

};

 

class Vector3D implements CommonInterfaces{

        private double ex1,ey1,ez1,ex2,ey2,ez2;

        

        Vector3D(double x1, double y1, double z1, double x2, double y2, double z2)

        {

                ex1 = x1;

                ey1 = y1;

                ez1 = z1;

                ex2 = x2;

                ey2 = y2;

                ez2 = z2;

        };

        

        public double Magnitude()

        {

            double mag;

            mag = Math.sqrt((ex2-ex1)*(ex2-ex1) + (ey2-ey1)*(ey2-ey1) + (ez2-ez1)*(ez2-ez1));

            

            return mag; 

        };

        

        public String ObjectType()

        {

            return "Vector3D";          

        }

};

 

 

public class InterfaceEx03 {

 

    public static void main(String[] args) {

        

        CommonInterfaces[] Objects = new CommonInterfaces[] {

                                              new Vector2D(0.0,0.0,1.0,1.0),

                                              new Vector3D(0.0,0.0,0.0,1.0,1.0,1.0)

                                    };

        

        for (int i = 0; i < Objects.length; i++)

        {

            System.out.println(Objects[i].ObjectType());

            System.out.println(Objects[i].Magnitude());

        }

      

    }

 

}

 

 

    C:\temp>java InterfaceEx03

     

    Vector2D

    1.4142135623730951

    Vector3D

    1.7320508075688772