Chapter 4 - OOP in General Flashcards
OOP meaning
Object-Oriented Programming (OOP) is a programming paradigm that emphasizes the organization of code and data into self-contained units called objects. Java is an object-oriented programming language, and it is designed to support OOP principles. OOP promotes the use of objects to structure and manage code, making it easier to design, develop, and maintain software.
Class and its features
A class is a blueprint or template for creating objects. It defines the properties (data members or fields) and behaviors (methods) that objects of that class will possess.
Attributes (Properties):
Methods (Functions):Constructor: Access Modifiers:
Object and its features
Objects are instances of classes, and they encapsulate both data (attributes or properties) and behaviors (methods or functions) that are associated with the class they belong to. Objects are used to model and interact with entities in a software system, making OOP a powerful and intuitive way to design and structure code.
Instance of a Class: An object is created based on a class, which serves as a blueprint or template for defining the object’s structure and behavior. Multiple objects of the same class can be created, each with its own set of attributes and state.
Attributes (Properties): Objects have attributes that represent their state or characteristics. These attributes are data members or variables within the class and define what data an object can store. For example, a Person object may have attributes like name, age, and address.
Methods (Behaviors): Objects have methods, which are functions defined within the class. Methods specify the actions or behaviors that objects can perform. Methods can operate on the object’s attributes and implement specific functionalities. For instance, a Car object may have methods like startEngine(), accelerate(), and brake().
Identity: Each object has a unique identity or reference that distinguishes it from other objects. This identity is often represented by a memory address or a reference in the program. This identity is usually represented by a memory address or a reference.
State: The state of an object refers to the values of its attributes at a particular point in time. It reflects the object’s current data.
Behavior: An object’s behavior is defined by its methods and describes what it can do or how it can respond to messages or requests from other objects.
Object type
the term “object type” refers to the specific class or data type of an object.
write class
[access_modifier] class ClassName {}
Class access modifiers
Public
Default
Private
Protected
Public access modifier of class
The class, field, method, or constructor declared as public can be accessed from anywhere in your code, both within and outside the package where the class is defined.
It has the widest scope and the least restrictive access level.
Default access Modifier of class
A class with default access is accessible to other classes and components within the same package, but it’s not visible to classes outside that package.
Private access modifier of class
A class declared as private can only be accessed within the same class. It is the most restrictive access modifier for classes and is rarely used for classes themselves.
Protected access modifier of class
Members declared as protected are accessible within the class they are defined in and in its subclasses. They are not accessible from outside the class hierarchy.
What is constructor?
A constructor in Java is a special type of method that is used to initialize objects. It is called automatically when an object of a class is created. The primary purpose of a constructor is to set up the initial state of an object, allocate memory for the object, and perform any necessary initialization tasks.
What can you tell me about name and return type of constructor?
A constructor must have the same name as the class it belongs to. It’s called automatically when an object is created using the new keyword.
Constructors do not have a return type, not even void. They are implicitly assumed to return an instance of the class.
Constructor overloading? why we need it?
You can define multiple constructors in a class with different parameter lists. This is known as constructor overloading.
it allowss Initialization Flexibility: Different situations may require objects to be initialized with different sets of initial values. By providing multiple constructors, you give users of your class the flexibility to create objects with the initialization data that best suits their needs.
Default constructor and why is better custom one?
A default constructor in Java is a constructor that is automatically provided by the Java compiler when a class doesn’t explicitly define any constructors. The default constructor has no arguments and typically initializes instance variables to default values (e.g., numeric fields to 0, object references to null, etc.). Its purpose is to ensure that objects of the class can still be created without requiring any special initialization parameters.
Custom constructors allow you to provide flexibility when creating objects. You can offer multiple constructor options with different parameter sets, enabling users of your class to choose the one that best suits their needs. This promotes code reusability and makes your class more versatile.
Constructors can perform additional initialization logic beyond setting fields. For example, they can acquire resources, open files, connect to databases, or perform other setup tasks. The default constructor may not be suitable for these scenarios, so custom constructors are needed.
In Java, parametrized constructors and their advantages?
Parametrized constructors allow you to initialize object instances with different values, providing flexibility in how objects are created. This flexibility is especially useful when you need to create objects with various initial states.
Parametrized constructors accept one or more parameters (arguments) that are used to initialize the instance variables of the object. These parameters are specified in the constructor’s parameter list.
Constructor chaining and why?
Constructor chaining in Java is a technique that allows you to call one constructor from another within the same class or from a subclass constructor.
for example we want to create a car object with 5 different values (year, model, etc) you ensure that every Car object is initialized with some reasonable default values, even if the user does not specify them. This guarantees that you don’t have uninitialized or partially initialized objects.
Is constructor overriding allowed?
constructors cannot be overridden in the same way that methods can be overridden. Overriding a method means providing a different implementation of the method in a subclass, while maintaining the same method signature (name, return type, and parameters). However, constructors do not have a return type and are not inherited by subclasses.
Instead, when you create a subclass, you can invoke the constructor of the superclass using the super keyword and provide additional functionality specific to the subclass.