Skip to main content

Java For Python Programmers Edition 2

Section 6.9 Summary & Reading Questions

  1. In Java, instance variables (fields) must be declared in the class body before they are used. Unlike Python, you cannot dynamically add new instance variables to an object at runtime.
  2. Java uses access modifiers like private to enforce encapsulation, encouraging data hiding and controlled access through getter and setter methods.
  3. Java requires a constructor method to initialize objects. A constructor has the same name as the class and defines its parameters explicitly, whereas Python uses the __init__ method.
  4. Every Java class inherits from the Object class, which provides default methods like toString() and equals(). Overriding toString() gives more meaningful output when printing objects (similar to Python’s __str__).
  5. By default, Java’s equals() method checks reference equality, just like == for objects. To compare objects based on content (like Fraction values), you must override equals() and call it explicitly.
  6. Java supports inheritance through abstract classes (like Number) and interfaces. Extending an abstract class requires implementing its abstract methods, allowing objects like Fraction to be used where a Number is expected.

Reading Questions Reading Questions

1.

How are instance variables declared in Java compared to Python?
  • They can be created dynamically anywhere in the class like Python.
  • No, Java does not allow dynamic creation of instance variables at runtime.
  • Instance variables are declared inside methods only.
  • No, instance variables are declared in the class body, not in methods.
  • They must be declared in the class body before use.
  • Correct! Java requires instance variables (fields) to be declared in the class body.
  • Java does not use instance variables.
  • No, instance variables are fundamental in Java classes.

2.

What Java feature encourages encapsulation and controlled access to instance variables?
  • Declaring all variables as public.
  • No, that would expose data and reduce encapsulation.
  • Using access modifiers like private and providing getter/setter methods.
  • Right! This is how Java enforces encapsulation.
  • Using global variables.
  • No, Java does not support global variables and this reduces encapsulation.
  • Avoiding the use of classes altogether.
  • No, encapsulation is a class-based concept in Java.

3.

How does Java initialize objects differently than Python?
  • Java uses a constructor method named after the class with explicit parameters.
  • Correct! Unlike Python’s __init__, Java constructors share the class name.
  • Java uses the __init__ method like Python.
  • No, Java does not have __init__.
  • Java initializes objects automatically without constructors.
  • No, Java requires constructors for explicit initialization.
  • Java uses global initialization functions instead of constructors.
  • No, Java uses constructors, not global functions, for object initialization.

4.

What must you do in Java to print objects in a readable way and compare two objects based on their contents rather than their memory references?
  • Use == for content comparison and no need to override toString().
  • No, == compares memory references, not content.
  • Only override toString() and use == for equality.
  • No, you should override equals() to compare content correctly.
  • Java automatically handles content comparison without overrides.
  • No, default equals() compares references, not content.
  • Override toString() for printing and override equals() to compare object contents.
  • Yes! This improves output and content-based comparison.
You have attempted of activities on this page.