Object-Oriented Programming in Java
In this lesson, we’ll explore Object-Oriented Programming (OOP) in Java, comparing it to Python’s approach. While both languages support OOP, Java’s implementation has some distinct features that you’ll need to understand as a Python developer transitioning to Java.
Classes and Objects
Let’s start with the basics. In both Python and Java, classes are blueprints for objects. However, the syntax and some concepts differ:
// Java
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void sayHello() {
System.out.println("Hello, I'm " + name);
}
}
In Java, we use the public
keyword to make the class accessible from other parts of the program. The private
keyword is used for encapsulation, which we’ll discuss shortly.
To create an object in Java:
// Java
Person person = new Person("Alice", 30);
person.sayHello();
Notice the use of the new
keyword, which is not present in Python. This keyword explicitly calls the constructor and allocates memory for the object.
Constructors
In Python, you use the __init__
method to initialize objects. Java uses constructors, which have the same name as the class:
// Java
public Person(String name, int age) {
this.name = name;
this.age = age;
}
The this
keyword is similar to Python’s self
, referring to the current instance of the class.
Access Modifiers
Java provides access modifiers to control the visibility of class members. This concept is not present in Python and is crucial for encapsulation in Java:
public
: Accessible from any other classprivate
: Accessible only within the same classprotected
: Accessible within the same package and subclasses- Default (no modifier): Accessible within the same package
// Java
public class Person {
private String name; // Only accessible within this class
public int age; // Accessible from any other class
protected String address; // Accessible in same package and subclasses
String phoneNumber; // Default: accessible in same package
}
Inheritance
Inheritance in Java is similar to Python, but with some differences:
// Java
public class Employee extends Person {
private String company;
public Employee(String name, int age, String company) {
super(name, age); // Call the parent constructor
this.company = company;
}
}
In Java, we use the extends
keyword instead of Python’s parentheses syntax. The super
keyword is used to call the parent constructor, similar to super()
in Python.
Abstract Classes and Interfaces
Java supports abstract classes and interfaces, which are powerful tools for designing class hierarchies:
// Java
public abstract class Shape {
abstract double area();
}
public interface Drawable {
void draw();
}
public class Circle extends Shape implements Drawable {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
Abstract classes can have both abstract and concrete methods, while interfaces (prior to Java 8) can only have abstract methods. From Java 8 onwards, interfaces can also have default and static methods.
Method Overriding
Method overriding in Java is similar to Python, but Java provides the @Override
annotation:
// Java
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
The @Override
annotation is optional but recommended. It helps catch errors if you accidentally mis-type the method name or signature.
Conclusion
In this lesson, we’ve covered the fundamental aspects of Object-Oriented Programming in Java, highlighting the differences from Python. We’ve explored classes and objects, constructors, access modifiers, inheritance, abstract classes, interfaces, and method overriding.
Java’s OOP model is more rigid and explicit compared to Python’s, with features like access modifiers and the new
keyword for object creation. These features provide stronger encapsulation and clearer structure, which can be beneficial in large-scale applications.
In the next lesson, we’ll dive into String manipulation and Regular Expressions in Java, exploring how these operations differ from their Python counterparts.