OOP Flashcards
(81 cards)
What is an Object?
An object is an entity with state and behavior, such as a chair or a dog. It is an instance of a class, has an address, occupies memory, and can communicate with other objects.
How is a dog an example of an object?
A dog has states (color, name, breed) and behaviors (wagging tail, barking, eating)
What is a class?
A class is a template used to create objects, defining their data types and methods. It acts as a blueprint for objects and does not consume memory space.
What are the advantages of OOPs over Procedure Oriented Programming?
1.Makes development and maintenance easier, espcially as project size grows.
2.Provides data hiding, while in procedure-oriented programming global data can be accessed from anywhere.
What is a constructor in Java?
A constructor in Java is a special method used to initialize objects. It is called when an object is created and has the same name as the class. Constructors do not have a return type and can be used to set initial values for object attributes.
What is a no-argument constructor? Give an example
A constructor with no parameters.
public class MyClass { // No-argument constructor public MyClass() { System.out.println("No-argument constructor called"); } public static void main(String[] args) { // Create an instance of MyClass MyClass obj = new MyClass(); // This will call the no-argument constructor } }
Show an example of constructor overloading
public class Car { private String brand; private String color; // Default constructor public Car() { this.brand = "Unknown"; this.color = "Black"; } // Constructor with brand only public Car(String brand) { this.brand = brand; this.color = "Black"; } // Constructor with both parameters public Car(String brand, String color) { this.brand = brand; this.color = color; } }
What are the three rules for creating a constructor?
- Constructor name must be the same as the class name
- Constructor must have no explicit return type
- Java constructor cannot be abstract, static, final, or synchronized
When are constructors called?
- Called when an object is created
- Compiler ensures constructors for all subobjects ( member and inherited objects) are called
- Default/no-parameter constructors are called automatically
- Parameterized constructors are called using initializer list (passing the values in parenthenses)
What is a constructor in Java?
A constructor is a block of code similar to a method, used to initialize and object when an instance is created. It allocates memory for the object.
Why is a constructor called a “constructor” ?
It is called a constructor because it “constructs” or sets initial values at the time of object creation.
What are the three rules to remember when creating a constructor in Java?
- The constructor name must match the class name
- It must have no explicit return type
- It cannot be abstract, static , final , or synchronized
What are the two main types of constructors in Java?
Default constructor and Parameterized Constructor
What is a default constructor?
A default constructor is a no-args constructor that Java automatically provides if no other constructor is defined in the class. It includes the default call to super()
.
What is the behavior of a default constructor in Java?
If there is no constructor in the class, the compiler adds a default constructor.
Provide an example of a no-argument constructor in Java
public Bicycle() { gear = 1; cadence = 10; speed = 0; }
What is a parameterized constructor in Java?
A parameterized constructor in Java is a constructor with specific parameters, allowing distinct objects to have different values.
Provide an example of a parameterized constructor in Java
class Car { private String model; private int year; // Parameterized constructor public Car(String model, int year) { this.model = model; this.year = year; } } // Usage: Car myCar = new Car("Civic", 2024);
What are the differences between a constructor and a method in Java?
- Constructor: Initializes the state of an object, no return type, invoked implicitly, provided by the compiler if absent, name matches class name.
- Method: Exposes the behavior of an object, has a return type, invoked explicitly, not automatically provided, name may differ from the class name.
What is inheritance in Java?
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object, allowing code reuse and creating a parent-child relationship.
What is the purpose of inheritance in Java?
Inheritance allows new classes to be created based on existing classes, enabling reuse of methods and fields from the parent class and adding new methods and fields in the child class.
What kind of relationship does inheritance represent in Java?
Inheritance represents the IS-A relationship, also known as a parent-child relationship.
What are the main advantages of inheritance?
Method Overriding (enabling runtime polymorphism), 2) Code Reusability.
Define “Class” in the context of inheritance.
A class is a group of objects with common properties; it acts as a blueprint from which objects are created.