📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will discuss important Java OOPS (Object Oriented Programming) interview questions and answers for freshers as well as experienced candidates.
Is Java a pure object-oriented programming language?
There are a lot of arguments around whether Java is purely object-oriented or not. Well, Java is not a pure OOP language due to two reasons:The first reason is that the Object-oriented programming language should only have objects whereas Java contains 8 primitive data types char, boolean, byte, short, int, long, float, and double which are not objects. These primitive data types can be used without the use of any object.
public class IsJavaFullyOOPS {
public static void main(String[] args) {
int i = 10;
byte b = 20;
short s = 30;
long l = 100L;
double d = 100.10;
float f = 200f;
boolean flag = true;
char c = 'R';
System.out.println(i);
System.out.println(b);
System.out.println(s);
System.out.println(l);
System.out.println(d);
System.out.println(l);
System.out.println(d);
System.out.println(f);
System.out.println(flag);
System.out.println(c);
}
}
Output:10
20
30
100
100.1
100
100.1
200.0
true
R
public class IsJavaFullyOOPS {
private static String message = "hello";
public static void main(String[] args) {
// calling message instance variable without object
System.out.println(message);
// calling demo static method without object
demo();
}
private static String demo(){
return "hello from method";
}
}
What are Core OOPS Concepts?
What is Method Overloading in OOP (Java)?
package com.javaguides.corejava.basics.polymorphism;
public class MethodOverloading {
public static void main(String[] args) {
OverloadDemo ob = new OverloadDemo();
double result; // call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// Overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a * a;
}
}
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
1. Method overloading is accomplished by changing the method parameters2. Return type is not a part of method signature3. We can overload private, static and final methods4. We overload method within the same class.
public void println() {
// logic goes here
}
public void println(String x) {
// logic goes here
}
public void println(boolean x) {
// logic goes here
}
public void println(int x) {
// logic goes here
}
public void println(char x) {
// logic goes here
}
Read more about method overloading at Method Overloading in Java
What is Method Overriding in OOP (Java)?
In a Java class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. Method overriding can be used in the presence of Inheritance or Runtime Polymorphism.public class MethodOverriding {
public static void main(String args[]) {
SuperClass superClass = new SubClass();
superClass.method1("hello");
}
}
class SuperClass {
void method1(String message) {
System.out.println(message);
}
}
class SubClass extends SuperClass {
void method1(String message) {
message = message + " in SubClass";
System.out.println(message);
}
}
hello in SubClass
class Shape {
void draw() {
System.out.println("drawing...");
}
}
class Rectangle extends Shape {
void draw() {
System.out.println("drawing rectangle...");
}
}
class Circle extends Shape {
void draw() {
System.out.println("drawing circle...");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("drawing triangle...");
}
}
class TestPolymorphism2 {
public static void main(String args[]) {
Shape s;
s = new Rectangle();
s.draw();
s = new Circle();
s.draw();
s = new Triangle();
s.draw();
}
}
drawing rectangle...
drawing circle...
drawing triangle...
How can the superclass overridden method be called from the subclass overriding method?
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}
Output:
i and j: 1 2
k: 3
Can we override or overload the main() method?
Can we override a non-static method as static in Java?
class Bike {
int speedlimit = 90;
}
class Honda extends Bike {
int speedlimit = 150;
public static void main(String args[]) {
Bike obj = new Honda();
System.out.println(obj.speedlimit); //90
}
}
90
What is Abstraction and give a real-world example?
public interface Car {
public void speedUp();
public void slowDown();
public void turnRight();
public void turnLeft();
public String getCarType();
}
public class ElectricCar implements Car {
private final String carType;
public ElectricCar(String carType) {
this.carType = carType;
}
@Override
public void speedUp() {
System.out.println("Speed up the electric car");
}
@Override
public void slowDown() {
System.out.println("Slow down the electric car");
}
@Override
public void turnRight() {
System.out.println("Turn right the electric car");
}
@Override
public void turnLeft() {
System.out.println("Turn left the electric car");
}
@Override
public String getCarType() {
return this.carType;
}
}
public class Main {
public static void main(String[] args) {
Car electricCar = new ElectricCar("BMW");
System.out.println("Driving the electric car: " + electricCar.getCarType() + "\n");
electricCar.speedUp();
electricCar.turnLeft();
electricCar.slowDown();
}
}
What is Encapsulation and give a real-world example?
public class Cat {
private int mood = 50;
private int hungry = 50;
private int energy = 50;
public void sleep() {
System.out.println("Sleep ...");
energy++;
hungry++;
}
public void play() {
System.out.println("Play ...");
mood++;
energy--;
meow();
}
public void feed() {
System.out.println("Feed ...");
hungry--;
mood++;
meow();
}
private void meow() {
System.out.println("Meow!");
}
public int getMood() {
return mood;
}
public int getHungry() {
return hungry;
}
public int getEnergy() {
return energy;
}
}
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.feed();
cat.play();
cat.feed();
cat.sleep();
System.out.println("Energy: " + cat.getEnergy());
System.out.println("Mood: " + cat.getMood());
System.out.println("Hungry: " + cat.getHungry());
}
}
Feed ...
Meow!
Play ...
Meow!
Feed ...
Meow!
Sleep ...
Energy: 50
Mood: 53
Hungry: 49
What is Inheritance?
What is Composition?
What is Aggregation?
What is a SOLID OOPS Principle?
- Single Responsibility Principle - A class should have only one reason to change
- Open/Closed Principle - Software entities like classes, modules, and functions should be open for extension but closed for modifications.
- Liskov Substitution Principle - Derived types must be completely substitutable for their base types.
- Interface Segregation Principle - The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them.
- Dependency Inversion Principle - High-level modules should not depend on low-level modules. Both should depend on abstractions.
What is the Single Responsibility Principle?
What is the Open-Closed Principle?
What is the Liskov Substitution Principle?
What is the Interface Segregation Principle?
What is the Dependency Inversion Principle?
Related Java Interview Articles
- Spring Boot Interview Questions
- Java Tricky Coding Interview Questions
- Java String Interview Questions
- Java String Tricky Coding Questions
- Java main() Method Interview Questions
- Java 8 Interview Questions
- Top 10 Spring MVC Interview Questions
- Java OOPS Tricky Coding Questions
- Java Programs Asked in Interview
- OOPS Interview Questions and Answers
- Hibernate Interview Questions
- JPA Interview Questions and Answers
- Java Design Patterns Interview Questions
- Spring Core Interview Questions
- Java Exception Handling Interview
Comments
Post a Comment
Leave Comment