Java Memory Management and Inheritance Principles

Posted by Anonymous and classified in Computers

Written on in English with a size of 521.39 KB

Memory Management: Stack vs. Heap

The memory in Java is divided into two main areas:

  • Stack: Used for local primitive variables, local reference variables, and method calls.
  • Heap: Used for newly created objects.

Common Errors:

  • StackOverflowError: Occurs when the stack is overused.
  • OutOfMemoryError: Occurs when the heap is too full.

Access Control and Class Structure

Standard practices for class design include:

  • Public: Classes and constructors.
  • Private: Instance variables.
  • Public: Getters and setters to control access to class members.

Instance vs. Static:

  • Instance: Belongs to a particular object.
  • Static: Belongs to the class itself and is shared by all objects.

Variables and Data Types

Local variables must be initialized before use. When using arrays, they store objects rather than primitives. Note that arrays do not automatically create multiple objects.

Wrapper Classes: Primitives are not objects. Wrapper classes (e.g., Integer, Double, Character) allow primitives to be treated as objects. Use BigDecimal for high-precision decimal arithmetic.

Inheritance and Relationships

  • Composition: Represents a "has-a" relationship.
  • Inheritance: Represents an "is-a" relationship.

A superclass (parent/base class) is extended by a subclass (child class). For example: class Dog extends Animal.

Inheritance Rules

  • The subclass inherits properties and methods from the superclass and can add specialized fields.
  • A subclass is not a subset of its superclass; it usually contains more information and methods.
  • Superclass constructors are not inherited but can be invoked using super().
  • The statement super() invokes the no-arg constructor of the superclass, while super(arguments) invokes the matching constructor.
  • Always define a no-arg constructor for classes intended to be superclasses.
  • Use super.method() to call superclass methods and super.setter() to modify superclass fields.

Constructor Chaining

Constructing an instance invokes the constructors of all superclasses in the inheritance chain. The subclass constructor automatically calls the superclass constructor before executing its own logic.

Overriding Methods

  • To override, a subclass must define a method with the same signature and return type as the superclass.
  • Private methods cannot be overridden.
  • Static methods can be inherited but not overridden; they are hidden if redefined in a subclass.

Overriding vs. Overloading

  • Overloading: Defining multiple methods with the same name but different signatures within the same or related classes.
  • Overriding: Providing a new implementation for a method in a subclass with the same signature and return type.

+dbeZGAAAABklEQVQDANRBgfGGLsnXAAAAAElFTkSuQmCC

9HOmp4AAAABklEQVQDAIcJw4daNkxtAAAAAElFTkSuQmCC

vlw6zgAAAAZJREFUAwAZGinX1jONQQAAAABJRU5ErkJggg==

Mi37AAAAAElFTkSuQmCC

mIAAAAAGSURBVAMAuUEWBcuyzNEAAAAASUVORK5CYII=

haUvnwAAAAZJREFUAwBTESAPdwU7VgAAAABJRU5ErkJggg==

Related entries: