0% found this document useful (0 votes)
44 views3 pages

Abstract Class Concepts in Java

The document contains 15 questions related to abstract classes, inner classes, and static methods in Java. Question 1 asks about the output of a code sample that creates an instance of class B which extends class A, and prints the values of inherited fields. The correct answer is 1 2. Question 2 asks which statement about abstract classes is false; the answer is that an abstract class can be instantiated with the new operator.

Uploaded by

Chirag Ahuja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

Abstract Class Concepts in Java

The document contains 15 questions related to abstract classes, inner classes, and static methods in Java. Question 1 asks about the output of a code sample that creates an instance of class B which extends class A, and prints the values of inherited fields. The correct answer is 1 2. Question 2 asks which statement about abstract classes is false; the answer is that an abstract class can be instantiated with the new operator.

Uploaded by

Chirag Ahuja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Abstract, inner, static classes questions

1. What will be the output of the following Java code?

2. class A
3. {
4. public int i;
5. public int j;
6. A()
7. {
8. i = 1;
9. j = 2;
10. }
11. }
12. class B extends A
13. {
14. int a;
15. B()
16. {
17. super();
18. }
19. }
20. class super_use
21. {
22. public static void main(String args[])
23. {
24. B obj = new B();
25. [Link](obj.i + " " + obj.j)
26. }
27. }

a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error

2. Which of these is not a correct statement?


a) Every class containing abstract method must be declared abstract
b) Abstract class defines only the structure of the class not its implementation
c) Abstract class can be initiated by new operator
d) Abstract class can be inherited

3. Which of the following is FALSE about abstract classes in Java


(A) If we derive an abstract class and do not implement all the abstract
methods, then the derived class should also be marked as abstract using
‘abstract’ keyword
(B) Abstract classes can have constructors
(C) A class can be made abstract without any abstract method
(D) A class can inherit from multiple abstract classes.

4. Can We Instantiate an Abstract Class in Java?

5: What is Inner Class in Java? Explain with help of an example.

6: Why can outer Java classes access inner class private members?

7. What is the main difference between static and non-static nested classes? Also provide an
example for each of them?

8. What are member inner classes in java?

9. Create an abstract class 'Animals' with two abstract methods 'cats' and 'dogs'. Now create a class 'Cats'
with a method 'cats' which prints "Cats meow" and a class 'Dogs' with a method 'dogs' which prints "Dogs
bark", both inheriting the class 'Animals'. Now create an object for each of the subclasses and call their
respective methods.

10. what is the output of this question?


class Test1 {
public
    static void main(String[] args)
    {
        int x = 20;
        [Link](x);
    }
    static
    {
        int x = 10;
        [Link](x + " ");
    }
}

Option
A) 10 20 ..
B) 20 10
C) 10 10
D) 20 20

11. Why non-static variable cannot be referenced from a static method in Java?
12. What will happen if we do not override all abstract methods in
subclass?

13. What will happen if we do not provide implementation for all


abstract methods in subclass?

14. Why abstract class has constructor even though you cannot create
object?

15. Why should we create reference to superclass (abstract class


reference)?

Common questions

Powered by AI

Inner classes in Java can access private members of the enclosing outer class because the Java language specifies that inner classes have access to the lexical scope of their enclosing class. This means they can use private variables and methods of the outer class as if they were their own . While this enhances the utility and flexibility of the language, it presents complications in terms of encapsulation, as it opens up access pathways that break traditional encapsulation barriers .

Abstract classes in Java provide a way to define classes that cannot be instantiated on their own; they can only be used as a superclass. They can contain abstract methods, which must be implemented by non-abstract subclasses. If a subclass does not implement all the abstract methods, it must also be declared abstract . Abstract classes can have constructors and can inherit from other classes, allowing them to be part of an inheritance hierarchy. However, a class cannot inherit from multiple abstract classes .

A class with at least one abstract method must be declared abstract because it cannot provide complete implementation and relies on subclasses to fulfill that role . This requirement ensures that the compiler enforces the implementation of all abstract methods before allowing objects of the class to be created. If the subclass doesn't implement these methods, it too must be declared abstract, further propagating the requirement until a concrete class implements all abstract methods .

Member inner classes in Java are defined inside a class and are associated with an instance of the outer class. They have access to the encompassing class's private members. An example would be a 'Graph' class with an inner class 'Node', where 'Node' can access and modify the graph's data structure. These can be particularly useful when modeling entities that are tightly associated, where the inner class needs access to the outer class's data easily and frequently .

Abstract classes in Java can have constructors, and these constructors are used to ensure fields are correctly initialized when inherited. Although you cannot instantiate an abstract class directly, when a concrete subclass is instantiated, the constructor chain starts from the abstract class constructor down through the hierarchy . This is crucial for establishing initial states and behaviors defined at the abstract level. A common misconception is that abstract classes don't need constructors because they can't produce objects directly, yet constructors are fundamental in passing necessary parameters and setting initial values for subclasses .

Static nested classes in Java do not require an instance of the outer class; they can access static variables and methods but not instance variables or methods. This makes them suitable for use cases similar to static methods where instance-level context is not required. Non-static inner classes can access both static and non-static members of the outer class and require an instance of the outer class to be instantiated . Developers should use static nested classes when the task is more related to the class than to a specific instance and use non-static inner classes when there's a need to interact closely with an instance of the outer class .

Consider an abstract class 'Animals' with abstract methods 'makeSound' and 'move'. Subclasses 'Bird' and 'Mammal' implement these methods differently—'Bird' might use 'makeSound' to return chirping and 'move' for flying, while 'Mammal' might return a bark and running respectively. By utilizing polymorphism, a method in a zoo management system could dynamically interact with either 'Bird' or 'Mammal' objects, invoking their specific implementations of 'makeSound' and 'move'. This setup allows flexibility and extendability, as new animal types can be added with ease by simply creating subclasses that implement the abstract methods, ensuring diverse animal behaviors are encapsulated and utilized correctly .

If a subclass does not implement all abstract methods of its superclass, it must itself be declared abstract. This is because incomplete method implementation would leave the class with undefined behavior if instantiated . To resolve this, the subclass must be further extended by another class that implements the remaining abstract methods, or directly implement the missing methods to become a concrete class .

In Java, static initialization blocks are executed once when the class is loaded, before any instances of the class are created. Non-static/instance initialization blocks are executed each time an object is created, right after the constructor is called. If a class example contains a static block setting a variable, followed by a `main` method that prints a variable with the same name, the static block will run first and its changes will be overwritten by the initialization in the `main` method. Hence, for the provided example, the expected output would be '10 20', as the static block assigns '10', but '20' is finally printed from the `main` block .

Non-static variables cannot be referenced from static methods because static methods belong to the class itself rather than any instance of the class. Since an instance is necessary to determine which specific non-static variable value is being referenced (as they can differ from instance to instance), accessing non-static members in a context devoid of such instance references is restricted. This design ensures a clear separation between class-wide features and instance-specific features, simplifying the model for understanding and predicting the behavior of Java programs .

You might also like