Java Classes and Objects Study List Flashcards
Describe inheritance
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviours from another class. It enables code reuse and the creation of hierarchical relationships between classes.
The subclass can access and extend the members (fields and methods) of the superclass.
Describe polymorphism
Polymorphism refers to the ability of an object to take on many forms. In Java, polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables code to work with objects in a more general way, increasing flexibility and modularity. Polymorphism is achieved through method overriding and method overloading.
Subclasses can be treated as if they are superclasses.
Describe encapsulation
Encapsulation is a principle of OOP that combines data and methods into a single unit called a class. It promotes the idea of hiding internal implementation details and providing a public interface to interact with the class. Encapsulation helps in achieving data protection, abstraction, and code maintainability by preventing direct access to data and enforcing access through methods (getters and setters).
Describe abstraction
Define the essential characteristics of an object while hiding the implementation details.
Abstraction can be achieved through abstract classes and interfaces in Java.
Describe the single responsibility principle
The Single Responsibility Principle (SRP) states that a class should have only one reason to change. It suggests that a class should have a single responsibility or purpose and should encapsulate only one aspect of functionality. By adhering to SRP, classes become more focused, maintainable, and reusable. It promotes better code organization and separation of concerns.
What is a class?
A class is a blueprint or template that defines the structure, behavior, and initial state of objects. It serves as a blueprint for creating multiple instances of objects.
What is an object?
An object is a specific instance of a class. It represents a real-world entity and has its own unique state and behaviour.
What is an accessor/getter?
An accessor or getter is a method in a class that provides access to the value of a private or protected instance variable. It allows other classes to retrieve the value of the variable without directly accessing it. Getters typically follow the naming convention getVariableName(), where “VariableName” is the name of the variable being accessed.
What is a mutator/setter?
A mutator or setter is a method in a class that allows the modification or assignment of a value to a private or protected instance variable. It provides a way to update the value of a variable while encapsulating the logic or constraints associated with the assignment. Setters typically follow the naming convention setVariableName(), where “VariableName” is the name of the variable being modified.
What is a constructor?
A constructor is a special method in a class that is used to initialize the object of that class. It is called automatically when an object is created using the “new” keyword. Constructors have the same name as the class and can have parameters to accept initial values for the object’s instance variables. Constructors are used to set up the initial state of an object.
What is shadowing? (not best practice).
Shadowing occurs when a variable in a nested scope (such as a method or block) has the same name as a variable in the enclosing scope. It leads to the overshadowing or hiding of the variable in the outer scope, making it inaccessible within the inner scope. Shadowing can cause confusion and should be avoided for better code readability.
What is a static method
What is scope of public access modifier
Public: Accessible from anywhere, both within and outside the class.
What is scope of private access modifier
Private: Accessible only within the same class. Not visible to other classes.
What is the scope of a class without an access modifier
Default (no explicit modifier): Accessible within the same package but not outside the package.
What is scope of protected access modifier
Protected: Accessible within the same class, subclasses, and other classes in the same package.
What is a static object
In Java, the terms “static object” and “non-static object” are not commonly used. However, we can discuss static members and non-static members within a class.
What is a static method
Static methods and variables belong to the class itself rather than individual instances of the class. They can be accessed without creating an object of the class.
What is a non-static method
Non-static methods and variables are associated with specific instances (objects) of the class and require an object to be accessed.
What does the abstract keyword mean?
The abstract keyword is used to declare abstract classes and methods in Java. An abstract class cannot be instantiated and serves as a blueprint for creating concrete subclasses.
How is the abstract keyword used in conjunction with classes?
Abstract methods are declared without a body and must be implemented in the subclasses. Abstract classes provide common behavior and attributes that can be inherited by multiple subclasses.
How is the collection class used?
The Collection class is a built-in class in Java that provides a framework for working with groups of objects. It is a high-level interface that defines common methods and behaviors for collections. Collection classes, such as ArrayList, HashSet, and HashMap, implement the Collection interface and provide specific implementations for storing and manipulating groups of objects.
What does it mean if a class is cohesive?
A cohesive class is a class that has a single, well-defined responsibility or purpose. It focuses on a specific area of functionality and encapsulates related data and methods. A cohesive class is highly focused and avoids unnecessary dependencies on other classes or responsibilities.
What is a local class?
A local class is a class defined within a block, such as a method or a constructor, within another class. It is only accessible within the block where it is defined and is often used for implementing callbacks or specific functionality within a limited scope.