0% found this document useful (0 votes)
2 views6 pages

Java interview Questions

Object-Oriented Programming (OOP) organizes code through principles like Encapsulation, Abstraction, Inheritance, and Polymorphism, enhancing code management and reuse. Key concepts include method overloading and overriding, which provide flexibility and customization in method behaviors. OOP is supported by many programming languages, including Java, C++, and Python, and utilizes access modifiers for data security and organization.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
2 views6 pages

Java interview Questions

Object-Oriented Programming (OOP) organizes code through principles like Encapsulation, Abstraction, Inheritance, and Polymorphism, enhancing code management and reuse. Key concepts include method overloading and overriding, which provide flexibility and customization in method behaviors. OOP is supported by many programming languages, including Java, C++, and Python, and utilizes access modifiers for data security and organization.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 6

use of oops?

OOP helps keep code organized by grouping related data and functions together,
making it easier to manage and reuse. It also lets developers build on existing
code, making programs simpler to expand and maintain.

There are four key principles of OOP: Encapsulation, Abstraction, Inheritance, and
Polymorphism.

---Encapsulation? (Hiding the Details and Keeping it Together)


Encapsulation is bundling data and methods together in one unit, keeping them safe
from outside interference.

Encapsulation is like putting all the important details inside a box and only
showing the outside to others. It protects the inside details so they can’t be
changed or messed with directly.

Real-life example: Think of a TV remote. When you press a button, the remote sends
a signal to change the channel or volume, but you don’t see or control the internal
wires or circuits. All those internal parts are hidden (encapsulated), and you only
interact with the buttons.

Real-life example: Think of a car. As a driver, you can control the car by using
its interface: the steering wheel, pedals, and gear shift. You don’t need to know
how the engine, transmission, or fuel system works internally—these details are
encapsulated from you.

Example: Imagine a coffee machine. You can press a button to make coffee, but you
don’t see or change how the machine grinds the beans or heats the water. The coffee
machine’s internal parts are hidden (encapsulated), and you only interact with the
buttons.

