Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. How is an object \"instantiated\" in a cient program? Provide an example. Wha

ID: 3748985 • Letter: 1

Question

1. How is an object "instantiated" in a cient program? Provide an example. What two things make a constructor different from other methods in a class? 2. Explain how "information hiding" is accomplished with attributes and behaviors of a class. When would a method be declared as private instead of public? Give an example. 3. What does the term "encapsulation" mean in terms of defining objects of a class? How does that make it possible to write code time-efficiently and cost-effectively? 4. When is the word "static" used in defining variables or methods? How does this change the way that a client program refers to it? 5. When is the keyword "this" used? When is the keyword "null" used?

Explanation / Answer

In class-based programming, objects are created from classes by subroutines called constructors, and destroyed by destructors. An object is an instance of a class, and may be called a class instance or class object; instantiation is then also known as construction. ... An object may be varied in a number of ways.

.Constructor are used to initialize the state of object,where as method is expose the behaviour of object. 2.Constructor must not have return type where as methodmust have return type. 3.Constructor name same as the class name where asmethod may or may not the same class name.) In programming, the process of hiding details of an object or function.Information hiding is a powerful programming technique because it reduces complexity. ... The programmer can then focus on the new object without worrying about the hidden details.

14down voteaccepted

When a method is public it means it can be accessed by other objects

For instance:

The method getName may be accessed by other classes because it is public:

The advantage.. well you can use it from other places.

When a method is private it means it can only be accessed by objects OF THE SAME CLASS

For instance, in this new definition:

The method getAge can't be accessed by other classes because it is private, if you try to do it, the compiler will give you an error message:

But, if you can use it within David class:

The advantage? You can create a bunch of methods and keep them private, avoiding data corruption or in general preserving your objects encapsulated

About encapsulation

In OOP ( object oriented programming ) the intention is to model the software after real life objects.

Real life objects have ( among other things ) attributes and methods to access those attributes.

You want to make public some of those methods, and keep private others.

For instance, a Human being, have a heart. But it is not exposed to everybody, it would be dangerous. It is encapsulated inside our body.

If we were to model a software after a real Human we may declare the method: heartBeat as private ( so, nobody can access it )

In the other hand, it would be useful to have come publicmethods like getGender to find out if your Human instance is male or female.

There are other access modifiers such as: "protected" and package protected ( whose doesn't have a keyword )

Use static when you want to provide class level access to a method, i.e. where themethod should be callable without an instance of the class. Static methods don't need to be invoked on the object and that is when you use it. Example: your Main() is a static and you don't create an object to call it.

In databases a common issue is what value or placeholder do you use to represent a missing values. In SQL, this is solved with null. It is used to signify missing or unknown values. The keyword NULL is used to indicate these values.

this is a keyword in Java. this keyword in java can be used inside the Method or constructor of Class. It(this) works as a reference to the current Object, whose Method or constructor is being invoked. This keyword can be used to refer to any member of the current object from within an instance Method or a constructor