1. How is an object \"instantiated\" in a cient program? Provide an example. Wha
ID: 3748983 • 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
Question 4
5)
When is the ‘this ‘ keyword used
‘this’ keyword ork as a reference to the current object.
It used to handle variable hiding.
For ex,
Class a{
//Member(instance) variable ,val=10
Int val=10;
Public void display(){
//Local variable,val=5
variable=5;
System.out.println(this.val); //Here print 10
System.out.println(val); //Here print 5
}
}
When is the ‘null‘ keyword used
Null is used to avoid exception.
It is used as a reference variable.
Not used with primitive data type.
For ex,
Class myClass=null; //Object refernce as nothing or absence
String str1=null
4)
‘static keyword
They are reside in a class and called before an object is instatiated.
They store in heaps.
They shared among all objects in the same class.
Client program refer it
Using class name with dot operator.
For ex,
Class A{
Static void fun();
}
Main(){
A.fun();
}
3)
Encapsulation
Binding of data and it’s method together is called encapsulation.
It restricts the direct access of object data.
It’s actually minimizimg inter dependencies.
It makes an external interface between the module an dclients.
So client interact with interface,any change or upgradation will not affect other clients interaction.
In that sense it reduces time utilization of extension,called time-efficiency.
And cost of implementation also reducing., becauses external interface changes cannot affect internal Client’s modules.
2)
Information hiding
Hide the attributes and behavior of a class from outside world called information hiding.
In OOP , we use encapsulation for information hiding.
Use setters and getters for readability and accessability of this attributes and behavior.
For ex,
Class a{
Private int x;//Modifiers for hiding
Public void setX(int x){this.x=x}//Set value
Public int getX(){return x; }//get value.
When you need your data protect from outside world,you have to declare it as private.
Private attribute or method can’t be access anther classes or subclasses.
For ex,
Class A{
Public getName(){return name;}
Private getAge(){return age;}
}
Main(){
A.getName()//display name;
//I want to create age as secret,don’t want to expose.So if we use
A.getAge()//create error.
}
1)
Object Instatiated
An instance or object has a a value or realization making is called instantiated.
Consider , if you have a cake class,but cake has variety of types.So wecancreate each variety of cake instatiated fro the cake class.
For ex,
Class cake{
String shape;
void setShape(String shape){this.shape=shape;}
StringgetShape(){return shape;}
}
Main(){cake c=new cake();
c.setShape(“Red velvet”);
System.out.println(c.getShape());
}
Difference between constructor and other methods
Constructor create and initialize an object,that no exist yet,but method perform operations on already existing objects.
Constructor’s donot have any return type or don’t return any values,but methods must have a return type.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.