DEV Community

Cover image for "Key Syntax Differences in Object-Oriented Programming: Python vs. Java”
Mahia  Momo
Mahia Momo

Posted on

2

"Key Syntax Differences in Object-Oriented Programming: Python vs. Java”

class & object

python code:

#Student is the class
class Student:
    name = "Momo";

#here s1 is the object of Student class
s1 = Student()   
print(s1.name)

Enter fullscreen mode Exit fullscreen mode

constructor

python code

class Student:

    def __init__(self,fullname):  #constructor 
        self.name = fullname

s1 = Student("Momo")
print(s1.name)

Enter fullscreen mode Exit fullscreen mode

class & object in java

java code:

// Student is the class
class Student {
    String name = "Momo";
}

// Main class to test the Student class
public class Main {
    public static void main(String[] args) {
        // s1 is the object of the Student class
        Student s1 = new Student();
        System.out.println(s1.name);
    }
}

Enter fullscreen mode Exit fullscreen mode

constructor in java

java code:

// Student class
class Student {
    String name; // Instance variable

    // Constructor
    public Student(String fullname) {
        this.name = fullname;
    }
}

// Main class to test the Student class
public class Main1 {
    public static void main(String[] args) {
        // Create an object of the Student class
        Student s1 = new Student("Momo");
        // Print the name
        System.out.println(s1.name);
    }
}

Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise

Why not using a record; is easier:

record Student(String name) {}

public class Main1 {
    public static void main(String[] args) {
        var s1 = new Student("Momo");
        System.out.println(s1.fname());
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mahiamomo profile image
Mahia Momo

ok i'll remind it thank you so much for the suggestion

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay