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.
What level of access to the properties and methods of the enclosing class does a static nested class have?
A static nested class has access to the public and protected properties and methods of the enclosing class.
Under what conditions can a local class access variables in the enclosing scope?
A local class can access variables in the enclosing scope if they are effectively final or declared as final.
Write a constructor for a Car class that takes action and reaction parameters.
public class Car {
private String action;
private String reaction;
public Car(String action, String reaction) { this.action = action; this.reaction = reaction; } }
Write the getters and setters for your Dog class.
public class Dog {
private String name;
private int age;
public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Give an example of an interface.
public interface Drawable {
void draw();
}
Write an interface for airplane operations (turn left, climb, and so on). Create a class called AirBus787 that implements your interface.
public interface AirplaneOperations {
void turnLeft();
void turnRight();
void climb();
void descend();
}
Can you instantiate an interface?
Instantiating an Interface directly is not possible in Java. Interfaces are meant to be implemented by classes.
Compare and contrast the keywords extends and implements.
“Extends” is used to establish an inheritance relationship between classes. It allows a subclass to inherit the properties and behaviors of a superclass.
“Implements” is used to indicate that a class is implementing one or more interfaces. It defines the contract that the class must adhere to by implementing the methods specified in the interface.
When do you use the extends keyword
“Extends” is used to establish an inheritance relationship between classes. It allows a subclass to inherit the properties and behaviours of a superclass.
When do you use the implements keyword?
“Implements” is used to indicate that a class is implementing one or more interfaces. It defines the contract that the class must adhere to by implementing the methods specified in the interface.
If you have no access modifiers what is the default on your class?
If no access modifier is specified for a class, it has the default access level, which means it can be accessed within the same package but not from outside the package.
What is the base class of all classes?
The base class of all classes in Java is the Object class. All other classes in Java are either directly or indirectly derived from the Object class.
What is a List?
List is an interface that represents an ordered collection of elements. It allows duplicate elements and provides methods for accessing, modifying, and iterating over the elements.
What is an ArrayList?
ArrayList is a class that implements the List interface. It provides a dynamic array implementation of the List interface, allowing elements to be added, removed, and accessed by their index. ArrayList automatically adjusts its size as elements are added or removed.
What is a Set?
Set is an interface that represents a collection of unique elements with no defined ordering. It does not allow duplicate elements.
What is a HashSet?
HashSet is a class that implements the Set interface. It uses a hash table data structure to store elements, providing constant-time performance for basic operations like add, remove, and contains. HashSet does not guarantee the order of elements.
What is a Map?
Map is an interface that represents a mapping between keys and values. Each key in a Map is associated with a unique value, and keys are unique within a Map.
What is a HashMap?
HashMap is a class that implements the Map interface. It uses a hash table data structure to store key-value pairs, providing fast retrieval and insertion of elements. HashMap does not guarantee the order of keys or values.