Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet6/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   2   3   4   5   6   7   8   9   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Security: OOPs principles like encapsulation help to keep
the data and the code that operates on the data together and
makes the code secure
Simplicity: Java classes represent real world objects. This
makes code very easy to understand
Maintainability: Code written using OOPs principles is
easier to maintain
Write a code snippet that demonstrates encapsulation.
Answer:
Encapsulation refers to keeping the data and the code that
operates on the data together as a single unit. Simply creating a
class with private fields and public getter/setter methods is an


004.
a.
b.
example of encapsulation. The following code snippet
demonstrates this:
public class Laptop {
private String memory;
public String getMemory () {
return memory;
}
public String setMemory (String newMemory){
memory = newMemory;
}
}
Here, there is a class called Laptop. It has a private field called
memory and public getter and setter methods to access/modify
the memory field. So, the memory field cannot be accessed
directly outside the class, it can only be accessed via its
getter/setter methods.
What are the types of inheritance relationships?
Answer:
Inheritance relationships specify how code can be reused. There
are two types of inheritance relationships.
They are as follows:
IS–A
An IS–A relationship is implemented via inheritance, that is by
creating a sub–class. Assume that, Camera is a subclass and
Electronics is a super class. In that case, we can say that,
Camera IS–A Electronic product
HAS–A


005.
006.
A HAS–A relation can be created via composition, that is by
creating a field corresponding to another class. Assume that,
inside the Camera class, there is an object called Battery. In
that case, we can say that Camera HAS–A object called
Battery

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling