package <packagename>;
class ClassName
{
……..
……..
}
// Demo.java
package p1;
class Demo
{
public void m1()
{
System.out.println("Method m1..");
}
}
// Vehicle.java
package vehicles;
interface Vehicle
{
public void run();
public void speed();
}
//Bike.java
package vehicles;
public class Bike implements Vehicle
{
public void run()
{
System.out.println("Bike is running.");
}
public void speed()
{
System.out.println("Speed of Bike: 50 Km/h");
}
public static void main(String args[])
{
Bike bike = new Bike();
bike.run();
bike.speed();
}
}
class Demo extends java.util.Scanner
{
//statements
}
import java.util.Scanner;
class Demo
{
// statements
}
import java.util.*;
class Demo
{
// statements
}
interface InterfaceName
{
public void method1();
public void method2();
<type> variableName = value;
}
interface Employee
{
static final Id = 101;
static final String name = “ABC”;
void show();
void getSalary(double salary);
}
interface Base
{
public void display ();
}
interface Derive extends Base
{
public void show ();
}
class ClassName implements interfacename
{
// body of class
}
interface Results
{
final static float pi = 3.14f;
float areaOf(float l, float b);
}
class Rectangle implements Results
{
public float areaOf(float l, float b)
{
return (l * b);
}
}
class Square implements Results
{
public float areaOf(float l, float b)
{
return (l * l);
}
}
class Circle implements Results
{
public float areaOf(float r, float b)
{
return (pi * r * r);
}
}
public class InterfaceDemo
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Square square = new Square();
Circle circle = new Circle();
System.out.println("Area of Rectangle: "+rect.areaOf(20.3f, 28.7f));
System.out.println("Are of square: "+square.areaOf(10.0f, 10.0f));
System.out.println("Area of Circle: "+circle.areaOf(5.2f, 0));
}
}
interface Vehicle
{
void run();
}
interface Bike extends Vehicle
{
void stop();
}
public class Demo implements Bike
{
public void run()
{
System.out.println("Vehicle is running.");
}
public void stop()
{
System.out.println("Bike is stop.");
}
public static void main(String args[])
{
Demo obj = new Demo();
obj.run();
obj.stop();
}
}
Abstract class | Interface |
---|---|
It cannot support multiple inheritances. | Interface supports multiple inheritances. |
It contains both abstract and non abstract method. | It contains only abstract method. |
Abstract class is the partially implemented class. | Interface is fully unimplemented class. |
It can have main method and constructor. | It cannot have main method and constructor. |
It can have static, non-static, final, non-final variables. | It contains only static and final variable. |