0% found this document useful (0 votes)
10 views8 pages

Siddhant Patil - D10A - 50 - Java Exp6

This document outlines a Java lab experiment focused on implementing interfaces. It explains the concept of interfaces, their characteristics, and differences from classes, along with reasons for their use. The document also provides example code demonstrating the implementation of an interface in a vehicle context with classes for Bicycle, Bike, and Car.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Siddhant Patil - D10A - 50 - Java Exp6

This document outlines a Java lab experiment focused on implementing interfaces. It explains the concept of interfaces, their characteristics, and differences from classes, along with reasons for their use. The document also provides example code demonstrating the implementation of an interface in a vehicle context with classes for Bicycle, Bike, and Car.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA Lab

Lab Experiment no. 6


Name: Siddhant Krantikumar Patil Roll No. 50 Batch:C

Aim : write a java program to Implement the concept of interface .

Theory :

Q.1. What is an interface ?

An interface is a reference type in Java. It is similar to class. It is a collection of abstract


methods. A class implements an interface, thereby inheriting the abstract methods of the
interface.
Along with abstract methods, an interface may also contain constants, default methods, static
methods, and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and
behaviors of an object. And an interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need
to be defined in the class.
An interface is similar to a class in the following ways −
1. An interface can contain any number of methods.
2. An interface is written in a file with a .java extension, with the name of the interface
matching the name of the file.
3. The byte code of an interface appears in a .class file.
4. Interfaces appear in packages, and their corresponding bytecode file must be in a
directory structure that matches the package name.
However, an interface is different from a class in several ways, including −
1. You cannot instantiate an interface.
2. An interface does not contain any constructors.
3. All of the methods in an interface are abstract.
4. An interface cannot contain instance fields. The only fields that can appear in an
interface must be declared both static and final.
5. An interface is not extended by a class; it is implemented by a class.
6. An interface can extend multiple interfaces.
Q.2. Why And When To Use Interfaces?

1) To achieve security - hide certain details and only show the important details of an object
(interface).

2) Java does not support "multiple inheritance" (a class can only inherit from one super class).
However, it can be achieved with interfaces, because the class can implement multiple
interfaces.

Q.3. explain interface inheritance with an example.

When a class implements an interface, you can think of the class as signing a contract, agreeing
to perform the specific behaviors of the interface. If a class does not perform all the behaviors of
the interface, the class must declare itself as abstract.

A class uses the implements keyword to implement an interface. The implements keyword
appears in the class declaration following the extends portion of the declaration.

Example

/* File name : MammalInt.java */

public class MammalInt implements Animal {

public void eat() {

System.out.println("Mammal eats");

public void travel() {

System.out.println("Mammal travels");

public int noOfLegs() {

return 0;

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

MammalInt m = new MammalInt();

m.eat();

m.travel();

This will produce the following result −

Output

Mammal eats

Mammal travels
Source code :

import java.util.Scanner;

interface Vehicle

void Set_Descriptions(String a ,String b,int c,String d,int e);

void Display_Descriptions();

class Bicycle implements Vehicle

String company_name;

String colour;

int registration_NO;

String name_plate;

int no_of_wheel;

public void Set_Descriptions(String a,String b,int c,String d,int e)

this.company_name=a;

this.colour=b;

this.registration_NO=c;

this.name_plate=d;

this.no_of_wheel=e;

public void Display_Descriptions()

System.out.println("company name :"+this.company_name );


System.out.println("colour :"+this.colour);

System.out.println("registration number:"+this.registration_NO);

System.out.println("name:"+this.name_plate);

System.out.println("number of wheel :"+this.no_of_wheel);

class Bike implements Vehicle

String company_name;

String colour;

int registration_NO;

String name_plate;

int no_of_wheel;

public void Set_Descriptions(String a,String b,int c,String d,int e)

this.company_name=a;

this.colour=b;

this.registration_NO=c;

this.name_plate=d;

this.no_of_wheel=e;

public void Display_Descriptions()

System.out.println("company name :"+this.company_name );

System.out.println("colour :"+this.colour);
System.out.println("registration number:"+this.registration_NO);

System.out.println("name:"+this.name_plate);

System.out.println("number of wheel :"+this.no_of_wheel);

class Car implements Vehicle

String company_name;

String colour;

int registration_NO;

String name_plate;

int no_of_wheel;

public void Set_Descriptions(String a,String b,int c,String d,int e)

this.company_name=a;

this.colour=b;

this.registration_NO=c;

this.name_plate=d;

this.no_of_wheel=e;

public void Display_Descriptions()

System.out.println("company name :"+this.company_name );

System.out.println("colour :"+this.colour);

System.out.println("registration number:"+this.registration_NO);
System.out.println("name:"+this.name_plate);

System.out.println("number of wheel :"+this.no_of_wheel);

public class Interface

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

Car obj= new Car();

System.out.println("Enter the company of car:");

String a=sc.nextLine();

System.out.println("Enter the colour of car :");

String b=sc.nextLine();

System.out.println("Enter the registration number of car:");

int c=sc.nextInt();

System.out.println("Enter the name plate :");

sc.nextLine();

String d=sc.nextLine();

System.out.println("Enter the number of wheel:");

int e=sc.nextInt();

obj.Set_Descriptions(a,b,c,d,e);

System.out.println("Description of car:");

obj.Display_Descriptions();

}
Output :

You might also like