Java Object-Oriented Programming Concepts

Mars

This article is a part of the Java Programming Language article series.

  • A programming language or a technique that supports classes and objects
  • Java is an object-oriented programming language
  • Is a blueprint
  • Has properties, methods, and nested classes
  • Instance that is created from class
  • Instance is an executable copy of the class
  • Models the real-world objects
  • Two characteristic: state and behavior
  • State: name, color, speed, on, off, volume
  • Behavior: changing gear, turn on, turn off, increase volume, decrease volume
  • state = fields
  • behavior = methods
  • Nested Classes
  • Anonymous Classes
  • Types: Static Nested Classes, Inner Classes
  • Logically grouping classes that are only used in one place
  • Increases encapsulation
  • Declare and instantiate a class at the same time
  • A set of predefined constants
  • Compass directions, the days of the week
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Provided by interface and abstract class
  • Interaction with the outside world through the methods
  • Forms a contract between the class and the outside world
  • All methods must be implemented in the class
  • public, static, final properties (fields)
  • public methods
  • Abstract classes cannot be instantiated, but they can be subclassed
  • Not static and final properties (fields) can be defined
  • public, protected, private methods can be defined
  • An abstract method is a method that is declared without an implementation
  • If a class includes abstract methods, then the class itself must be declared abstract

Abstract classes

  • Share code with related classes
  • Have many common methods or fields

Interfaces

  • Implementation with unrelated classes
  • Multiple inheritance
  • An abstract class does not need to implement all of the interface’s methods
  • Hiding properties and methods

Access level modifiers

  • public: Full access
  • protected: Subclass access
  • no modifier: Package access
  • private: Class access
  • Hiding internal state and interact through methods
  • Using common states and behaviors from other classes
  • All classes inherits from the java.lang.Object class
  • Interface is used for multiple inheritance
  • superclass: base, parent class
  • subclass: derived, extended, child class
  • A subclass can define its own unique behaviors while inheriting superclass features
  • Override parent class methods with different definitions in subclasses
  • Within an instance method or a constructor, this is a reference to the current object
  • The methods declared final cannot be overridden by subclasses
  • Methods called from constructors should generally be declared final to avoid surprising or undesirable results
  • A class that is declared final cannot be subclassed
  • Declaring methods with different method signatures

Instance Methods

  • A subclass method with the same signature and return type overrides the superclass method

Static Methods

  • A static subclass method with the same signature hides the static superclass method
  • Namespace that organizes a set of related classes and interfaces
  • Similar to folders on a computer