[go: up one dir, main page]

0% found this document useful (0 votes)
6 views8 pages

oop-midterm-jy

Uploaded by

wuwadummy55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

oop-midterm-jy

Uploaded by

wuwadummy55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JY

STATIC MEMBERS, OBJECT PASSING, AND ESSENTIAL METHODS

WHAT IS STATIC KEYWORD?

• 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.

STATIC MEMBERS(STATIC FIELDS)

• 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

• can be called without creating an instance of the class.


• It belongs to the class rather than a specific object.
• A static method cannot directly access non-static (instance) fields or methods
because it doesn’t operate on a specific object.

STATIC BLOCKS

• used to initialize static variables when the class is loaded.


• executed only once, when the class is loaded into memory.

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.

PASSING AND RETURNING OBJECTS

• What is pass-by-value?
• What is pass-by-reference?
• What is the difference between object and a reference?

PASSING AND RETURNING OBJECTS


JY

• Java is strictly Passed-by-value. It means that when a variable is passed to a


method, Java passes a copy of the variable’s value to the method, not the variable
itself.
• Pass-by-reference, a method receives a direct reference to the original variable, not
a copy. This allows the method to modify the variable itself, not just its contents.
Java does not support true pass-by-reference.
• This applies to both primitive types and reference types in Java, but with some
differences in how each behaves due to how they store data:

PRIMITIVE TYPES VS REFERENCE TYPES

• 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.

PRIMITIVE TYPES VS REFERENCE TYPES

1 When we pass a primitive type to a method, it is passed by value.

2 But when we pass an object to a method, the situation changes dramatically.

3 Because objects are passed by what is effectively call-by-reference.

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

- It inherits the properties and behavior of a single-parent class.


- It is also called as simple inheritance.

Parent Class -> Child Class

SIMPLE INHERITANCE

This is the basic example of a simple inheritance in java.

// Parent class

public class Animal {

public void action ()

System.out.println(“Tet”);

// Child Class

public class Dog extends Animal {

public void back() {

System.out.println(“ Woof”);

// Driver class

public class Main {

// Main function

public static void main(String[] args)

Dog = new Dog();

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

ENUMERATED TYPES AND ESSENTIAL METHODS

WHAT ARE ENUMS?

- (enumerated types) provide a flexible way to define a collection of constants.


- They can be defined in various ways, including specifying fields, methods,
and constructors.

DEFINITIONS OF ENUMS

• BASIC ENUM DEFINITION


• ENUM WITH CONSTRUCTOR AND FIELDS
• ENUM WITH METHODS
• ENUM WITH STATIC METHODS

BASIC ENUM DEFINITION

- simplest form of an enum, where you only declare the constants.

ENUM WITH CONSTRUCTOR AND FIELDS

- 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.

BUILT IN METHODS IN ENUM

- 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).

ESSENTIAL METHODS TO NOTE

- The toString() Method provides a string representation of an object. This is


particularly useful for debugging and logging
- The equals() Method is used to compare two objects for logical equality. By
default, it compares references, so it must be overridden for meaningful
comparison.

ASSOCIATION, AGGREGATION, AND COMPOSITION


JY

- Association, Aggregation, and Composition are ways to establish


relationships between classes and objects.
- Each of these varies in strength and dependency.
- These relationships allow you to model complex systems by defining how
objects work together.

WHAT IS ASSOCIATION?

- indicates the relationship between two independent classes.


- It is a general relationship between the two where each class has a reference
to the other.
- They don’t depend on each other for their lifecycle.

CHARACTERISTICS OF ASSOCIATION

- Weak Relationship: Neither object “owns” the other.


- No Lifecycle Dependency: Each object can exist independently of the other.

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?

- Aggregation is a specific form of association.


- The ‘the whole’ contains a property of another class, which is ‘the part’.
- The lifecycle of ‘the part’ does not depend on ‘the whole’.

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?

- Composition is a stronger form of aggregation.


- The ownership and lifecycle depends on ‘the whole’.
- If ‘the whole’ gets destroyed, the parts no longer exist.

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.

You might also like