All Flashcards
What are the differences between static and dynamic method dispatch?
Static dispatch: Determined at compile time, faster. Dynamic dispatch: Determined at runtime, more flexible.
What are the differences between method overloading and method overriding?
Overloading: Multiple methods with the same name but different parameters in the same class. Overriding: A method in a subclass with the same name and parameters as a method in its superclass.
What is the difference between inheritance and polymorphism?
Inheritance: Mechanism for code reuse and creating 'is-a' relationships. Polymorphism: Ability of an object to take on many forms, often achieved through inheritance.
What is the difference between an abstract class and an interface?
Abstract Class: Can have concrete methods and instance variables. Interface: Can only have abstract methods and constants (until Java 8).
What are the differences between early and late binding?
Early Binding: Method call is resolved at compile time. Late Binding: Method call is resolved at runtime.
What are the differences between compile-time and runtime errors?
Compile-time errors: Detected during compilation. Runtime errors: Occur during program execution.
What is the difference between a class and an object?
Class: Blueprint for creating objects. Object: Instance of a class.
What is the difference between a local variable and an instance variable?
Local variable: Declared inside a method. Instance variable: Declared inside a class but outside any method.
What are the differences between procedural and object-oriented programming?
Procedural: Focuses on procedures/functions. Object-oriented: Focuses on objects and their interactions.
What is the difference between composition and inheritance?
Composition: 'Has-a' relationship, objects contain other objects. Inheritance: 'Is-a' relationship, classes inherit from other classes.
What is the significance of static type in method calling?
Determines which method is called for static methods.
What is the significance of dynamic type in method calling?
Determines which method is called for non-static methods.
How does inheritance relate to polymorphism?
Inheritance allows subclasses to be treated as instances of their superclass, enabling polymorphism.
Explain the concept of method overriding in polymorphism.
Subclasses provide specific implementations of methods already defined in their superclass, allowing different behavior for the same method call.
Why is understanding static and dynamic types important for polymorphism?
It clarifies which version of a method will be executed at runtime, especially in inheritance hierarchies.
Explain the difference between compile-time and runtime polymorphism.
Compile-time polymorphism (static binding) is resolved during compilation, while runtime polymorphism (dynamic binding) is resolved during execution.
What is the role of the @Override
annotation?
It indicates that a method is overriding a method in the superclass and helps prevent errors.
What is the Liskov Substitution Principle?
Subtypes must be substitutable for their base types without altering the correctness of the program.
How does polymorphism increase code flexibility?
Allows writing code that can work with objects of different classes in a uniform way.
Explain the concept of 'is-a' relationship in inheritance.
A subclass 'is-a' type of its superclass, meaning it inherits its properties and behaviors.
What does the following code output?
java
public class A {
public static void callOne() {
System.out.println("1");
}
}
public class B extends A {
public static void callOne() {
System.out.println("3");
}
}
public class Main {
public static void main(String[] args) {
A a = new B();
a.callOne();
}
}
1
What does the following code output?
java
public class A {
public void callTwo() {
System.out.println("2");
}
}
public class B extends A {
@Override
public void callTwo() {
System.out.println("4");
}
}
public class Main {
public static void main(String[] args) {
A a = new B();
a.callTwo();
}
}
4
What does the following code output?
java
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.makeSound();
}
}
Woof!
What does the following code output?
java
class Vehicle {
public void start() {
System.out.println("Vehicle starting");
}
}
class Car extends Vehicle {
@Override
public void start() {
System.out.println("Car starting");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myVehicle = new Vehicle();
Vehicle myCar = new Car();
myVehicle.start();
myCar.start();
}
}
Vehicle starting Car starting
What does the following code output?
java
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Shape();
Shape shape2 = new Circle();
shape1.draw();
shape2.draw();
}
}
Drawing a shape Drawing a circle
What does the following code output?
java
class Employee {
public void displaySalary() {
System.out.println("Employee salary");
}
}
class Manager extends Employee {
@Override
public void displaySalary() {
System.out.println("Manager salary");
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Manager();
emp.displaySalary();
}
}
Manager salary
Identify the error in the following code:
java
class Parent {
public void show() {
System.out.println("Parent's show()");
}
}
class Child extends Parent {
public void display() {
System.out.println("Child's display()");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Parent();
obj.display();
}
}
The Parent class does not have a display()
method. It is defined only in the Child class.
What does the following code output?
java
class Base {
public void printMessage() {
System.out.println("Base class message");
}
}
class Derived extends Base {
@Override
public void printMessage() {
System.out.println("Derived class message");
}
}
public class Main {
public static void main(String[] args) {
Base obj = new Derived();
obj.printMessage();
}
}
Derived class message
What does the following code output?
java
class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
class Herbivore extends Animal {
@Override
public void eat() {
System.out.println("Herbivore is eating plants");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Animal();
Animal animal2 = new Herbivore();
animal1.eat();
animal2.eat();
}
}
Animal is eating Herbivore is eating plants
What does the following code output?
java
class Device {
public void powerOn() {
System.out.println("Device is powering on");
}
}
class Phone extends Device {
@Override
public void powerOn() {
System.out.println("Phone is powering on");
}
}
public class Main {
public static void main(String[] args) {
Device myDevice = new Phone();
myDevice.powerOn();
}
}
Phone is powering on