Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Why are abstract class constructors defined as protected? 3. Class X defines

ID: 3562867 • Letter: 2

Question

2. Why are abstract class constructors defined as protected?

3. Class X defines method M1( ) as abstract. Class Y inherits from Class X and implements the method M1( ). Given the following code, explain why a reference to the abstract class X is allowed to refer to an object of the concrete class Y. Explain what happens when the reference to the abstract class X is used to execute method M1( ).

X obj = new Y( );

obj.M1( );

4., What are interfaces allowed to contain?

5. Define a simple interface named Motorized with one method (maxSpeed) which returns the maximum attainable speed and another method (fuelType) which returns the type of fuel required.

6. Given the interface defined in question 5, if a class named Motorcycle implements this interface, explain why the following code is legal. What happens when the interface reference vehicle invokes the maxSpeed method?

Motorized vehicle = new Motorcycle( );
System.out.print(

Explanation / Answer

1) A class that contains abstract methods is called Abstract class.
An abstract method is a method that does not have a body i.e Implementation

   Class must contains all Implemented methods ( no - abstract methods)
   Abstract class can contains abstaract methods also.

  

Note :


   An abstract class may or may not contain abstract methods.

   Normal Class can be instantiated i.e Object creation is Posible.
   Where as Abstract class can not be instantiated. (But it can hold the child class Objects)
  

2) As we know that Abstract classes can not be instantiated, so to use the Constructor of Abstract class we have to inherit it.

So the Posible modifier is PROTECTED for Abstract class.

Note:

Protected modifier scope is : Same package with Sub classes

3)

abstract class X
{
public abstract void M1();
  
}

class Y extends X
{
public void M1()
{
System.out.println("Hii");
}
}

In Java, if we have abstract method in class we Must declare that class as Abstract class.

In java abstract class cann't be instantiated i.e we can not create Object for the abstract class.

But we can create the Object for sub classes or child classes of Abstract class.

So by creating the Object of sub class we can assign this Object to Base class i.e Abstract class.

Finally Abstract class cann't be instantiated but it can hold the Sub class Object.


We know that Runtime Polymorphism works based on the Instance (Object) rather than which class reference is hold.
So When the reference to the abstract class X is used to execute method M1( ), It will execute the method M1() defined in class Y.
because of the Runtime polymorphism.

X obj = new Y( );

obj.M1( );

Here obj is reference variable of abstract class X but the Instance ( Object ) is of class Y
so it executes the M1() of class Y.


4)

An interface contains following
  - Methods
   - variables

   * variables of interface are by default "private static final" whether you declared or Not
   * Methods are public and abstract.

   Interface contains all abstract methods Only.


5)

interface Motorized
{
   public abstract int maxSpeed();
   public abstract String fuelType();

}

6)

Eventhrough Interface can not be instantiated but it can hold the Implemetation class( the one implements the interface) Object.
In Java based on Runtime polymorphism JVM will select the method based on the Instantiation ( Object that holds)
     
   Motorized vehicle = new Motorcycle( );
System.out.print(

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote