0% found this document useful (0 votes)
52 views7 pages

Key OOP Concepts in Java Explained

The document outlines key concepts of Object-Oriented Programming (OOP) using Java, including definitions of classes, objects, abstraction, encapsulation, method overloading, and method overriding. It also explains access modifiers, static members, inheritance, and provides examples for better understanding. Additionally, it covers the creation of arrays and packages in Java.

Uploaded by

rmb6gpcgvk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views7 pages

Key OOP Concepts in Java Explained

The document outlines key concepts of Object-Oriented Programming (OOP) using Java, including definitions of classes, objects, abstraction, encapsulation, method overloading, and method overriding. It also explains access modifiers, static members, inheritance, and provides examples for better understanding. Additionally, it covers the creation of arrays and packages in Java.

Uploaded by

rmb6gpcgvk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOP USING JAVA IMPORTANT TWO MARK QUESTIONS

1. What is meant by Object Oriented Programming?

• Object-Oriented Programming (OOP) is a programming paradigm based on the concept of


"objects", which are instances of classes. Java is a pure object-oriented language.
• Object oriented programming is a programming approach in which there is collection of
objects.
• Object-oriented programming aims to implement real-world entities like Object, Class, Data
Abstraction, Data Encapsulation, Inheritance & Polymorphism
2. What is a class and object?
Object:
• An instance of a class. Objects have states and behaviors.
• Objects are basic run-time entities..
• Using objects we can access the member variable and member function of a class.
Syntax:
• ClassName objectName = new ClassName();

Class:
• A blueprint/template for creating objects. It defines properties (variables) and behaviors
(methods).
• A class is a collection of similar objects, class can be defined as an entity in which data and
functions are put together.
Syntax:
class ClassName
{
// attributes / properties (variables)
// methods (functions)
}
• Attributes / Properties / Fields = variables that hold data related to the object (like name, age,
etc.)
• Methods = functions/behaviors that define what the object can do or how it can act.

3. Define abstraction.

• Data abstraction means representing only essential features by hiding all the implementation
details.
• Hiding complex implementation details and showing only the necessary features.
Ways to achieve Abstraction:
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)

4. Define Encapsulation.

• Wrapping data (variables) and code (methods) together as a single unit and restricting access
using access modifiers (like private).

5. Outline the concept of method overloading in Java and explain how it works with an example.

• Method Overloading is a feature in Java that allows a class to have more than one method with
the same name, but different parameter lists (type, number, or order of parameters).
• It is a form of compile-time polymorphism.
Example:
class Calculator
{
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
}
public class Main
{
public static void main(String[] args)
{
Calculator c = new Calculator();
[Link]([Link](5, 10));
[Link]([Link](3.5, 4.5));
[Link]([Link](1, 2, 3));
}
}
Output:
15
8.0
6

6. Outline method overriding.

• Method Overriding is a feature in Java where a subclass provides a specific implementation /


redefine of a method that is already defined in its superclass.
• It is used to achieve runtime polymorphism.
Example:
class Vehicle
{
void run()
{
[Link]("Vehicle is running");
}
}
class Bike extends Vehicle
{
void run()
{
[Link]("Bike is running");
}
}
public class Main
{
public static void main(String[] args)
{
Vehicle v = new Bike();
[Link]();
}
}
Output:
Bike is running

7. In Java, how to make an object completely encapsulated?


To make an object completely encapsulated in Java:
Declare all variables as private. Provide public getter and setter methods to access and update them.

8. How do you make an abstract class?

• A class that is declared as abstract is known as abstract class. Abstract classes cannot be
instantiated, but they can be subclassed.
Syntax:
abstract class <class_name>
{
Member variables;
Concrete methods
{
}
Abstract methods();
}

9. What is the difference between constructor and method?

Feature Constructor Method


Used to perform operations or
Purpose Used to initialize objects.
behaviors of objects.
Can have any valid identifier name
Name Must have the same name as the class.
(not necessarily class name).
Must have a return type (int, String,
Return Type No return type (not even void).
void, etc.).
Called automatically when an object is Called explicitly using object
Invocation
created using new. reference or class name (if static).
Methods are inherited and can be
Inheritance Constructors are not inherited.
overridden (if not final or private).
Can be overloaded (multiple Can also be overloaded and
Overloading
constructors with different parameters). overridden.
Default If no constructor is defined, Java If no method is defined, Java does not
Availability provides a default constructor. provide a default method.
10. What is Static member?

• Static Members are data members (variables) or methods that belong class rather than to the
objects of the class. Hence it is not necessary to create object of that class to invoke static
members.

11. What are different types of access modifiers (Access specifiers)?


1. Private - Accessible only inside the class where it is defined. Used to enforce encapsulation.
2. default (no modifier specified) - Accessible only within the same package.
3. Protected - Accessible in the same package and also in subclasses of other packages.
4. Public - Accessible from anywhere in the program

12. List the operators in java.


Operator Operators /
No. Example with Variables Output Explanation
Type Symbols
int a = 10, b = 3; c = 13 — Adds a and
1 Arithmetic +, -, *, /, %
int c = a + b; b
a = 10, b = 3; true — 10 is greater
2 Relational ==, !=, >, <, >=, <=
[Link](a > b); than 3
&&, ||,
a = true; b = false;
3 Logical ! ( Reverses the false
[Link] (a && b);
boolean value)
=, +=, -=, *=, /=, a = 8 — Same as a =
4 Assignment int a = 5; a += 3;
%= a+3
b = ++a => b = 6
Unary /
b = a++ => b = 5
5 Increment / +, -, ++, --, ! int a = 5;
b = a-- => b = 5
Decrement
b = --a => b = 4
int a = 6, b = 3;
[Link](a
& b);
6 Bitwise &, ` , ^, ~, <<, >>` Binary of a = 6 →
0110
Binary of b = 3 →
0011
Ternary / int a = 5, b = 10;
7 ?: max = 10
Conditional int max = (a > b) ? a : b;
int a = 4; 8 — 4 shifted left by
8 Shift <<, >>, >>>
[Link](a << 1); 1 (4×2 = 8)

