Question

In: Computer Science

(a) What is a class? What is an object? What is the relationship? (b) What are the instance variables and methods of a class?


 (a) What is a class? What is an object? What is the relationship?

 (b) What are the instance variables and methods of a class?

 (c) What is the effect of declaring instance variables and methods public or private?

 (d) Why do we often declare the instance variables of classes private?

 (e) Could we declare methods private? Would we want to do so? 

 (f) What does the identifier this mean? Give an example of its use (g) What is a constructor?

 (h) What is inheritance? Why is it useful?


Solutions

Expert Solution

a.
class:class is a model for creating objects and doesn't exist physically.a group of related things which will doesn't exist comes under a class

object:An object is anything that really exists in the world with certain properties

relationship:both the classes and objects contain variables and methods.

b.instance variables:instance variables are also kind of variables but which are declared inside a class not inside any method.which means they are useful to any method which are in same class.
       variables values can be different in different methods,they just available to all the methods inside a same class
method:In java method is useful to achieve a particular task.The statements inside a method are intended to achieve a particular sort of thing.

c.public:when we declared the instance variables or methods are public then they will available to other classes also.When we want to use the variables and methods of a class in other classes then it should be declared with public keyword.
private:we use private keyword to prevent usage for other classes.If they declared with private keyword then they only available for same class,not accessible or usable in any other classes.

d.It is just for security concerns.with the help of private keyword we can disable the use of access in other classes

e.Yes,we could declare methods as private.We can do this when this method has no use in other classes.There is a situation where a same method need to use in different classes.If there is no use with this method in other classes.
    then we can declare it as a private.All this can achieve with the help of concept called "inheritance"

f.this:"this" is a special keyword in java,which is used to invoke current class object.this refers to the object of the present class.At most this is used to achieve the clashes of variables or methods which have same names.
   It can point out the locality.

Example:

output:

g.constructor:constructor is used to initialize the instance variables.When we directly initialized the instance variables of a class then no need of writing any constructor, a default constructor can takes place all the work.
   with parameter passing we need to write a parametrized constructor.the name of the class name of constructor name should be same.

h.Inheritance:Acquiring the properties from it's previous classes is known as inheritance.Which means newly derived class can have the all the properties of derived class.The keyword 'extends' will be used.
   It is used when we need to work with it's previous class in present class ,without writing the entire code of previous class we just use inheritance concept.


Related Solutions

Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
Problem 3 Prototype object doubleQ A doubleQ object has 2 instance variables: firstQ and secondQ arrays...
Problem 3 Prototype object doubleQ A doubleQ object has 2 instance variables: firstQ and secondQ arrays that together implement a queue enqueue() instance method adds its argument to the end of this.secondQ dequeue() instance method removes and returns the element at the front of this.firstQ transfer() instance method removes the element from the front of this.secondQ, adds it to the end of this.firstQ If this.secondQ is empty, does nothing Test code is included in the file Download this file (with...
1. Describe the difference between instance methods and class methods in Java and give an example...
1. Describe the difference between instance methods and class methods in Java and give an example of each. 2. A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application? 3. Describe the difference between abstract classes and concrete classes, giving an example of each. 4. Explain how data are encapsulated and information is hidden in Java? 5. Explain the difference between a class and an interface...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
1. Can two variables share a deterministic relationship, and yet be uncorrelated? For instance, suppose that...
1. Can two variables share a deterministic relationship, and yet be uncorrelated? For instance, suppose that X follows a continuous, uniform distribution between −1 and 1: X ~ U[−1, 1]. a. Draw a graph of the distribution of X. By a symmetry argument—the distribution of X is symmetric around zero—it is clear that E(X) = 0. Now let Y = X2. b. Clearly Y and X are not independent; in fact, a deterministic relationship exists between them (if you know...
1. Can two variables share a deterministic relationship, and yet be uncorrelated? For instance, suppose that...
1. Can two variables share a deterministic relationship, and yet be uncorrelated? For instance, suppose that X follows a continuous, uniform distribution between −1 and 1: X ~ U[−1, 1]. a. Draw a graph of the distribution of X. By a symmetry argument—the distribution of X is symmetric around zero—it is clear that E(X) = 0. Now let Y = X2. b. Clearly Y and X are not independent; in fact, a deterministic relationship exists between them (if you know...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT