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

Java_OOPS1

Uploaded by

writeups.tanvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java_OOPS1

Uploaded by

writeups.tanvi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Java OOPS

# Encapsulation
• Encapsulation is “Wrapping of data in a single unit, with
hiding the data from another classes.”
• Fully Encapsulated class properties
a) All the attributes, data members must be declared
private.
b) There must be methods i.e. member functions
declared public to be used in other classes.
c) The class must have the getter and setter methods,
If the data members are required to access or modify
in another classes.

• Example of Encapsulation:
These are the example files where we do Encapsulation
of data in Student.java class.

---|Package com.tanvi ------->|-- Student.java


| |-- Test.java
---|Package com.student ---> |-- Test.java
Package com.tanvi ------->|-- Student.java
package com.tanvi;

class Student {
// data members i.e. attributes declared
private
private int rollNo;
private String firstName;
private String lastName;

// getter and setter methods declared public


// to access these methods in another classes
within the same package.
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;
}
}
Package com.tanvi ------->|-- Test.java
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());
}
}

Package com.student ------->|-- Test.java


package com.student;

import com.tanvi.Student; // Import the


encapsulated class from another package
// also to use the class member functions and data
members, you need to declare the Student class as
public.

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());
}
}
• Hence, A Fully Encapsulated class can be
accessed by other classes within the same
folder.

• The classes which are inside the other package


needs the Encapsulated class to be declared
public and the package of Student class to be
imported.

• Else the other classes outside the


Encapsulated class package cannot use the data
members and member functions from the
encapsulated class.
# Abstraction

• Encapsulation --> Data Hiding


• Abstraction --> Implementation Hiding
• Abstraction is the process in which we hide the
background implementation details. It provides only
essential information, which user wants or company
wants to provide.

• Abstraction can be implemented using 2 keywords:


a) By using abstract keyword
b) By using Interface

• We can achieve 100% Abstraction through Interface.


• Abstract means “declaration”

a) By using abstract keyword.


• Declare class as abstract.
• This class can have both abstract and non-abstract
methods.
• Variables declared inside the abstract class cannot be
accessed, because we can only implement the abstract
methods in sub classes.
• Sub classes must use the extends keyword to implement
the abstract methods.
• Non-abstract methods cannot be implemented or
modified in the sub classes.
• In this way Abstraction can be achieved, by which we
can hide the relevant data in the abstract class by
declaring them as private non-abstract.
And we declare only those methods as abstract which
are to be implemented, used or modified by the user.
The non-abstract methods are kept safe.
• If we want to hide some specific implementation of
some method in the abstract class from the user, then
we can declare the non-abstract method as private.
• Then the non-abstract method can never be
implemented inside the subclasses. The functionality is
always kept private.
• Other non-abstract methods can be called in sub classes
and can even be overridden to display specific outputs.

b) By using Interface.
• By using Interface, we can achieve 100% Abstraction of
data.
• In Interface we cannot declare the methods as non-
abstract methods.
• Any method or implementation inside the Interface is
always abstract whether you use abstract keyword or
not.
• The object of the Interface is not created directly, it is
not instantiated directly.
• Instead, we create a sub class that implements the
interface and its abstract methods.
• We create the object of the Interface using the sub
class.

Hence, we can hide relevant logic or implementation from


the user by using Abstraction.
# Inheritance

• Parent class is also called as Super class and Base class


• Child class is also called as Sub class and Derived class
• Child class inherits characteristics of parent class.

• Why use inheritance?


To make the code reusable.
For Method Overriding (so runtime polymorphism can
be achieved)

• Types of inheritance
a) Single level inheritance:

• There is only single parent whose properties are


inherited by a single child class.
Example:
class Student {
void study() {
System.out.println("Student is studying.");
}
}
class CollegeStudent extends Student {
void attendClass() {
System.out.println("College student is attending class.");
}
}
public class Main {
public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
cs.study(); // Output: Student is studying.
cs.attendClass(); // Output: College student is attending class.
}
}
b) Multi-level inheritance:

• When there is a chain of inheritance it is called as multi-


level inheritance.
• There are multiple classes like class A, class B, class C
class B inherits properties from class A, class C inherits
properties from class B.

Example:
class Student {
void study() {
System.out.println("Student is studying.");
}
}

class CollegeStudent extends Student {


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

class GraduateStudent extends CollegeStudent {


void conductResearch() {
System.out.println("Graduate student is conducting research.");
}
}

public class Main {


public static void main(String[] args) {
GraduateStudent gs = new GraduateStudent();
gs.study(); // Output: Student is studying.
gs.attendClass(); // Output: College student is attending class.
gs.conductResearch(); // Output: Graduate student is conducting
research.
}
}
c) Hierarchical Inheritance

• When two or more classes inherits a single class, it is


known as hierarchical inheritance.
Example:
class Student {
void study() {
System.out.println("Student is studying.");
}
}

