Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
••
Linux Tipps & HardeningCPU Graph, a scrubbable adjustable CPU monitor(30.09.2026 um 21:25 Uhr)
••
Linux Tipps & HardeningLinux 7.2.2 on iPhone XS? First boot!(30.09.2026 um 22:12 Uhr)
••
Linux Tipps & HardeningMinecraft Dungeons 2 fix for linux(01.10.2026 um 00:49 Uhr)
••
Linux Tipps & HardeningEuropean Citizens' Initiative(01.10.2026 um 04:15 Uhr)
••••
Linux Tipps & HardeningCPU Graph, a scrubbable adjustable CPU monitor(30.09.2026 um 21:25 Uhr)
••
Linux Tipps & HardeningLinux 7.2.2 on iPhone XS? First boot!(30.09.2026 um 22:12 Uhr)
••
Linux Tipps & HardeningMinecraft Dungeons 2 fix for linux(01.10.2026 um 00:49 Uhr)
••
Linux Tipps & HardeningEuropean Citizens' Initiative(01.10.2026 um 04:15 Uhr)
••
Intelligence View
⚡ tsecurity.de Intelligence

From Java OOP to Golang

As a Java developer, moving to Go can be a bit challenging, especially when it comes to understanding how Go handles object-oriented programming (OOP)…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

As a Java developer, moving to Go can be a bit challenging, especially when it comes to understanding how Go handles object-oriented programming (OOP) concepts. In this article, I will represent some common OOP concepts in Java and how they can be implemented in Go. This will help you transition from Java to Go more smoothly (hopefully, lol).






How To Make Go Classes in Go? (Compared to Java)



Let's say we have a class in Java like this:




class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}






In Go, we can create a similar structure using structs. Here's how you can do it:




type Person struct {
Name string
Age int
}






Go is more similar to C in this regard, where we use structs to define data types. We can also create methods for the struct, but Go does not have classes in the same way Java does.



Another example, we can create a service class in Java like this:




class PersonService {
public Person createPerson(String name, int age) {
return new Person(name, age);
}
}






In Go, we can achieve a similar functionality using a struct and methods. Here's how you can do it:




type PersonService struct{}
func (ps *PersonService) CreatePerson(name string, age int) Person {
return Person{Name: name, Age: age}
}






Classes in Go are not defined in the same way as in Java. Instead, we use structs to define data types and methods to define behaviors associated with those types.






How To Do Inheritance in Go (Compared to Java)



Inheritance in Java is very straightforward, where a class can extend another class. Here's an example in Java:




class Animal {
public void speak() {
System.out.println("Animal speaks");
}
}
class Dog extends Animal {
@Override
public void speak() {
System.out.println("Dog barks");
}
}






As Java developers, inheritance in Go is a bit weird at first. Go doesn't have traditional inheritance like Java, but it supports composition through struct embedding. Here's how we can achieve similar functionality in Go:




type Animal struct{}
func (a Animal) Speak() {
fmt.Println("Animal speaks")
}
type Dog struct {
Animal // Embedding for inheritance
}
func (d Dog) Speak() {
fmt.Println("Dog barks")
}









How To Do Interfaces in Go (Compared to Java)



In Java, we define interfaces like this:




interface Animal {
void Speak();
}
class Dog implements Animal {
@Override
public void Speak() {
System.out.println("Dog barks");
}
}






In Go, we define interfaces similarly, but we don't use the implements keyword. Here's how you can do it:




type Animal interface {
Speak()
}
type Dog struct{}
func (d Dog) Speak() {
fmt.Println("Dog barks")
}









How To Do Dependency Injection in Go (Compared to Java)



In Java, we often use interfaces and constructor injection for dependency injection. Here's an example:




class Service {
private final Repository repository;

public Service(Repository repository) {
this.repository = repository;
}

public void doSomething() {
repository.save();
}
}






In Go, we can achieve dependency injection using interfaces and struct embedding. Here's how you can do it:




type Repository interface {
Save()
}
type Service struct {
repo Repository
}
func NewService(repo Repository) Service {
return Service{repo: repo}
}
func (s Service) DoSomething() {
s.repo.Save()
}






That's it! You can see that Go has a different approach to OOP compared to Java, but it still allows you to implement similar concepts. The key differences are the use of structs instead of classes, the absence of traditional inheritance, and the way interfaces are defined and implemented.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten From Java OOP to Golang

Thematisch verwandte Begriffe: From, Java, Golang · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag