1. Conceptual Questions Briefly explain how Object Oriented Programming implemen
ID: 3877071 • Letter: 1
Question
1. Conceptual Questions
Briefly explain how Object Oriented Programming implements the concept of encapsulation and information hiding
b) In general, objects have two sets of members : attributes and methods. Explain the role these members play in designing an object.
c) What is the purpose of the constructor of a class?
d) Briefly explain the purpose of the toString method
e) What is the main difference between a static data member and non-static data member of an object. Give an example
f) When we use the Scanner class, we first create an object instance of String as shown below Scanner scan = new Scanner(System.in); However, when we use the Math class, we do not create an object instance, but use the object name Math as shown below.
Math.sqrt(20);
Briefly explain this difference in usage
Explanation / Answer
a) Encapsulation is binding wrapping of data and its behaviors together in a single unit or we can say that Encapsulation is a technique which is used to protect the information in an object from the other object. It supports three types of data access specifiers. they are:-
Public: public class member or variable means it can be used anywhere without the access restrictions.
Private: private class member or variable means it can be used only by the members and friends of class.
Protected: protected class member or variable means , it can be used only by the members and friends of class and the members and friends of classes derived from class.
information hiding means hiding the internal details of object and showing only the essential details for example all the details related to objects are stored within the object, it is hidden from the outside world and it is only manipulated by the object itself.
cout<<"Hello"; // we only know cout is used to print any variable or string but the internal details what is used for prinitng or its internal details is hidden from user.
b) Objects have 2 sets of members: attributes and methods or functions.
A method defines the behavior of the objects that are created from the class or method is an action that an object is able to perform. The association between method and class is called binding. For example: an object of the type 'person,' created using the person class. Methods associated with this class could consist of things like walking and driving. Methods are sometimes confused with functions, but they are distinct.
An attributes allow you to check the functions that are taking place.
for example:
void main(){
sum(5,4);
}
public void sum(int a, int b){
cout<<"Sum is"<<a+b;
}
in the above example the public void sum(int a, int b) is a method which is used to calculate the sum of 2 numbers while int a and int b are the attributes of function sum which is used to check whether the function is used to calculate the correct sum or not.
c) Constructor in class
- constructor is used to initialise the objects
- constructor does not have any return type but it does have the access specifies.
- constructor is same as the class name
- constructor has no return type
- constructor can be of 2 types
parametrised constructor and default constructor
default constructor: constructor with no argument
parametrised constructor: constructor with atleast on argument
example
class A{
A(){} // default constructor
A(int a, int b){return a+b; // parametrised constructor with 2 argument
}
d) toString method is used to represent object as string or it returns the object as string respresentation.
example
class Child{
String name;
int id;
Child(String name, int id){
this.name=name;
this.id=id;
}
public String toString(){//overriding the toString() method
return name+" "+id;
}
public static void main(String args[])
{
Child c1= new Child("abc",1);
Child c2 = new Child("def",2);
System.out.println(c1);
System.out.println(c2);
}
}
// output
abc 1
def 2
e) static and non static member of class
static means if we need an object to access any method or variable inside or outside the class and hence we dont need to create any instance or object. we can directly access it by its class name. it will gets the memory once the class is loaded into memory. its objects are initialised only once.
for example
class A{
static int a;
}
while the non static members means it can only be accessed outside the class by creating its instance or object. It can only be accesed by its object. Memory will be allocated multiple times whenever new objects are created. and also these are specific to objects.
example
class A{
int a;
}
f) Scanner class is used when we have to take any input from user at run time while the Math.class is used to perform the numeric operations like elementary exponential, logarithm, square root, and trigonometric functions and this will return a constant value.
for example if i want to calculate the sqaure a any number then i can write
class SqrtNum{
public static void main(String args[]){
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.