1. How is an object \"instantiated\" in a cient program? Provide an example. Wha
ID: 3748743 • 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. 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? 3. 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
3. Answer :
1. An object is instantiated in a client program using the keyword 'new'. Instantiating basically means creating a new instance of the class. For example -
In a class Student, object declaration will be like -
Student stud;
where 'stud' is an object of the type Student. Now object instantiation will be like -
stud = new Student();
where 'new' keyword instantiates the object 'stud' by allocating memory for it. Now 'new' requires an argument which is the constructor method for the object, this method is responsible for initializing the object declared and instantiated.
The two things that make a constructor different from other methods in a class are -
-> Constructors must be named after the name of the Class only and they can't return anything except the implicit object return, not even 'void', whereas a normal method may or may not have the same name as that of the Class and must have a return type.
-> Constructors initialize objects that are created with the 'new' operator and also they can't be called directly. They are called implicitly when the 'new' keyword creates an object whereas methods perform certain operations on objects which already exists and can be directly called on an object that has already being created using 'new'.
2. Information Hiding is accomplished in a Class to its members using attributes such as 'private' which means only the parent Class can access that particular object or member, 'public' which means any Class can access that information of its parent class. Information Hiding is basically a mechanism to restrict or allow access to the components of Class members or objects.
A method is declared 'public' when it can be accessed by other objects which are members of the same class or other classes.
Class Student {
public String getName() {
return "Max";
}
private int getRollNo() {
return 23;
}
}
class Teacher {
Student stud = new Student();
String studName = stud.getName(); // no error as getName() is public
int roll = stud.getRollNo(); // compiler error as getRollNo is private and can only be accessed within Student class
}
As you can see, whenever there is a need for object preservation and security, by declaring the methods as 'private' we can ensure data integrity.
3. 'Encapsulation' in terms of defining objects of a class, means wrapping the data(variables and objects) and code manipulating that data(member methods) together as a single unit. In encapsulation, the member variables of a class are hidden from other classes and can only be accessed by methods of their parent class. Hence, encapsulation is also known as Data Hiding.
Encapsulation thus makes it possible to write code time and cost efficiently, by essentially segregating the pieces of information that can be made to be accessed publically and the information which is to be kept hidden from public access and can be accessed only by public methods of its parent class thus, saving time, complexity and cost of implementing extra means of data security and integrity.
4. 'static' is a non-access modifier and is used for defining variables or methods when we need to make a single copy of that variable available for sharing among all Class objects. All instances of a Class uses or shares that same copy of the 'static' variable.
The most commonly seen 'static' method is the main() method. A method is declared 'static' when we want it to be accessed before any of its parent Class objects is declared and also without any kind of reference to any other object.
A client program has to share a single instance that is available of the variables and methods initialized by 'static'. For example -
class Student {
static String name;
static int roll;
static void setNameRoll(String n, int r) {
name = n;
roll = r;
}
}
public class Single {
public static void main(String[] args) {
Student.setNameRoll("Richard", 30); // calling static method without instantiating an object of Student class
}
}
As we can see, 'static' keyword prevents the creation of multiple instances of the same method or variables by making every object to share the same copy of them and also doesn't need the instantiating an object of the parent class for accessing 'static' variables or methods.
5. 'this' is a reference variable used to refer to the current variable or method. It can be used to refer to the instance variable of the current class. It can also be passed in the method or constructor call as an argument. It's also used to initiate the current class constructor and it's member variables.
'null' is a member of the 'null' reference type and it's the only reference value that doesn't refer to an object. Hence, there is no memory associated with 'null'. It's basically not an instance of ANYTHING and it is used when a null value is supposed to be initiated during object reference.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.