oop-midterm-jy
oop-midterm-jy
• a member belongs to the class itself rather than to instances of the class.
• It can be used in a field, method or nested classes.
• It is also used in several contexts to create class-level methods and variables that
are shared among all instances of the class.
• A static variable belongs to the class, not to any specific object of the class.
• All instances of the class share the same static variable.
• Static variables can be accessed directly using the class name, without creating an
object.
STATIC METHODS
STATIC BLOCKS
STATIC CLASSES
• A static nested class is a class that is defined within another class and is declared
static.
• A static nested class can be instantiated without an instance of the outer class.
• What is pass-by-value?
• What is pass-by-reference?
• What is the difference between object and a reference?
• Primitive types: When you pass a primitive to a method, Java sends a copy of its
value. Changes to this value inside the method don’t affect the original variable.
• Reference types (objects): When you pass an object to a method, Java sends a
copy of the reference (or pointer) to that object.
• This means you can change the object’s contents, and those changes will show
outside the method. However, if you make the reference point to a new object, it
won’t affect the original reference outside the method.
INTRODUCTION TO INHERITANCE
WHAT IS INHERITANCE?
• allows one class to inherit the properties and behaviors (methods) of another class.
• This promotes code reusability and establishes a relationship between classes.
CLASSES IN INHERITANCE
• Parent Class: whose properties and methods are inherited by another class.
• Child Class: inherits the properties and methods of the base class.
TYPES OF INHERITANCE
• Single Inheritance
- a sub-class is derived from only one super class.
JY
SIMPLE INHERITANCE
// Parent class
System.out.println(“Tet”);
// Child Class
System.out.println(“ Woof”);
// Driver class
// Main function
dog.action();
dog.bark();
dog.action();
}
JY
• Multilevel Inheritance
- a class inherits from a superclass, which in turn is a subclass of another
class.
• For example, class A serves as a base class for the derived class B,
which in turn serves as a base class for the derived class C.
• Note that, a class cannot directly access the grandparent’s
members.
• Hierarchical Inheritance
- one class serves as a superclass (base class) for more than one subclass.
- This structure allows for logical grouping of related classes and the ability to
define common behavior in a single place (the superclass) while enabling
specific behavior in the subclasses.
JY
DEFINITIONS OF ENUMS
- You can define fields and a constructor for the enum to associate additional
data with each constant.
JY
WHAT IS AN ENUM?
- can also define enums with methods, static methods, and implementing
interfaces.
- We can also create enums with fields of different types.
- Values(): Returns an array of all the constants of the enum type in the order
they are declared.
- valueOf(String name): Returns the enum constant of the specified enum
type with the specified name.
- Ordinal(): Returns the ordinal of the enum constant (its position in the enum
declaration, starting from 0).
WHAT IS ASSOCIATION?
CHARACTERISTICS OF ASSOCIATION
TYPES OF ASSOCIATION
- Unidirectional Association: The idea that the class is aware and associated
with another class.
- Bidirectional Association: The idea is that the classes interact and are
aware of each other.
WHAT IS AGGREGATION?
CHARACTERISTICS OF AGGREGATION
- Weak Ownership: The “whole” can use the “part,” but it does not own it. The
part can exist outside of the whole.
- No Lifecycle Dependency: When the whole is destroyed, the parts can still
exist separately.
WHAT IS COMPOSITION?
CHARACTERISTICS OF COMPOSITION
JY
- Strong Ownership: The “whole” fully owns the “part,” and the “part” is
created and destroyed with the “whole.“
- Lifecycle Dependency: When the whole is destroyed, the part is also
destroyed.