Showing posts with label interfaces. Show all posts
Showing posts with label interfaces. Show all posts

Monday, 13 December 2021

Can an interface extend multiple interfaces in Java?

In Java, one interface can extend multiple interfaces.

 

Syntax

interface MyInterface extends interface1, interface2...interfaceN{
	......
	......
}

 

Example

public interface AppUtil extends WelcomeUtil, LogUtil{
	.....
	.....
}

 

 


 

Find the below working application.

 

WelcomeUtil.java

 

package com.sample.app.interfaces;

public interface WelcomeUtil {
	
	public void sayHi();

}

LogUtil.java

package com.sample.app.interfaces;

public interface LogUtil {

	public void log(String msg);
}

AppUtil.java

package com.sample.app.interfaces;

public interface AppUtil extends WelcomeUtil, LogUtil{

	public String appDetails();
}



You may like

Interview Questions

How to get the capacity of ArrayList in Java?

volatile reference vs Atomic references defined in java.util.concurrent.atomic package

Check whether I have JDK or JRE in my system

How to check whether string contain only whitespaces or not?

How to call base class method from subclass overriding method?

Thursday, 3 June 2021

Php: Interfaces

Interface is an abstract type that contain method signatures(It do not contain method definitions). Since interface describes the contract, it contains only public functions.

 

How to create an interface?

‘interface’ keyword is used to create an interface.

 

Syntax

interface InterfaceName{
    
    public function function_1(args);
    public function function_2(args);
    .....
    .....
    .....
}

 

Example

interface Circle{
    public function area_of_circle($radius);
    public function perimeter_of_circle($radius);
}

 

How to implement an interface?

Once an interface is defined, a class can implement this interface and provide definition of all the signatures declared interface.

 

Syntax

class ClassName implements InterfaceName{

    //Provide definition to all the method signatures of interface.

}

 

Example

class MyCircle implements Circle{
    public const PI = 3.14;

    public function area_of_circle($radius){
        return self::PI * $radius * $radius;
    }
    public function perimeter_of_circle($radius){
        return 2 * self::PI * $radius;
    }
}

 

Find the below working application.

 

interfaces.php

#!/usr/bin/php

<?php

interface Circle{
    public function area_of_circle($radius);
    public function perimeter_of_circle($radius);
}

class MyCircle implements Circle{
    public const PI = 3.14;

    public function area_of_circle($radius){
        return self::PI * $radius * $radius;
    }
    public function perimeter_of_circle($radius){
        return 2 * self::PI * $radius;
    }
}

$circle1 = new MyCircle();
$radius = 12;

$area = $circle1->area_of_circle($radius);
$perimeter = $circle1->perimeter_of_circle($radius);

echo "Radius of Circle : $radius\n";
echo "Area of Circle : $area\n";
echo "Perimeter of Circle : $perimeter\n";
?>

Output

$./interfaces.php 

Radius of Circle : 12
Area of Circle : 452.16
Perimeter of Circle : 75.36


 

Previous                                                    Next                                                    Home

Thursday, 26 December 2019

Why the interface variables are public, static, final by default?

As per Java specification, every field declaration in the body of an interface is implicitly public, static, and final.

Why the interface variables are static?
Interface variables are static because Java interfaces cannot be instantiated by their own, so the variables should be defined in static context.

Why the interface variables are final?
Interface is a specification. It can be implemented by multiple classes. If the variables are not final, then there is a chance that implementing classes can change the value of interface variable. To avoid this, variables in interface are implicitly final.

Why the interface variables are public?
To be able to access by all the implementing classes, interface variables are public.

You may like

Thursday, 11 January 2018

Kotlin: Overriding conflict while implementing interfaces

If you are implementing more than one interface and those interfaces have same method signatures, then you will end up in conflict scenario.

HelloWorld.kt
interface Interface1{
 fun welcomeMessage(name : String){
  println("Welcome $name")
 }
}

interface Interface2{
 fun welcomeMessage(name : String){
  println("Good Morning $name")
 }
}

class MyClass : Interface1, Interface2{

 
}

When you try to compile above program, you will end up in below error.

ERROR: Class 'MyClass' must override public open fun welcomeMessage(name: String): Unit defined in Interface1 because it inherits multiple interface methods of it (13, 1)

How to resolve above error?
By overriding the function welcomeMessage in MyClass, you can get rid of above error.

class MyClass : Interface1, Interface2 {
 override fun welcomeMessage(name: String) {
  println("Hello $name")
 }

}

Find the below working application.


HelloWorld.kt
interface Interface1 {
 fun welcomeMessage(name: String) {
  println("Welcome $name")
 }
}

interface Interface2 {
 fun welcomeMessage(name: String) {
  println("Good Morning $name")
 }
}

