Here are some last-minute revision notes on object-oriented programming (OOP). These questions cover essential OOP concepts and can help you prepare for job interviews.
Answer: Object-oriented programming is a programming paradigm based on the concept of "objects". It focuses on data and functions bundled together in the form of objects.
Answer:
Class: A class is the blueprint from which individual objects are created. It defines a datatype by bundling data and methods that operate on the data.
Object: An object is an instance of a class. When a class is defined, no memory is allocated, but memory is allocated when an object of that class is instantiated.
class Person {
// Data Members
String name;
// Member Function
void printName() {
System.out.println("Person's name is: " + name);
}
}
public class Main {
public static void main(String[] args) {
// Create an object of class Person
Person person = new Person();
// Access data member
person.name = "Thanos";
// Call member function
person.printName();
}
}
Output
Person's name is: Thanos
Answer:
-
Constructors are special methods invoked when an object is created. They initialize the object.
-
Constructors have the same name as the class and may be defined inside or outside the class definition.
There are 3 types of constructors:
- Default Constructor: Does not take any arguments.
- Parameterized Constructor: Accepts parameters.
- Copy Constructor: Initializes an object using another object of the same class.
- Constructors have no return type.
- They are automatically called when an object is created.
- If no constructor is defined, a default constructor is provided by the compiler.
- Constructors can be overloaded but cannot be virtual.
class Student {
String name;
int age;
boolean gender;
// Default Constructor
Student() {
System.out.println("Default Constructor");
}
// Parameterized Constructor
Student(String name, int age, boolean gender) {
this.name = name;
this.age = age;
this.gender = gender;
System.out.println("Parameterized Constructor");
}
// Copy Constructor
Student(Student student) {
this.name = student.name;
this.age = student.age;
this.gender = student.gender;
System.out.println("Copy Constructor");
}
void printInfo() {
System.out.println("Name = " + name);
System.out.println("Age = " + age);
System.out.println("Gender = " + gender);
}
}
public class Main {
public static void main(String[] args) {
// Default Constructor
Student s1 = new Student();
s1.printInfo();
// Parameterized Constructor
Student s2 = new Student("Sumeet", 20, true);
s2.printInfo();
// Copy Constructor
Student s3 = new Student(s2);
s3.printInfo();
}
}
Output
Default Constructor
Name = null
Age = 0
Gender = false
Parameterized Constructor
Name = Sumeet
Age = 20
Gender = true
Copy Constructor
Name = Sumeet
Age = 20
Gender = true
Answer:
- In Java, destructors do not exist explicitly like in C++. Java has automatic garbage collection to handle object destruction.
Answer: The four main pillars of OOP are:
Answer: Inheritance allows a new class to inherit properties and methods from an existing class.
Real Life Example
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Java avoids multiple inheritance of classes to prevent conflicts like the diamond problem, but interfaces provide flexibility in achieving similar outcomes.
- Single Inheritance: When a subclass inherits from one base class.
class A {
void displayA() {
System.out.println("Base Class");
}
}
class B extends A {
void displayB() {
System.out.println("Inherited from Class A");
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
obj.displayA();
obj.displayB();
}
}
Output
Base Class
Inherited from Class A
Answer:
Encapsulation is the process of wrapping code and data together into a single unit, for example, a class. It restricts access to data to ensure integrity.
Real Life Example
class EncapsulationExample {
private int data;
public void setData(int data) {
this.data = data;
}
public int getData() {
return data;
}
}
public class Main {
public static void main(String[] args) {
EncapsulationExample obj = new EncapsulationExample();
obj.setData(10);
System.out.println("Data = " + obj.getData());
}
}
Output
Data = 10
Answer:
Abstraction focuses on hiding the internal details and showing only essential information.
Real Life Example
You don’t need to know how the coffee machine works to press the button and get coffee.
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("Barks");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
}
}
Output
Barks
Answer:
Polymorphism allows one interface to be used for multiple implementations.
Real Life Example
- Method Overloading (Compile-time Polymorphism):
class OverloadExample {
void display() {
System.out.println("No arguments");
}
void display(int a) {
System.out.println("One argument: " + a);
}
}
public class Main {
public static void main(String[] args) {
OverloadExample obj = new OverloadExample();
obj.display();
obj.display(10);
}
}
Output
No arguments
One argument: 10
- Method Overriding (Runtime Polymorphism):
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}
Output
Dog barks
Answer:
An abstract class cannot be instantiated and may contain abstract methods that must be implemented by subclasses.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
Answer:
A method declared as abstract without implementation in a class, forcing subclasses to implement it.
12: Friend Class & Friend Function (Java does not have a direct equivalent to C++ Friend Classes/Functions)
In Java, you control access using private
, protected
, public
, and package-private access levels.
- Private – Accessible only within the class.
- Protected – Accessible within the same package or subclasses.
- Public – Accessible from anywhere.
This concludes the OOP concepts in Java.