class CollegeStudent extends Student {


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

class HighSchoolStudent extends Student {


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

public class Main {


public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
cs.study(); // Output: Student is studying.
cs.attendClass(); // Output: College student is attending class.

HighSchoolStudent hs = new HighSchoolStudent();


hs.study(); // Output: Student is studying.
hs.playSports(); // Output: High school student is playing sports.
}
}
d) Multiple Inheritance

• When a single child class inherits properties from two or


more parent classes it is called Multiple Inheritance.
• Java does not support multiple inheritance with classes
but by the use of interfaces.
Example:
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.");
}
}

public class Main {


public static void main(String[] args) {
HighSchoolStudent hs = new HighSchoolStudent();
hs.study(); // Output: High school student is studying.
hs.playSports(); // Output: High school student is playing sports.
}
}
e) Hybrid Inheritance

• Hybrid inheritance is a combination of two or more


types of inheritance.
• Since Java does not support multiple inheritance
directly, we achieve hybrid inheritance using interfaces.
Example:
class Student {
void study() {
System.out.println("Student is studying.");
}
}

interface Athletic {
void playSports();
}
class CollegeStudent extends Student {
void attendClass() {
System.out.println("College student is attending class.");
}
}
class HighSchoolStudent extends Student implements Athletic {
@Override
public void playSports() {
System.out.println("High school student is playing sports.");
}
}
public class Main {
public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
cs.study(); // Output: Student is studying.
cs.attendClass(); // Output: College student is attending class.

HighSchoolStudent hs = new HighSchoolStudent();


hs.study(); // Output: Student is studying.
hs.playSports(); // Output: High school student is playing sports.
}
}
# Polymorphism

• Polymorphism in Java is a concept by which we can


perform a single action in different ways.
• Polymorphism is derived from 2 Greek words: poly and
morphs.
• The word "poly" means many and "morphs" means
forms.
• So, polymorphism means many forms.

• There are two types of polymorphism in Java:


a) compile-time polymorphism
b) runtime polymorphism.

• We can perform polymorphism in java by


a) method overloading
b) method overriding.

A) Compile-time polymorphism:

- It is also called as static type polymorphism


- In this type of polymorphism, we do method
overloading.
- Method overloading --> happens within class.
- In method overloading we can declare multiple
methods with the same name but different
parameter types or different number of parameters.
- Change return type, types of parameters, no. of
arguments.
B) Runtime Polymorphism:

- It is also called as dynamic or runtime polymorphism.


- In this type of polymorphism, we do method
overriding.
- Method overriding --> happen within different
classes.
- In method overriding we can declare multiple methods
with the same name but logic return inside the
method body changes.
- Changes method body, else everything is as it is
- No number of arguments, type of parameters is
changed, only the logic is changed.
Interview questions on OOPS

1) Limitations of OOPs
- Complexity of code increases.
- Code length increases.

2) Name any seven widely used OOPs languages


- C++
- Java
- Python
- C#
- PHP
- JavaScript
- Kotlin
- TypeScript
-R

3) What is the purpose of using OOP’s concepts?


- Makes code readable
- Easily understandable
- Increases code reusability
- Extends features from one class to another

4) What are the four main features of OOPs?


- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
5) What is the difference between Object oriented
programming and structural programming?
- OOP is more secure, more readable
- Reusability of code is another difference.
- More expandable.

6) What do you understand by pure object oriented


programming language and why Java is not pure object
oriented programming language?
- Due to pre-defined data types
- Static keyword --> does not require object to call a
function. (eg: s.name() is not acceptable if the name
function is declared static. It can be called directly)
- Wrapper classes --> makes the objects of the primitive
datatypes. Also called user-defined objects.
- Smalltalk and Ruby are the two purely Object oriented
programming languages.

7) What do you understand by class and object give an


example.
- Class is a virtual entity. It is a category which has
specific multiple objects that have some common
features
- An object is a real-world entity, which exhibits the
properties of the class. It is an instance of the class.
- Example: Student is some class, which has properties
i.e. attributes such as rollNo, GRNo, firstname,
lastname, gender, etc. and the object are the real-
world entities such as s1, is a object who is some
students he has all the details in the Student class
such as his name-“Tanvi Sawant”, rollNo – 47, GRNo –
123456, etc. And there are such many objects of
student class.

8) What are the characteristics of an abstract class?


- Performs 0 – 100% abstraction has abstract as well as
non-abstract i.e. concrete methods.
- Abstract class in not instantiated.

9) Is it possible for a class to inherit the constructor from


its base class?
- No, we can’t inherit the constructor of any class.
- It is used to construct the object of the defined class
only.

10) What is constructor chaining?


- Constructor chaining is the process of calling one
constructor from another constructor with respect to
current object.

11) What are types of variables in OOPs?


- Global, local, instance

You might also like