Interfaces In Java

  Program to illustrate the concept of Interfaces

package com.company;
interface bycycle{
void speed();// you can only initialize methods in interfaces
void brake();// you can't declare any method in interfaces
}
interface code{
void cls();
}
class cycle implements bycycle { //implement keyword for the interfaces
@Override
public void brake() { //you must declare all the methods of interfaces
// or it will show error
System.out.println("decrement the speed");
}
@Override
public void speed() {
System.out.println("increment the speed");
}
public void xls(){
System.out.println("hy I'm class object");//you can add methods to class
}
}
public class interfaces {
public static void main(String[] args) {
cycle c= new cycle();
c.brake();
c.speed();
c.xls();
}
}

program to use two different interfaces in single class and also use inheritance in same class

package com.company;
interface bycycle{
void speed();// you can only initialize methods in interfaces
void brake();// you can't declare any method in interfaces
}
interface code{
void cls();
}
class class2{// you can also extend the class using inheritance in interfaces
public void object(){
System.out.println("hy, I'm class2's object");
}
}
class cycle extends class2 implements bycycle, code {
//implement keyword for the interfaces
@Override
// here i used two different interfaces in single class i.e.(bycycle and code)
public void brake() {
System.out.println("decrement the speed");
}
@Override
public void speed() {
System.out.println("increment the speed");
}
public void xls(){
System.out.println("hy I'm class object");
}
public void cls(){
System.out.println("hy, I'm second interface");
}
}
public class interfaces {
public static void main(String[] args) {
cycle c= new cycle();
c.brake(); //interface1 object1
c.speed(); //interface2 object2
c.xls(); //child class object
c.cls(); //interface2 object
c.object(); //parent class object
}
}

program to illustrate the concept of default keyword in Interface

package com.company;
interface car{
void drive();
}
interface truck{
void Load();
// 'default' keyword is used it initialize object in interface
//this helps if you have lots of classes, there is no need to initialize same object again and again
//you just need to call the object with class reference
default void tyres(){
System.out.println("repair all tyres before going");
}
}
class vehicle implements car,truck{
public void drive(){
System.out.println("driving car in highway");
}
public void Load(){
System.out.println("loading things in truck");
}
}
public class defaultMETHODS {
public static void main(String[] args) {
vehicle v= new vehicle();
v.tyres();
v.drive();
v.Load();
}
}

Inheritance in interfaces

package com.company;
interface sample{
void method1();
void method2();
}
interface sample1 extends sample{
void method3();
void method4();
}
class random implements sample1{
public void method1(){
System.out.println("Hy, I'm method 1");
}
public void method2(){
System.out.println("Hy, I'm method 2");
}
public void method3(){
System.out.println("Hy, I'm method 3");
}
public void method4(){
System.out.println("Hy, I'm method 4");
}
}
public class inheritanceINinterfaces {
public static void main(String[] args) {
random r= new random();
r.method1();
r.method2();
r.method3();
r.method4();
}
}



No comments:

Post a Comment

    Hello, I'm Vishal Rana  I'm 19 years old  I live in Himachal Pradesh and  currently I'm studying in college for my UG degree...