What is OOPs (object-oriented programming)?
Object-oriented programming (OOP) structures a program around objects: bundles of data (fields) and the methods that work on that data. A class describes the shape; each object is one instance with its own state.
Javapublic class Main { public static void main(String[] args) { Counter a = new Counter("clicks"); Counter b = new Counter("views"); a.increment(); a.increment(); b.increment(); System.out.println(a + ", " + b); } } class Counter { private final String name; private int value; Counter(String name) { this.name = name; } void increment() { value++; } @Override public String toString() { return name + "=" + value; } }One class, two objects counting separately: it prints
clicks=2, views=1.What is the difference between object-oriented and procedural programming?
Procedural code is functions that operate on data passed to them; object-oriented code keeps data and the functions that change it together in objects. The difference shows when you add a new kind of thing: procedural code edits every function that switches on the type, OOP adds one class.
What is the difference between a class and an object?
A class is a blueprint that declares fields and methods and holds no per-object data (only its
staticfields). An object is an instance created from it at runtime (withnewin Java), with its own copy of the instance fields. Syntax in Java classes.What are the four pillars of OOPs?
Encapsulation, abstraction, inheritance and polymorphism.
- Encapsulation: private fields changed only through methods that enforce the rules.
- Abstraction: expose what an object does, hide how.
- Inheritance: a class extends another in an is-a relation (
class Dog extends Animal). - Polymorphism: one call, behavior chosen by the object (
shape.area()).
What are the advantages and disadvantages of OOP?
OOP makes large programs easier to change because each class owns its data and rules, but it adds ceremony small programs do not need. Gains: modularity, reuse, new types without editing callers, and testing with fakes. Costs: more indirection, fragile deep inheritance trees, and shared mutable objects that complicate concurrency.
Is Java a pure object-oriented language?
No. Java's eight primitive types (
int,double,booleanand so on) are not objects, andstaticmembers belong to no object. Wrapper classes (Integer) and autoboxing bridge the gap. Smalltalk is pure OOP, and Python is close, since even integers and functions are objects:Pythonprint((5).bit_length(), type(len).__name__, isinstance(len, object))It prints
3 builtin_function_or_method True.What is the difference between a method and a function?
A method is a function that belongs to a class and runs in the context of an object (or of the class, if static); a function stands alone. In Java every function is a method. Python has both:
len(items)is a function,items.append(3)a method that receivesitemsasself.What are access modifiers in OOPs?
Access modifiers decide which code may use a class member. Java has four:
private(the class only), package-private with no keyword (the package),protected(the package plus subclasses elsewhere) andpublic(everywhere). Keep fieldsprivate. Python has only conventions (_name) and name mangling (__name). Details in Java access modifiers.What do
countandidhold after threenew Student()calls in Java?Javapublic class Main { public static void main(String[] args) { Student first = new Student(); new Student(); Student third = new Student(); System.out.println(Student.count + " " + first.id + " " + third.id); } } class Student { static int count = 0; int id; Student() { count++; id = count; } }countisstatic, so the three objects share one copy and it ends at 3.idis an instance field, so each object keeps the valuecounthad when it was built: 1 for the first, 3 for the third. A static method could readcountbut notid, because it has nothis.How do
==andequals()compare two strings in Java?Javapublic class Main { public static void main(String[] args) { String a = "hi"; String b = "hi"; String c = new String("hi"); System.out.println((a == b) + " " + (a == c) + " " + a.equals(c)); } }==compares references,equals()compares content. String literals are interned, soaandbare the same pooled object anda == bistrue.new String("hi")always creates a new object, soa == cisfalsewhilea.equals(c)istrue. Compare objects withequals(); use==for primitives, enums and deliberate identity checks.
OOPs Basics Interview Questions
The basics placement rounds start with: what OOP is, classes and objects, access modifiers, two output questions.