Question

In: Computer Science

A class variable is visible to and shared by all instances of a class. How would...

  1. A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application?

  1. Describe the difference between abstract classes and concrete classes, giving an example of each.

  1. Explain how data are encapsulated and information is hidden in Java?

  1. Explain the difference between a class and an interface in Java, using at least one example.

Solutions

Expert Solution

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.

abstract class DemoAbstractClass {

    abstract void display();

}

public class JavaApplication {

    public static void main(String[] args)

    {

        DemoAbstractClass AC = new DemoAbstractClass() {

            void display()

            {

                System.out.println("Hi.");

            }

        };

        AC.display();

        System.out.println("How are you?");

    }

}

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.

abstract class DemoAbstractClass {

    abstract void display();

}

class ConcreteClass extends DemoAbstractClass {

    void display()

    {

        System.out.println("Hi.");

    }

}

public class JavaApplication {

    public static void main(String[] args)

    {

        ConcreteClass C = new ConcreteClass();

        C.display();

        System.out.println("How are you?");

    }

}

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.


Related Solutions

swift language declare a Swift array variable that can hold instances of any class type
swift language declare a Swift array variable that can hold instances of any class type
Task 4 [10 pts] Broadcasting Messages to All Clients (Shared Variable Among Threads) Modify the server...
Task 4 [10 pts] Broadcasting Messages to All Clients (Shared Variable Among Threads) Modify the server code so that when the server sends a message, that message is sent to all connected clients #include<stdio.h> #include<string.h> //strlen #include<stdlib.h> //strlen #include<sys/socket.h> #include<arpa/inet.h> //inet_addr #include<unistd.h> //write #include<pthread.h> //for threading , link with lpthread #define MAX 80 #define PORT 6543 #define SA struct sockaddr // Function designed for chat between client and server. void join( pthread_t *thread_id) { if(!pthread_join( *thread_id , NULL)) printf("thread %ld...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on Program 13-1 (see attachment Pr13-1.cpp) in the textbook, create a class name “Circle” with the following declarations (hint: you can use PI=3.14): //Circle class declaration class Circle { private: double radius; public: void setRadius(double); double getRadius() const; double getArea() const; double getPerimeter() const; }; 2: Based on Program 13-2 (see attachment Pr13-2.cpp) in the textbook, create two instances of the Circle class, pizza1 and...
Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each...
Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each Clown only needs to be identified by a String for her/his name. Always join a new Clown to the end of the Parade. Only the Clown at the head of the Parade (the first one) can leave the Parade. Create a test application to demonstrate building a parade of 3 or 4 clowns (include your own name), then removing 1 or 2, then adding...
5. Find all the wavelengths of visible light emitted when Hydrogen is heated. Approximate visible light...
5. Find all the wavelengths of visible light emitted when Hydrogen is heated. Approximate visible light to have the range from 400 nm to 700 nm. No points for only telling the wavelengths. Points will be for showing how you found these wavelengths (mention the transitions).
How does one declare a class variable in C#?
How does one declare a class variable in C#?
How many colors are in the visible spectrum?
How many colors are in the visible spectrum?
1. A)how would parallax effect a measurement 1. B) Give several instances when error can be...
1. A)how would parallax effect a measurement 1. B) Give several instances when error can be introduced while making a measurement. 1.C) Elaborate on the statement that "all measurements have a degree of uncertainty".
(1 point) Given the results of the in-class visible minority survey, an employment equity survey is...
(1 point) Given the results of the in-class visible minority survey, an employment equity survey is likely to _______________ over-estimate, correctly estimate, under-estimate the percent of a workforce that is composed of persons from visible minorities. (1 point) A job can be identified as belonging to a male job class if ___________ percent of the employment are men. (1 point) The Indian system of dowry ________________ ( is / is not) based on religion, and _____________ (does / does not)...
Explain how shared, conserved, fundamental processes and features support the concept of common ancestry for all organisms.
Explain how shared, conserved, fundamental processes and features support the concept of common ancestry for all organisms. (Use the terms below)Genetic informationribosomesGenetic codeMetabolic pathways
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT