In: Computer Science
A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application?
Describe the difference between abstract classes and concrete classes, giving an example of each.
Explain how data are encapsulated and information is hidden in Java?
Explain the difference between a class and an interface in Java, using at least one example.
A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application?
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.
public class Product {
public int Barcode;
}
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.
public class Product {
public static int Barcode;
}
Describe the difference between abstract classes and concrete classes, giving an example of each.
Abstract Class:
An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along with the new statement. It may or may not contain an abstract method. An abstract method is declared by abstract keyword, such methods cannot have a body. If a class contains an abstract method, then it also needs to be abstract.
Example: Valid instantiation by defining all abstract method of an abstract class.
|
Concrete Class:
A concrete class in Java is a type of subclass, which implements all the abstract method of its super abstract class which it extends to. It also has implementations of all methods of interfaces it implements.
Example: Direct instantiation of concrete using new keyword.
|
Explain how data are encapsulated and information is hidden in Java?
Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
To achieve encapsulation in Java −
Declare the variables of a class as private.
Provide public setter and getter methods to modify and view the variables values.
Explain the difference between a class and an interface in Java, using at least one example.
Class:
A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:
1. Modifiers : A class can be public or has default access (Refer this for details).
2. Class name: The name should begin with a initial letter (capitalized by convention).
3. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
4. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
5. Body: The class body surrounded by braces, { }.
Constructors are used for initializing new objects. Fields are variables that provides the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.
Interface:
Like a class, an interface can have methods and variables, but the
methods declared in interface are by default abstract (only method
signature, no body).
1. Interfaces specify what a class must do and not how. It is the blueprint of the class.
2. An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
3. If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract.