<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: VibhorDubey</title>
    <description>The latest articles on DEV Community by VibhorDubey (@vibhordubey333).</description>
    <link>https://dev.to/vibhordubey333</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F400233%2F7e231474-6901-4589-abab-dca09932e512.png</url>
      <title>DEV Community: VibhorDubey</title>
      <link>https://dev.to/vibhordubey333</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vibhordubey333"/>
    <language>en</language>
    <item>
      <title>Interface Usage Golang vs Java</title>
      <dc:creator>VibhorDubey</dc:creator>
      <pubDate>Mon, 06 Oct 2025 11:12:59 +0000</pubDate>
      <link>https://dev.to/vibhordubey333/interface-usage-golang-vs-java-36gd</link>
      <guid>https://dev.to/vibhordubey333/interface-usage-golang-vs-java-36gd</guid>
      <description>&lt;p&gt;While experimenting with interfaces in Java and Golang, I noticed some fundamental differences in how each language approaches abstraction. This post summarizes those insights for anyone curious about both worlds.&lt;/p&gt;

&lt;h2&gt;
  
  
  A. Definition and Declaration
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In Java:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An interface is an explicit construct declared using the interface keyword.&lt;/li&gt;
&lt;li&gt;Classes must explicitly declare that they implement an interface using the implements keyword.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Shape {
    void draw();
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In Go:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Shape interface {
    Draw()
}

type Circle struct{}

func (c Circle) Draw() {
    fmt.Println("Drawing Circle")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An interface is also declared using the interface keyword, but implementation is implicit.&lt;/p&gt;

&lt;p&gt;A type (struct) implements an interface automatically if it defines all the methods in that interface.&lt;/p&gt;

&lt;p&gt;👉 Key difference:&lt;br&gt;
In Java, a class must declare that it implements an interface.&lt;br&gt;
In Go, a type implements an interface implicitly — no need to declare it.&lt;/p&gt;

&lt;h2&gt;
  
  
  B. Method Implementation and Type Checking
&lt;/h2&gt;

&lt;p&gt;Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Statically checked at compile time.&lt;/li&gt;
&lt;li&gt;If a class claims to implement an interface but doesn’t define all methods, compilation fails.&lt;/li&gt;
&lt;li&gt;Strongly tied to inheritance hierarchy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Also statically checked, but &lt;a href="https://stackoverflow.com/questions/4205130/what-is-duck-typing" rel="noopener noreferrer"&gt;duck typing&lt;/a&gt; applies.&lt;/li&gt;
&lt;li&gt;As long as a type has all methods required by an interface, it’s valid.&lt;/li&gt;
&lt;li&gt;Promotes composition over inheritance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  C. Multiple Interfaces and Composition
&lt;/h2&gt;

&lt;p&gt;Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class can implement multiple interfaces.&lt;/li&gt;
&lt;li&gt;Interface composition is explicit (using extends).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface A { void a(); }
interface B { void b(); }
interface C extends A, B { void c(); }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interfaces are composable — you can embed one interface into another.&lt;/li&gt;
&lt;li&gt;This is done by embedding rather than inheritance.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type A interface { a() }
type B interface { b() }
type C interface {
    A
    B
    c()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Go favors composition, while Java relies on inheritance.&lt;/p&gt;

&lt;h2&gt;
  
  
  D. Default Methods
&lt;/h2&gt;

&lt;p&gt;Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since Java 8, interfaces can have default and static methods with implementation.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Shape {
    default void info() {
        System.out.println("I am a shape");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interfaces cannot have any implementation — they are purely abstract.&lt;/li&gt;
&lt;li&gt;Behavior must be defined on concrete types.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  E. Nil and Zero Values
&lt;/h2&gt;

&lt;p&gt;Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;null can represent an interface reference with no object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The zero value of an interface is nil.&lt;/li&gt;
&lt;li&gt;If an interface variable is nil, calling a method on it will panic.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  F. Performance and Memory
&lt;/h2&gt;

&lt;p&gt;Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses JVM vtables for dynamic dispatch.&lt;/li&gt;
&lt;li&gt;Garbage collection handles object lifetime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses interface value pairs (type + data pointer).&lt;/li&gt;
&lt;li&gt;Interface dispatch is efficient but slightly slower than direct method calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hoping this article helped in understanding the nuances of interface in Java &amp;amp; Golang.&lt;/p&gt;

&lt;p&gt;Happy Coding - onwards and upwards 🚀&lt;/p&gt;

</description>
      <category>go</category>
      <category>java</category>
      <category>programming</category>
      <category>oop</category>
    </item>
    <item>
      <title>Generics In Golang</title>
      <dc:creator>VibhorDubey</dc:creator>
      <pubDate>Mon, 09 Jan 2023 12:17:59 +0000</pubDate>
      <link>https://dev.to/vibhordubey333/genrics-in-golang-4hgc</link>
      <guid>https://dev.to/vibhordubey333/genrics-in-golang-4hgc</guid>
      <description>&lt;p&gt;Generics in Go got introduced in release 1.18.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are generics?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Generics is a mechanism through which we can pass any type of value in a function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What were the issues prior to Go version 1.18?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import "fmt"

func main() {
    stringObject := "Hello"
    floatObject := 3.14
    intObject := 7

    assertType(stringObject)
    assertType(intObject)
    assertType(floatObject)
}

func assertType(object interface{}) {
    switch object.(type) {
    case string:
        fmt.Println("Received Type:", object.(string))
    case int:
        fmt.Println("Received Type:", object.(int))
    default:
        fmt.Println("Unknown Type")
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, every time we need to infer the type if we infer it with wrong datatype it will result into panic. Let's see another simple example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import "fmt"

func main() {
    stringObject := "Hello"
    printValue(stringObject)
}

func printValue(object interface{}) {
    fmt.Println(object.(int))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're trying to infer the string as int so it will result in a panic. So if we don't use the correct type it will cause panic in our program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Genrics Code Example?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A. Any&lt;/strong&gt;&lt;br&gt;
Let's see our first example. Where we'll pass any value in function be it array, int, string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"
)

func assertType(T any) {
    fmt.Println(T)
}
func assertTypeArray[T any](arrayObject []T) []T {
    for i := range arrayObject {
        fmt.Println(i)
    }
    return arrayObject
}
func main() {
    assertType("Hello")
    assertType(12.32)

    array := []int{1, 2, 3}
    arrayObject := assertTypeArray(array)
    fmt.Println(arrayObject)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;B. Predefined constraints.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can define the datatypes in interface like Integer, Float then embed inside Number interface.  Using predefined constraint we can restrict the input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "fmt"
)

type Integer interface {
    ~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Float interface {
    ~float32 | ~float64
}

type Number interface {
    Integer | Float
}

func Min[T Number](x, y T) T {
    if x &amp;lt; y {
        return x
    }
    return y
}

type IntType int64

func main() {
    x, y := 1, 3
    a, b := 1.1, 4.5
    k, v := IntType(-12344), IntType(12)
    fmt.Println(Min(x, y))
    fmt.Println(Min(a, b))
    fmt.Println(Min(k, v))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>go</category>
    </item>
  </channel>
</rss>