13. Define string in Java. Write the syntax for string literal.

• A String in Java is an immutable sequence of characters. Once created, the value of a String
object cannot be changed
Creating string
There are two common ways to create a String:
Using String Literal
String str1 = "Hello World";

14. Demonstrate how do you call a static member in Java?

• In Java, a static member (variable or method) belongs to the class itself, not to any specific
object. That means you can access it without creating an object of the class.

15. How do you make an array in Java?

• An array is a collection of similar-type variables grouped under one name, accessed using an
index.

Step Description Example


Declaration Declares an array variable of a specific data type. int[] arr;
Instantiation Allocates memory for the array using the new keyword. arr = new int[5];
Assigns values to the array elements using index arr[0] = 10;
positions or
Initialization
or int[] arr = {10, 20, 30, 40,
directly. 50};

16. How does one import a single package?


• A package in Java is a namespace that organizes a set of related classes and interfaces.
. Syntax to Declare a Package
package packageName;
This must be the first line in the Java file.
Syntax to Import a Class or Package
Syntax Purpose Example
import [Link]; Imports a specific class import [Link];
import packageName.*; Imports all classes in the package import [Link].*;
• we can use import to bring built-in or user-defined packages.

17. What is meant by Inheritance? List its advantages?


• Inheritance is the process of deriving a new class (child/subclass) from an existing class
(parent/superclass).
Advantages of Inheritance
• Reusability → Parent class code can be reused.
• Extensibility → Base logic can be extended.
• Data Hiding → Parent can keep sensitive data private.
• Method Overriding → Child can redefine parent methods.

18. Define super class and subclass.


Superclass
• Superclass / Base class / Parent class → The class whose properties are inherited.
• A superclass is the class whose properties and behaviors (fields & methods) are inherited by
another class (subclass).
• Also called base class or parent class.
Subclass
• Subclass / Derived class / Child class → The class that inherits properties from another class.
• A subclass is a class that inherits properties and methods from another class (superclass).
• Also called derived class or child class.

19. Define inheritance. List the types of inheritance.


• The child class inherits the properties and behaviors (fields and methods) of the parent class
and can also define its own features.

20. What is the difference between super class and subclass?


Feature Superclass Subclass
The parent class from which other classes The child class that inherits from a
Definition
derive properties and behavior. superclass.
Inherits members (fields & methods)
Inheritance Does not inherit from the subclass.
of the superclass.
Can contain public, protected, private, or Can access superclass members
Accessibility
default members. (except private).
Represents specific characteristics
Purpose Represents general characteristics.
(extends functionality).
Keyword Declared normally. Declared using the extends keyword.
Example class Animal { ... } class Dog extends Animal { ... }

Common questions

Powered by AI

Inheritance in Java allows a new class (child class) to inherit fields and methods from an existing class (parent class), enhancing both reusability and extensibility . For example, if you have a class 'Animal' with common methods like 'move()' and 'eat()', a subclass 'Bird' can inherit these, allowing code reuse without rewriting the methods. Additionally, the 'Bird' class can add or override methods to fit specific requirements, such as a 'fly()' method. This extends the functionality by leveraging existing code while introducing specialized behaviors in subclasses . Thus, inheritance enables the building of a hierarchy of classes that promotes code reuse and growth.

Object-Oriented Programming (OOP) is a paradigm that uses "objects" - instances of "classes" - to create models based on the real world. The core principles include encapsulation, inheritance, polymorphism, and abstraction . In Java, a language designed as a pure object-oriented one, these principles are integral. Encapsulation is achieved by wrapping data and methods into classes and using access modifiers . Inheritance allows new classes to inherit properties and methods from existing ones . Polymorphism enables objects to be processed differently based on their data type or class . Abstraction involves hiding implementation details to highlight essential features . These principles together form the backbone of Java's design and facilitate creating flexible, scalable, and maintainable software applications.

Encapsulation in Java enables data hiding by restricting direct access to an object's components and only exposing the necessary aspects through well-defined interfaces (getter and setter methods). It is implemented using access modifiers like 'private' to restrict access to the class variables, ensuring that any changes to an object’s state are controlled and validation can be incorporated within the setter methods when altering the object's variables . This abstraction not only enhances data security but also prevents accidental interference with an object's internal state from unauthorized access or modification.

Abstract classes and interfaces in Java both serve to achieve abstraction by enabling the definition of abstract types that hide implementation details and expose essential operations . An abstract class can have both abstract methods (without body) and concrete methods (with body), allowing for a partial implementation to be shared among subclasses. It supports 0 to 100% abstraction. In contrast, an interface allows only abstract methods (originally, though Java 8 introduced default and static methods) and thus supports 100% abstraction. While an abstract class is intended for extending to create tightly related objects, interfaces are used for defining capabilities that can be added to classes from different hierarchies .

Method overloading in Java is an example of compile-time polymorphism, where multiple methods have the same name but different parameter lists within the same class . The exact method to use is determined at compile time, based on the method signature. For example, in a class 'Calculator', there might be several 'add' methods overloaded with different parameters like int, double, or multiple int values. At compile time, the type of the arguments passed will determine which 'add' method will be executed . This allows the flexibility to perform the same operation - such as addition - on different types or numbers of inputs.

You might also like