Object-Oriented Programming
Classes, objects, and inheritance
Introduction
In this lesson, you'll learn about object-oriented programming in Java. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C, you're familiar with classes, objects, and inheritance.
Java has its own approach to classes, objects, and inheritance, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
// Java - native OOP
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void birthday() {
this.age++;
}
public String getName() { return name; }
public int getAge() { return age; }
}
// Usage
Person p = new Person("Alice", 30);
p.birthday();Comparing to C
Here's how you might have written similar code in C:
// C - simulating OOP with structs
typedef struct {
char name[50];
int age;
} Person;
Person* person_create(const char* name, int age) {
Person* p = malloc(sizeof(Person));
strcpy(p->name, name);
p->age = age;
return p;
}
void person_birthday(Person* p) {
p->age++;
}
void person_destroy(Person* p) {
free(p);
}You may be used to different syntax or behavior.
Classes combine data and methods
You may be used to different syntax or behavior.
Constructor is a special method
You may be used to different syntax or behavior.
Access modifiers (public, private, protected)
You may be used to different syntax or behavior.
No manual memory management
Step-by-Step Breakdown
1. Class Structure
Classes encapsulate data (fields) and behavior (methods).
public class Car {
// Fields (instance variables)
private String model;
private int year;
// Constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
// Methods
public void drive() {
System.out.println(model + " is driving");
}
// Getters and Setters
public String getModel() { return model; }
public void setModel(String model) { this.model = model; }
}2. Inheritance
Classes can inherit from other classes using extends.
// C - manual vtable for polymorphismpublic class Animal {
protected String name;
public void speak() {
System.out.println("...");
}
}
public class Dog extends Animal {
@Override
public void speak() {
System.out.println("Woof!");
}
}
Animal a = new Dog(); // polymorphism
a.speak(); // "Woof!"3. Interfaces
Interfaces define contracts that classes must implement.
// Define behavior contract
public interface Drawable {
void draw();
}
public interface Resizable {
void resize(int width, int height);
}
// Class can implement multiple interfaces
public class Rectangle implements Drawable, Resizable {
public void draw() { ... }
public void resize(int w, int h) { ... }
}Common Mistakes
When coming from C, developers often make these mistakes:
- Classes combine data and methods
- Constructor is a special method
- Access modifiers (public, private, protected)
Key Takeaways
- Classes combine data and methods
- Use private for encapsulation
- extends for inheritance, implements for interfaces
- Java supports single inheritance only