In: Computer Science
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.
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