All Flashcards
What are the differences between a constructor and a method?
Constructor: Initializes objects, has the same name as the class, no return type. | Method: Performs actions, can have any name, has a return type (or void).
What are the differences between public and private access modifiers?
Public: Accessible from anywhere. | Private: Accessible only within the class.
What are the differences between instance variables and local variables?
Instance variables: Declared within a class, outside any method, accessible by all methods in the class. | Local variables: Declared within a method, accessible only within that method.
What are the differences between getter and setter methods?
Getter: Accesses the value of an instance variable. | Setter: Modifies the value of an instance variable.
What are the differences between a class and an object?
Class: A blueprint or template. | Object: An instance of a class.
What are the differences between a default constructor and a parameterized constructor?
Default constructor: No parameters, provides default values. | Parameterized constructor: Takes parameters, allows custom initialization.
What are the differences between a method with a return type and a method with a void return type?
Return type: Returns a value of the specified type. | Void: Does not return any value.
What are the differences between a class header and a class body?
Class header: Specifies the class name and visibility. | Class body: Contains variable declarations and methods.
What are the differences between creating a new object and using an existing object?
Creating a new object: Allocates new memory and initializes the object. | Using an existing object: Operates on an object that has already been created.
What are the differences between calling a getter method and directly accessing a public instance variable?
Getter method: Provides controlled access and allows additional logic. | Public instance variable: Direct access, no control or additional logic.
How are classes used in designing a banking system?
Classes can represent entities like accounts, customers, and transactions, each with specific attributes and behaviors.
How are classes used in developing a game?
Classes can represent game objects like players, enemies, and items, each with their own properties and actions.
How are classes used in creating a graphical user interface (GUI)?
Classes can represent GUI components like buttons, text fields, and windows, each with specific properties and event handlers.
How are classes used in managing data in a database?
Classes can represent database tables and records, each with specific fields and methods for data manipulation.
How is encapsulation applied in real-world scenarios?
Protecting sensitive information in a user profile by making attributes private and providing controlled access through methods.
How are classes used in designing a library management system?
Classes can represent books, members, and librarians, each with specific attributes and behaviors related to borrowing and returning books.
How are classes used in developing an e-commerce platform?
Classes can represent products, customers, orders, and shopping carts, each with specific attributes and methods for managing products and processing orders.
How are classes used in creating a social media application?
Classes can represent users, posts, comments, and messages, each with specific attributes and methods for creating and sharing content.
How are classes used in managing student records in a school?
Classes can represent students, courses, teachers, and grades, each with specific attributes and methods for managing student information and academic performance.
How are classes used in designing a flight booking system?
Classes can represent flights, passengers, bookings, and airports, each with specific attributes and methods for managing flight schedules and booking reservations.
What are the general steps to writing a class in Java?
- Define the class header. 2. Declare instance variables. 3. Define constructors. 4. Define methods.
What are the steps to create a new object from a class?
- Declare a variable of the class type. 2. Use the
newkeyword followed by the class constructor to create the object. 3. Assign the newly created object to the variable.
What are the steps to access an instance variable of an object?
- Ensure the instance variable has appropriate access (e.g., public or via getter method). 2. Use the dot operator (
.) to access the variable (e.g.,objectName.variableNameorobjectName.getVariableName()).
What are the steps to call a method on an object?
- Use the dot operator (
.) followed by the method name and parentheses (e.g.,objectName.methodName()). 2. Pass any required arguments inside the parentheses.
What are the steps to define a constructor?
- Use the
publickeyword. 2. Use the same name as the class. 3. Define parameters (if any) inside parentheses. 4. Write the constructor body to initialize instance variables.
What are the steps to define a method?
- Specify the access modifier (e.g.,
public,private). 2. Specify the return type (orvoidif no return value). 3. Give the method a name. 4. Define parameters (if any) inside parentheses. 5. Write the method body.
What are the steps to implement encapsulation?
- Declare instance variables as
private. 2. Providepublicgetter methods to access the values of the instance variables. 3. Providepublicsetter methods to modify the values of the instance variables (if needed).
What are the steps to overload a constructor?
- Create multiple constructors with the same name as the class. 2. Ensure each constructor has a different parameter list (different number or types of parameters).
What are the steps to determine instance variables?
- Identify the characteristics or attributes of the class. 2. For each characteristic, determine an appropriate data type. 3. Declare the instance variables with the chosen data types.
What are the steps to determine methods?
- Identify the actions or behaviors that objects of the class should perform. 2. Determine the inputs (parameters) required for each action. 3. Determine the output (return type) of each action. 4. Implement the methods to perform the actions.