0% found this document useful (0 votes)
20 views4 pages

Encapsulation Send

Uploaded by

Huy Cauchy
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 views4 pages

Encapsulation Send

Uploaded by

Huy Cauchy
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/ 4

Encapsulation

1. Encapsulation Overview:
o Encapsulation is the aggregation of data and behavior.
o A class is a combination of data (fields/properties) and methods.
o Data should be hidden from the outside, and behaviors should be accessed only
via methods.
o Methods should include boundary conditions to ensure data validity.
o Constructors initialize objects, and getters/setters are used to enforce
encapsulation.
2. Identifying Classes:
o Main nouns represent classes.
o Nouns as modifiers of the main noun represent fields.
o Verbs related to the main noun represent methods.
o Example: For a student, the main noun is "Student," fields are code, name, birth
year, address, and methods are input and output.
3. Class Design Hints:
o Coupling: Low coupling is preferred to ensure that a class does not depend too
heavily on the internals of another class.
o Cohesion: High cohesion is preferred, where a class or method performs a single
task or closely related tasks.
4. Declaring/Using a Class in Java:
o Class declaration syntax is provided, including fields, constructors, and methods.
o Constructors can be default (no parameters) or parametric (with parameters).
5. Member Functions:
o Getter and Setter methods are used to read and modify field values while
maintaining encapsulation.
o Other methods include operations related to the class.
6. Creating Objects:
o Objects are created using the new keyword, which involves declaration,
instantiation, and initialization.
7. Access Modifiers:
o Access modifiers (public, protected, private, and default) control the visibility and
access levels of classes, methods, and fields.
8. Case Study:
o An example case study of a sports car is provided, demonstrating the application
of encapsulation principles in designing a class with various attributes and
methods.

1
Questions and Anwsers

1. Q: What is encapsulation in OOP?


o A: Encapsulation is the concept of bundling data (fields/properties) and methods
that operate on the data into a single unit, typically a class. It also involves
restricting access to some of the object's components and only exposing a limited
interface (via methods) to interact with that data.
2. Q: What are the components of a class?
o A: A class is composed of data (fields/properties) and methods (functions that
operate on the data).
3. Q: Why should data in a class be hidden?
o A: Data should be hidden (using access modifiers like private) to protect it from
unauthorized access and modification, ensuring that the internal state of the object
can only be changed in controlled ways.
4. Q: What is the role of a constructor in a class?
o A: A constructor initializes an object of a class, setting initial values for its fields
or performing other setup procedures.
5. Q: What are getters and setters?
o A: Getters are methods that retrieve the value of a property, while setters are
methods that set or update the value of a property. They provide controlled access
to the fields of a class.
6. Q: How do you identify a class from a problem statement?
o A: The main noun often represents the class, modifiers of the main noun represent
fields, and verbs related to the main noun represent methods.
7. Q: Give an example of identifying a class from a problem statement.
o A: For a problem involving a student, the main noun is "Student"; fields could
include code, name, birthYear, address, and methods could include input()
and output().
8. Q: What is coupling in OOP?
o A: Coupling is the degree of dependency between two classes or objects. High
coupling means that classes are highly dependent on each other, while low
coupling indicates a more independent relationship.
9. Q: What is preferred, high coupling or low coupling?
o A: Low coupling is preferred because it ensures that classes are not overly
dependent on each other, which enhances modularity and ease of maintenance.
10. Q: What is cohesion in OOP?
o A: Cohesion refers to how closely related and focused the responsibilities of a
class or a method are. High cohesion means that a class or method is focused on a
single task or closely related tasks.
11. Q: What is preferred, high cohesion or low cohesion?
o A: High cohesion is preferred because it leads to more understandable,
maintainable, and reusable code.
12. Q: How do you declare a class in Java?
o A:

2
java
Copy code
public class ClassName {
[modifier] Type field;
[modifier] Type method(Type param) {
// <code>
}
}

13. Q: What is the syntax for a default constructor in Java?


o A:

java
Copy code
public ClassName() {
// <initialization code>
}

14. Q: What is the syntax for a parametric constructor in Java?


o A:

java
Copy code
public ClassName(Type param1, Type param2) {
this.field1 = param1;
this.field2 = param2;
}

15. Q: What does the keyword this represent in Java?


o A: this represents the current object within its class.
16. Q: What is a member function?
o A: A member function is a function (method) that is defined inside a class and
operates on the data members (fields) of the class.
17. Q: Provide an example of a getter method.
o A:

java
Copy code
public String getName() {
return name;
}

18. Q: Provide an example of a setter method.


o A:

java
Copy code
public void setName(String name) {
if (!name.isEmpty()) {
this.name = name;
}
}

3
19. Q: How are arguments passed to a constructor or method in Java?
o A: Arguments are passed by value in Java, meaning that a copy of the argument is
passed to the method or constructor.
20. Q: How do you create an object of a class in Java?
o A:

java
Copy code
ClassName obj = new ClassName();
// or
ClassName obj = new ClassName(params);

21. Q: What is the difference between a default and parametric constructor?


o A: A default constructor has no parameters and initializes an object with default
values, while a parametric constructor takes one or more parameters to initialize
an object with specific values.
22. Q: What happens if you do not implement any constructor in a class?
o A: The Java compiler automatically provides a default no-argument constructor if
no constructor is implemented.
23. Q: What is a package in Java?
o A: A package is a namespace that organizes a set of related classes and interfaces,
helping to avoid naming conflicts and to manage the code structure.
24. Q: What is the purpose of access modifiers in Java?
o A: Access modifiers control the visibility and accessibility of classes, methods,
and fields, determining which parts of the code can access or modify them.
25. Q: Name the four access levels in Java.
o A: Public, protected, private, and default (no specifier, also known as package-
private).
26. Q: Which access modifier allows free access to a class or member from any other
class?
o A: Public.
27. Q: Which access modifier restricts access to a class or member to its own package
and subclasses?
o A: Protected.
28. Q: Which access modifier restricts access to a class or member to within its own
class only?
o A: Private.
29. Q: What does the keyword super do in Java?
o A: super is used to refer to the superclass (parent class) of the current object and
can be used to call the superclass's methods or constructors.
30. Q: What is the order of access levels from least restrictive to most restrictive?
o A: Public > Protected > Default > Private.

You might also like