Question

In: Computer Science

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 in Java, using at least one example.

Solutions

Expert Solution

Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. Class methods cannot access instance variables or instance methods directly they must use an object reference.

They both are member variables, meaning that both are associated with a class. Now of course, there are differences between the two:

Instance variables:

These variables belong to the instance of a class, thus an object. And every instance of that class (object) has it's own copy of that variable. Changes made to the variable don't reflect in other instances of that class.

Class variables:

These are also known as static member variables and there's only one copy of that variable that is shared with all instances of that class. If changes are made to that variable, all other instances will see the effect of the changes.

The only real difference is that a concrete class can be instantiated because it provides (or inherits) the implementation for all of its methods. An abstract class cannot be instantiated because at least one method has not been implemented.

The primary use for encapsulation is to hide implementation details from calling code. When calling code wants to retrieve a value, it should not depend on from wherethe value comes. Internally, the class can store the value in a field or retrieve it from some external resource


Related Solutions

Give an example of an inner and outer class. (Java)
Give an example of an inner and outer class. (Java)
true or false give reason language in java 1.A static inner class can access the instance...
true or false give reason language in java 1.A static inner class can access the instance variables and methods of its outer non-static class 2.executeQuery from statement may return more than one resultset object 3.Connection is java.sql interface that establishes a session with a specific database 4.Writable is an interface in Hadoop that acts as a wrapper class to almost all the primitive data type 5.Text is the wrapper class of string in Hadoop
Describe the difference between a repressible operon and an inducible operon. Give an example of either...
Describe the difference between a repressible operon and an inducible operon. Give an example of either type of operon and describe the mechanism by which the operon responds to an environmental cue. Describe the differences between general transcription factors and specific transcription factors and their impact on gene expression. Compare and contrast microRNA (miRNA) and synthetic RNA (siRNA). Be sure to include their occurrence in nature, and their mechanism(s) of action.
Describe the difference between exposure and contamination and why this distinction is important. Give an example...
Describe the difference between exposure and contamination and why this distinction is important. Give an example to illustrate your description.
Describe the difference between a service and merchandising firm. Give an example of each from publicly...
Describe the difference between a service and merchandising firm. Give an example of each from publicly traded companies. Give a detailed example of the recording of purchases under a perpetual inventory system including returns, allowances and discounts. Give a detailed example of the recording sales revenues under a perpetual inventory system including returns, allowances and discounts. Give a detailed example of the recording of purchases under a periodic inventory system including returns, allowances and discounts. Give a detailed example of...
Define partial and semi partial correlation and describe the difference between them. Give an example of...
Define partial and semi partial correlation and describe the difference between them. Give an example of each.
Describe the difference between a service and merchandising firm. Give an example of each from publicly...
Describe the difference between a service and merchandising firm. Give an example of each from publicly traded companies. Give a detailed example of the recording of purchases under a perpetual inventory system including returns, allowances and discounts. Give a detailed example of the recording sales revenues under a perpetual inventory system including returns, allowances and discounts. Give a detailed example of the recording of purchases under a periodic inventory system including returns, allowances and discounts. Give a detailed example of...
1-a)What is the difference between an experiment and an observational study? b)Give an example of an...
1-a)What is the difference between an experiment and an observational study? b)Give an example of an experiment. In your example, what population are you studying? 2-a)Define the mode and median of a set of data points. b)Construct a set of 5 data points with a mode of 3. Construct a second set of 5 data points with a median of 5. Clearly indicate which set is which. 3)Construct a frequency distribution using 4 classes for the following set of 10...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT