Java vs Python - Concept Comparison
1. Functions & Recursion
Java: - Functions defined inside classes (methods). - Recursion similar to other languages.
Example:
static int fact(int n) { if (n == 0) return 1; return n * fact(n - 1); }
Python: - Functions defined using def keyword. - Recursion works the same way. Example:
def fact(n): if n == 0: return 1 return n * fact(n-1)
2. Arrays
Java: - Fixed size, strongly typed. Example: int[] arr = {1,2,3};
Python: - Use lists (dynamic size). Example: arr = [1,2,3]
3. ArrayList vs List
Java: - ArrayList is dynamic array. Example: ArrayList list = new ArrayList<>();
Python: - Lists are built-in and dynamic. Example: list = []
4. Encapsulation
Java: - Use private variables + getters/setters. Example:
class Student { private int age; public void setAge(int a) { if (a>0) age
= a; } public int getAge() { return age; } }
Python: - Use underscore naming + @property. Example:
class Student: def __init__(self): self._age = 0 @property def age(self):
return self._age @[Link] def age(self, a): if a > 0: self._age = a
5. Inheritance & super/this
Java: - 'extends' keyword, use super to call parent. Example:
class Animal { void sound(){...} } class Dog extends Animal { void
sound(){ [Link](); ... } }
Python: - Parent class in parentheses, use super(). Example:
class Animal: def sound(self): ... class Dog(Animal): def sound(self):
super().sound() ...
6. Abstraction
Java: - abstract class or interface. Example:
abstract class Shape { abstract void draw(); }
Python: - Use abc module (Abstract Base Classes). Example:
from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def
draw(self): pass
7. Interface
Java: - Interface = contract (methods only). Example:
interface Switchable { void turnOn(); }
Python: - No strict interfaces, but abstract base classes mimic this. Example:
from abc import ABC, abstractmethod class Switchable(ABC):
@abstractmethod def turnOn(self): pass
8. final, static, instanceof
Java: - final = constant / no override / no inheritance. - static = class level variable/method. -
instanceof = type check.
Python: - final (typing module in Python 3.8+). - staticmethod/classmethod decorators for
class-level behavior. - isinstance(obj, Class) for type check.