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
Do'stlaringiz bilan baham: