OOP Flashcards
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.
What is a “Subclass” or “Child Class”?
A subclass is a class that inherits properties and behaviors from another class, also known as a derived, extended, or child class.
What is a “Superclass” or “Parent Class”?
A superclass is the class from which a subclass inherits features, also known as a base or parent class.
Define “Reusability” in the context of inheritance.
Reusability allows a new class to reuse fields and methods from an existing class, making code more efficient and reducing duplication.
What is the syntax for defining inheritance in Java?
class SubclassName extends SuperclassName { // methods and fields }
What does the extends
keyword indicate in Java?
The extends
keyword indicates that a class is being derived from an existing class, increasing its functionality.
In the example Programmer
and Employee
classes, what is the relationship between them?
Programmer is a subclass of Employee, meaning Programmer IS-A type of Employee.
Provide a simple example of single inheritance in Java.
class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } }
What is single inheritance in Java?
Single inheritance is when a class inherits from one superclass only.
What is multilevel inheritance in Java?
Multilevel inheritance is when a class is derived from a class that is also derived from another class, creating a multi-level chain.
Provide a simple example of multilevel inheritance in Java.
class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class BabyDog extends Dog { void weep() { System.out.println("weeping..."); } }
What is hierarchical inheritance in Java?
Hierarchical inheritance is when multiple classes inherit from the same superclass.
Q: Provide a simple example of hierarchical inheritance in Java.
class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Cat extends Animal { void meow() { System.out.println("meowing..."); } }
Why is multiple inheritance not supported in Java?
Multiple inheritance is not supported to reduce complexity and avoid ambiguity if two parent classes have methods with the same name.
What error occurs in Java when trying to inherit from two classes?
Java produces a compile-time error if a class tries to inherit from two classes to prevent ambiguity.
How does Java achieve multiple inheritance?
Java supports multiple inheritance through interfaces, not class inheritance.
What is polymorphism in Java?
Polymorphism is the ability of an object to take on many froms, commonly seen when a parent class reference is used to refer to a child class object.
How is an objec in Java considered polymorphic?
An object is polymorphic if it can pass more than one IS-A test (ots own type and Object class type)
IS-A in this context is like: a DOG IS-A Animal (if dog extends animal)
What is method overloading?
Method overloading occurs when a class has multiple methods with the same name but different parameters, improving readability and flexibility.
Why is method overloading useful?
Method overloading increases program readability, especially for similar operations that take different types or numbers of arguments.
What are two ways to overload a method in Java?
- By changing the number of arguments.
- By changing the data type of arguments.
Give an example of method overloading by changing the number of arguments.
static int add(int a, int b) { return a + b; } static int add(int a, int b, int c) { return a + b + c; }
Give an example of method overloading by changing the data type of arguments.
static int add(int a, int b) { return a + b; } static double add(double a, double b) { return a + b; }
Can method overloading be done by changing only the return type?
No, method overloading cannot be achieved by changing the return type alone. Parameters must differ.
Can you overload the main() method in Java?
Yes, you can overload the main() method with different parameter lists, but only main(String[] args) is called by the JVM.
What is method overriding in Java?
Method overriding is when a subclass provides a specific implementation of a method that is already defined in its parent class.
Why is method overriding used?
Method overriding is used to provide specific implementation of a method from a superclass and to achieve runtime polymorphism.
Everything is the same, but the method logic is different. names params
What are the rules for method overriding?
- Same method name as in the parent class.
- Same parameter list as in the parent class.
- An IS-A (inheritance) relationship must exist.
Give an example of method overriding.
class Parent { void show() { System.out.println("Parent method"); } } class Child extends Parent { void show() { System.out.println("Child method"); } }
Can a static method be overridden?
No, static methods cannot be overridden because they belong to the class, not an instance of the class.
What is the main difference between method overloading and method overriding?
Overloading is compile-time polymorphism (same method name, different parameters within the same class). Overriding is runtime polymorphism (same method signature in parent and child classes with an IS-A relationship).
Java decides which method to use either at compile-time or run-time.
How does method overloading affect the readability of a program?
Method overloading enhances readability by allowing similar operations to be performed with methods of the same name but different arguments.
How does method overriding provide flexibility in Java?
Method overriding allows subclasses to provide specific implementations of methods, enabling different behavior in child classes while sharing a common interface.
In method overloading, can the return type differ?
Yes, in method overloading, the return type can differ, but it doesn’t influence overloading itself.
In method overriding, can the return type differ?
In method overriding, the return type must be the same or covariant.
Describe a real-world example scenario for method overriding.
Consider a Bank
class with a getRateOfInterest()
method. Different bank subclasses (SBI, ICICI, AXIS
) override this method to provide specific interest rates.
What is encapsulation in Java?
Encapsulation is the process of wrapping code and data together into a single unit, like a capsule. It involves making class data members private and using getter and setter methods to access and modify them.
What are the advantages of encapsulation?
Encapsulation allows for data hiding, control over data, and easy unit testing. It can make a class read-only or write-only by providing only getter or setter methods.
What does the following code demonstrate?
public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
This code is an example of encapsulation, where the Student
class has a private field name with getter and setter methods.
What is a read-only class in Java?
A read-only class has only getter methods, allowing data to be accessed but not modified. Example: A class with a private field and only a getter method.
What is a write-only class in Java?
A write-only class has only setter methods, allowing data to be modified but not accessed. Example: A class with a private field and only a setter method.
What are the types of access modifiers in Java?
The four types of access modifiers are private, default, protected, and public. They define the accessibility of classes, methods, and fields.
What is a rule for access modifiers in method overriding?
When overriding a method, the overridden method in the subclass must not be more restrictive than the method in the superclass.
What does the private access modifier do in Java?
The private access modifier restricts access to the member (fields, methods, constructors) within the same class only. It is the most restrictive access level.
What does the default access modifier do in Java?
The default access modifier (no keyword) allows access to the member within the same package. It is more restrictive than protected and public but less restrictive than private.
What does the protected access modifier do in Java?
The protected access modifier allows access to the member within the same package and by subclasses in other packages. It provides more accessibility than default but less than public.
What does the public access modifier do in Java?
The public access modifier allows access to the member from any other class in any package. It is the least restrictive access level.
What is abstraction in programming?
Abstraction is the process of hiding implementation details and showing only the functionality to the user. It helps focus on what an object does rather than how it does it.
Provide an example of abstraction.
Sending an SMS: You type the message and send it without knowing the internal processing of message delivery.
What is generalization in object-oriented programming?
Generalization involves extracting shared characteristics from multiple classes and combining them into a generalized superclass, sharing attributes, associations, or methods.
Give an example of generalization.
Classes Piece of Luggage and Piece of Cargo share attributes that are combined into a superclass called Freight. Shared properties like Identification and Weight are placed in Freight.
What is specialization in object-oriented programming?
Specialization is creating subclasses from an existing class when certain attributes or methods apply only to some objects. Subclasses inherit properties from the superclass.
Provide an example of specialization.
In the Freight class, Piece of Cargo may have a unique property like Degree of Hazardousness, making it a specialized subclass inheriting common properties from the Freight superclass.
How can abstraction be achieved in Java?
Abstraction can be achieved using abstract classes (0 to 100%) and interfaces (100%).
What is an abstract class in Java?
An abstract class is declared with the abstract keyword and can have both abstract and non-abstract methods. It cannot be instantiated and must be extended by subclasses.
What is an abstract method in Java?
An abstract method is declared without implementation and must be implemented by subclasses that extend the abstract class.
Describe a sample exercise to practice abstraction.
Create an abstract class Bank
with an abstract method getRateOfInterest()
. Create subclasses SBI
and PNB
, overriding this method to return different interest rates, and test in a TestBank
class.