0% found this document useful (0 votes)
20 views

Java_OOPS

Uploaded by

writeups.tanvi
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)
20 views

Java_OOPS

Uploaded by

writeups.tanvi
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
You are on page 1/ 6

Java OOPS

Encapsulation

• Definition: Encapsulation is the wrapping of data and methods


into a single unit, with data hiding from other classes.
• Fully Encapsulated Class Properties:
1. All attributes must be declared private.
2. Public getter and setter methods for accessing and
modifying attributes.
3. Encapsulated class can be accessed within the same
package; needs to be declared public for access from other
packages.
• Example:
java
Copy code
// Student.java in package com.tanvi
package com.tanvi;

class Student {
private int rollNo;
private String firstName;
private String lastName;

public int getRollNo() {


return rollNo;
}

public void setRollNo(int rollNo) {


this.rollNo = rollNo;
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}
}

// Test.java in package com.tanvi


package com.tanvi;

public class Test {


public static void main(String[] args) {
Student st1 = new Student();
st1.setRollNo(1);
st1.setFirstName("Tanvi");
st1.setLastName("Sawant");
System.out.println(st1.getRollNo());
System.out.print(st1.getFirstName() + " ");
System.out.print(st1.getLastName());
}
}

// Test.java in package com.student


package com.student;

import com.tanvi.Student;

public class Test {


public static void main(String[] args) {
Student st1 = new Student();
st1.setRollNo(1);
st1.setFirstName("Tanvi");
st1.setLastName("Sawant");
System.out.println(st1.getRollNo());
System.out.print(st1.getFirstName() + " ");
System.out.print(st1.getLastName());
}
}

Abstraction

• Definition: Hides the implementation details and shows only


essential features.
• Implementation:
o Abstract class: Can have both abstract and non-abstract
methods.
o Interface: Achieves 100% abstraction, containing only
abstract methods (no method bodies).
• Key Points:
o Abstract classes can’t be instantiated.
o Interfaces are implemented by classes, and their methods
must be overridden.
• Example:
java
Copy code
// Abstract class example
abstract class Student {
abstract void study();
void attendClass() {
System.out.println("Attending class.");
}
}

class CollegeStudent extends Student {


void study() {
System.out.println("Studying.");
}
}

// Interface example
interface Athlete {
void playSports();
}

class HighSchoolStudent implements Athlete {


public void playSports() {
System.out.println("Playing sports.");
}
}

Inheritance

• Definition: Child class inherits properties and methods from a


parent class.
• Types:
1. Single Level: One parent, one child.
2. Multi-Level: Chain of inheritance.
3. Hierarchical: Multiple classes inherit from a single
parent.
4. Multiple: Single child inherits from multiple parents
(achievable via interfaces in Java).
5. Hybrid: Combination of two or more types of inheritance
(achievable using interfaces).
• Examples:
java
Copy code
// Single Level
class Student {
void study() {
System.out.println("Student is studying.");
}
}

class CollegeStudent extends Student {


void attendClass() {
System.out.println("College student is attending
class.");
}
}

// Multi-Level
class GraduateStudent extends CollegeStudent {
void conductResearch() {
System.out.println("Graduate student is
conducting research.");
}
}

// Hierarchical
class HighSchoolStudent extends Student {
void playSports() {
System.out.println("High school student is
playing sports.");
}
}

// Multiple and Hybrid (using interfaces)


interface Studious {
void study();
}

interface Athletic {
void playSports();
}

class HighSchoolStudent implements Studious, Athletic {


@Override
public void study() {
System.out.println("High school student is
studying.");
}

@Override
public void playSports() {
System.out.println("High school student is
playing sports.");
}
}

Polymorphism

• Definition: Ability to take many forms. A single action can


perform in different ways.
• Types:
1. Compile-Time (Static):
▪ Method Overloading: Same method name, different
parameters.
2. Runtime (Dynamic):
▪ Method Overriding: Subclass has the same method
as declared in the parent class.
• Example:
java
Copy code
// Compile-Time Polymorphism
class Math {
int add(int a, int b) {
return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
}

// Runtime Polymorphism
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}

class Dog extends Animal {


void sound() {
System.out.println("Dog barks");
}
}

Interview Questions on OOPs

1. Limitations of OOPs: Increased complexity and code length.


2. Popular OOP Languages: C++, Java, Python, C#, PHP,
JavaScript, Kotlin, TypeScript, R.
3. Purpose of OOP Concepts: Readability, understanding,
reusability, and extension of features.
4. Four Main Features of OOP: Encapsulation, Abstraction,
Polymorphism, Inheritance.
5. Difference Between OOP and Structural Programming:
OOP is more secure, readable, and expandable with code
reusability.
6. Why Java is Not Pure OOP: Due to primitive data types, static
methods, and wrapper classes.
7. Class and Object:
o Class: A blueprint with common properties.
o Object: An instance of a class.
o Example: Student class with attributes like rollNo, name;
s1 object with specific details.
8. Abstract Class Characteristics: Contains both abstract and
non-abstract methods; cannot be instantiated.
9. Constructor Inheritance: Not possible; constructors are not
inherited.
10. Constructor Chaining: Calling one constructor from
another within the same class.
11. Types of Variables in OOP: Global, local, instance.

You might also like