C
JV

C to Java

10 lessons

Progress0%
1Introduction to Java2Data Types3Strings4Arrays and Collections5Object-Oriented Programming6Exception Handling7Collections and Generics8Modern Java Features9Interfaces and Polymorphism10Threads and Concurrency
All Mirror Courses
C
JV
Object-Oriented Programming
MirrorLesson 5 of 10
Lesson 5

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.

Mirror Card
C
From C:

In C, you're familiar with classes, objects, and inheritance.

JV
In Java:

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:

JV
Java 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
C (What you know)
// 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);
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Classes combine data and methods

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Constructor is a special method

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Access modifiers (public, private, protected)

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

No manual memory management

Step-by-Step Breakdown

1. Class Structure

Classes encapsulate data (fields) and behavior (methods).

JV
Java
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
C
// C - manual vtable for polymorphism
JV
Java
public 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!"
Rule of Thumb
Java has single inheritance. Use interfaces for multiple type relationships.

3. Interfaces

Interfaces define contracts that classes must implement.

JV
Java
// 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)
Common Pitfall
Don't assume Java works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Classes combine data and methods
  • Use private for encapsulation
  • extends for inheritance, implements for interfaces
  • Java supports single inheritance only
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Java to practice these concepts.
PreviousNext