---Abstraction? (Showing Only What's Needed, Hiding Complexity)

Abstraction is hiding complex details to show only the essential parts, making
things easier to understand and use.

showing only the essential features and hiding the complex parts.

Real-life example: Using an ATM is a good example. When you interact with an ATM,
you only care about withdrawing money or checking your balance. You don't need to
know the internal workings of the machine or how the bank processes your
transaction.

Example: Think of driving a car. When you drive, you only care about the controls
like the steering wheel, pedals, and gear shift. You don’t need to know how the
engine works or how the car is built. The complex details are hidden, and you only
interact with the important functions (drive, brake, etc.).

---Polymorphism?
Polymorphism allows objects to be used in different ways through a shared
interface, enabling flexibility and simpler code management.

Real-life example: Think of shapes like circles, squares, and rectangles. They all
have a method to calculate area, but each shape calculates its area differently.

Real-life example : animals - barking

--Inheritance?
Inheritance allows a new class to use properties/characteristics from an existing
class, saving time by reusing code.

Real-life example: A smartphone can be thought of as a specialized form of a basic


phone. Both can make calls and send texts, but a smartphone has additional features
like browsing the web or using apps. The smartphone inherits basic phone features
and adds its own.
basic phone - parent/base class
smartphone - child/derived class

----OOP is supported in many widely-used programming languages, including:

Java
C++
Python
C#
Ruby
JavaScript
Swift
PHP

Method Overloading and Method Overriding - key concepts in Object-Oriented


Programming (OOP)
They allow flexibility and the ability to customize behaviors of methods within
classes, but they serve different purposes.

---Method Overloading:
Definition: Method overloading is when multiple methods in the same class have the
same name but differ in the number or type of parameters. This is also known as
compile-time polymorphism or static polymorphism.

Example: (ATM TRANSACTION) ATM CLASS - DEPOSIT METHODS .. CASH/CHECK , calculator

class ATM {
// Deposit with cash
void deposit(int cashAmount) {
System.out.println("Deposited cash: " + cashAmount);
}

// Deposit with check


void deposit(String checkNumber, double amount) {
System.out.println("Deposited check: " + checkNumber + ", Amount: " +
amount);
}
}

Here, both methods are named deposit, but one accepts an integer (for cash
deposits) and the other accepts a string and a double (for check deposits).
Depending on how the user wants to deposit, the appropriate method will be called
at compile time.

---Method Overriding:
Method overriding occurs when a subclass provides a specific implementation of a
method that is already defined in its superclass. This is called runtime
polymorphism or dynamic polymorphism.
Real-Time Example (Employee Management System):, vehicle speed

class Employee {
// General salary calculation
void calculateSalary() {
System.out.println("Calculating employee salary");
}
}

class FullTimeEmployee extends Employee {


// Full-time employee salary calculation
@Override
void calculateSalary() {
System.out.println("Calculating salary for full-time employee");
}
}

class PartTimeEmployee extends Employee {


// Part-time employee salary calculation
@Override
void calculateSalary() {
System.out.println("Calculating salary for part-time employee");
}
}

In this example, both FullTimeEmployee and PartTimeEmployee override the


calculateSalary method defined in the Employee class. The method is called at
runtime depending on the type of employee object. This provides a more specific
implementation without changing the method name, making the code clean and
extensible.

---Method overloading allows for flexibility in how methods with the same name
handle different parameters within a class, while method overriding enables a
subclass to provide its own specific implementation of a method. Both are essential
for achieving polymorphism in object-oriented programming.

---Access Modifiers - public, private, default, protected


Public allows access from anywhere.
Private restricts access to within the class.
Protected allows access within the package and to subclasses outside the package.
Default allows access only within the package.

These modifiers are essential for encapsulation, which helps in controlling the
access to data and methods, ensuring better security and code organization in Java
programs.

---JAVA - PLATFORM INDEPENDENT


Java is platform-independent because it compiles code into bytecode, which can run
on any system with a Java Virtual Machine (JVM). The JVM translates this bytecode
into machine code for the specific operating system. This means you can write your
code once and run it anywhere without changes.

---STATIC KEYWORD
Method is called directly using the class name without creating object.
Example:

class Counter {
// Static variable
static int count = 0;

// Static method
static void increment() {
count++; // Increase count by 1
}

// Instance method
void displayCount() {
System.out.println("Count: " + count);
}
}

public class Main {


public static void main(String[] args) {
Counter.increment(); // Call static method without creating an object

Counter c1 = new Counter(); // Create an object of Counter


c1.displayCount(); // Display count, which is now 1

Counter.increment(); // Call static method again


Counter c2 = new Counter(); // Create another object
c2.displayCount(); // Display count, which is now 2
}
}

--=FINAL KEYWORD
to declare constants or to prevent method overriding and inheritance.

Example :
final int max_sum = 100; //constant

---ABSTRACT CLASS
1. we cannot create object directly.
2. It contains both regular methods(with code implementation) and abstract
methods(no code implementation).
3. Implementation of abstract method will be done in subclasses by inheriting and
overriding.
4. It is used as blueprint for other classes.
5. extends keyword
6. will have variables and constructors. (single inheritance)

// Abstract class
abstract class Animal {
// Abstract method (no implementation)
abstract void makeSound();

// Regular method (with implementation)


void eat() {
System.out.println("This animal eats food.");
}
}

// Subclass that extends the abstract class


class Dog extends Animal {
// Implementing the abstract method
@Override
void makeSound() {
System.out.println("Bark");
}
}

// Another subclass
class Cat extends Animal {
// Implementing the abstract method
@Override
void makeSound() {
System.out.println("Meow");
}
}

public class Main {


public static void main(String[] args) {
Animal myDog = new Dog(); // Creating a Dog object
Animal myCat = new Cat(); // Creating a Cat object

myDog.makeSound(); // Outputs: Bark


myDog.eat(); // Outputs: This animal eats food.

myCat.makeSound(); // Outputs: Meow


myCat.eat(); // Outputs: This animal eats food.
}
}

---Interface :
1. java doesnot support multiple inheritance, but using interface it is done.
(implements multiple interfaces)
2. Implements keyword is used
3. class will have, declaration of method without implementation and default
implementation about behaviour since java 8.
4. No instance variables, only constant variables (public,static,final).
5. what methods must be present, without telling how they should be done, allowing
any class to implement those methods.
6. Loose coupling

interface Flyable {
void fly();
}

class Bird implements Drivable, Flyable {


@Override
public void drive() {
System.out.println("Bird is moving.");
}

@Override
public void fly() {
System.out.println("Bird is flying.");
}
}

--- class - class (extends)


interface - interface (extends)
class - interface (implements)
---

You might also like