class MyClass : Interface1, Interface2 {
 override fun welcomeMessage(name: String) {
  println("Hello $name")
 }

}

fun main(args: Array<String>) {
 var obj = MyClass()
 obj.welcomeMessage("krishna")
}

Output
Hello Krishna

How can I call interface method implementations from class?
By using super keyword, you can call interface method implementations from a class.

Ex
class MyClass : Interface1, Interface2 {
         override fun welcomeMessage(name: String) {
                 super<Interface1>.welcomeMessage(name)
                 super<Interface2>.welcomeMessage(name)
         }
}

Find the below working application.

HelloWorld.kt

interface Interface1 {
 fun welcomeMessage(name: String) {
  println("Welcome $name")
 }
}

interface Interface2 {
 fun welcomeMessage(name: String) {
  println("Good Morning $name")
 }
}

class MyClass : Interface1, Interface2 {
 override fun welcomeMessage(name: String) {
  super<Interface1>.welcomeMessage(name)
  super<Interface2>.welcomeMessage(name)
 }
}

fun main(args: Array<String>) {
 var obj = MyClass()
 obj.welcomeMessage("krishna")
}

Output

Welcome krishna
Good Morning Krishna


Previous                                                 Next                                                 Home

Kotlin: Interfaces

What is an Interface ?
Interfaces are the contracts in the outside world.

What it mean?
Take an example, let us assume, there is a standard for all mobile phones, as per the standard, below are the basic functionality for the mobile phones to provide. 

Vendor1 provides the functionality for the mobile in language JAVA
Vendor2 provides the functionality for the mobile in language C++
Vendor3 provides the functionality for the mobile in language JAVA
Vendor4 provides the functionality for the mobile in language C

that mean all the vendors providing the same functionality, but the way they are providing is different.

One more example is IBM JAVA, Oracle JAVA are providing the functionality for JAVA, but the way they implemented the features is different.

How to define an interface in kotlin?
You can define the interfaces using 'interface' keyword. Interfaces in kotlin are similar to interfaces in Java8.

interface InterfaceName{
 /* Function without body*/
 fun method1()
 
 /* Function with body*/
 fun mehtod2(){
 
 }

}

How to implement an interface?
kotlin class can implement one or more interfaces.

Syntax
class ClassName : Interface1, Interface2...InterfaceN{

}

Find the below working application.


HelloWorld.kt

interface Arithmetic {
 fun sum(a: Int, b: Int): Int
 fun sub(a: Int, b: Int): Int
}

class ArithmeticImpl : Arithmetic {
 override fun sum(a: Int, b: Int): Int {
  return a + b
 }

 override fun sub(a: Int, b: Int): Int {
  return a - b
 }
}

fun main(args: Array<String>) {
 var obj = ArithmeticImpl()
 
 println("Sum of 10 and 20 is : ${obj.sum(10, 20)}")
 println("Sub of 10 and 20 is : ${obj.sub(10, 20)}")
}

Output

Sum of 10 and 20 is : 30
Sub of 10 and 20 is : -10

Can a class implement more than one interface?
yes, a class can implement more than one interfaces.

class ArithmeticImpl : Arithmetic, Message {

}

In the above example, "ArithmeticImpl" class is implementing Arithmetic and Message interfaces.

Find the below working application.


HelloWorld.kt
interface Arithmetic {
 fun sum(a: Int, b: Int): Int
 fun sub(a: Int, b: Int): Int
}

interface Message {
 fun amoutMe(): String
}

class ArithmeticImpl : Arithmetic, Message {
 override fun sum(a: Int, b: Int): Int {
  return a + b
 }

 override fun sub(a: Int, b: Int): Int {
  return a - b
 }

 override fun amoutMe(): String {
  return "ArithmeticImpl class"
 }
}

fun main(args: Array<String>) {
 var obj = ArithmeticImpl()

 println("Sum of 10 and 20 is : ${obj.sum(10, 20)}")
 println("Sub of 10 and 20 is : ${obj.sub(10, 20)}")
 println(obj.amoutMe())
}

Output

Sum of 10 and 20 is : 30
Sub of 10 and 20 is : -10
ArithmeticImpl class

Can I define properties in interface?
Yes, you can define properties in interfaces. Properties can be abstract (or) have definition.


HelloWorld.kt

interface DemoInterface {
 var variable1: String
 val variable2: String
  get() = "Hello World"
}

class MyClass : DemoInterface {
 override var variable1: String = "Hello"
}

fun main(args: Array<String>) {
 var obj = MyClass()

 println("variable1 : ${obj.variable1}")
 println("variable2 : ${obj.variable2}")
}

Output

variable1 : Hello
variable2 : Hello World



Previous                                                 Next                                                 Home