DCIT 50A

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

How to implement encapsulation in java

1) Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class. 2) Have getter and setter methods in the class to set and get the values of the fields.

Advantages of Encapsulation

1. It improves maintainability and flexibility and re-usability: for e.g. In the above code the implementation code of void setEmpName(String name) and String getEmpName() can be changed at any point of time. Since the implementation is purely hidden for outside classes they would still be accessing the private field empName using the same methods (setEmpName(String name) and getEmpName()). Hence the code can be maintained at any point of time without breaking the classes that uses the code. This improves the re-usability of the underlying class. 2. The fields can be made read-only (If we don't define setter methods in the class) or write-only (If we don't define the getter methods in the class). For e.g. If we have a field(or variable) that we don't want to be changed so we simply define the variable as private and instead of set and get both we just need to define the get method for that variable. Since the set method is not present there is no way an outside class can modify the value of that field. 3. User would not be knowing what is going on behind the scene. They would only be knowing that to update a field call set method and to read a field call get method but what these set and get methods are doing is purely hidden from them.

Basic concepts of OOPs are:

1. Object 2. Class 3. Inheritance 4. Polymorphism 5. Abstraction 6. Encapsulation

The Java platform differs from most other platforms in the sense that it is a softwarebased platform that runs on the top of other hardware-based platforms. It has two components:

1. Runtime Environment 2. API(Application Programming Interface)

Java supports four access modifiers that you can use to define the visibility of classes, methods, and attributes. Each of them specifies a different level of accessibility, and you can only use one modifier per class, method or attribute. As a rule of thumb, you should always use the most restrictive modifier that still allows you to implement your business logic.

Access Modifiers

(Types of package) The already defined package like java.io.*, java.lang.* etc are known as ______.

Built-in package

A class variable is one which has the static keyword as a prefix in its declaration. Its definition occurs only inside a class and outside any function.

Class Variables

- any other Object-oriented Language is a blueprint for objects to follow a specific schema defined in the class. - It represents a collection of properties (data and functions) for all its objects. It supports a template for creating objects which bind code and data. Further, classes act as a means to define methods and data. It helps in maintaining access specifications for member variables using access specifiers.

Class in Java

Encapsulation is also known as "_______"

Data hiding

These variables are inside a class however outside any method. They come into existence when the class instantiates. These are accessible from any constructor or block of that specific class.

Instance variable

- These are variables which have declarations inside methods, constructors or blocks. They are local to the part of the code they belong. - come into existence when the control enters into the code block that keeps their declaration. And they vanish with the block leaving out of execution

Local Variables

When you don't provide any access modifier for your attribute or method, you can access it within your class and from all classes within the same package. That's why it's often called package-private.

No modifier

- is the most fundamental entity in Java or any other Object-Oriented Language. - In Java, the object is an offspring of its class. The class has properties to reflect the object state and methods to represent the behavior. The methods also show an object's response to other objects. Identity is a unique name for the object assigned by the user, much like variables.

Object in Java

-It is a way of organizing software in the form of objects that contain both data and the behavior of these objects. -are more structured towards objects. As a result, it increases performance, maintainability, and development of the program.

Object oriented programming (OOP)

- Attributes and methods with the access _______ can be accessed within your class, by all classes within the same package, and by all subclasses within the same or other packages - The ______ gets mostly used for internal methods that need to be called or overridden by subclasses. You can also use it to allow subclasses to access internal attributes of a superclass directly.

Protected modifier

- This is the least restrictive access modifier. Methods and attributes that use the ______ can be accessed within your current class and by all other classes. - _______ and attributes become part of the public API of your class and of any component in which you include them. That is almost never a good idea for any attribute, and you should think twice before you use this modifier on a method.

Public or Public modifier

Advantages of using a package in Java

Reusability Better Organizations Name Conflicts

A class can contain any of the accompanying variable sorts.

Variable types

two types of packages in Java

built-in packages packages we can create (also known as user defined package)

_______ looks like a method but it is in fact not a method. It's name is same as class name and it does not return any value. You must have seen this statement in almost all the programs I have shared above:

constructor

simply means binding object state(fields) and behaviour(methods) together. If you are creating class, you are doing encapsulation.

encapsulation

is a pack(group) of classes, interfaces and other packages. In java we use _____ to organize our classes and interfaces.

packages

These modifiers are, starting from the most to the least restrictive one

private no modifier protected public

There are two types of platforms

software-based and hardware-based

A package inside another package is known as ______.

sub package

(Types of package) The package we create is called ________.

user-defined package


Ensembles d'études connexes

EMT Chapter 17 - Neurologic Emergencies

View Set

Saylor Effective Business Communication Chapter 1

View Set

African Kingdoms/Slave Trade/Imperialism

View Set

Chinese horoscope characteristics

View Set

Ethics-Chapter 18: Code of Ethics

View Set

EOC16: End of Chapter Problems - Ch. 16: Externalities

View Set

Operations Management Quiz 3 Chapter 6, 6S

View Set

NS 125 quiz 4 practice questions

View Set