Question

In: Computer Science

1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...

1. Can you extend an abstract class? In what situation can you not inherit/extend a class?
2. Can you still make it an abstract class if a class does not have any abstract methods?

Solutions

Expert Solution

First of all, let's understand the meaning of Abstract class.

Abstract class:

  • An abstract class is a class that is declared abstract.
  • It may or may not include abstract methods.
  • It cannot be instantiated, but they can be subclassed.

Answer - Question: 1

Yes, we can extend an abstract class.

abstract class Animal{
   public abstract void sound();
}


public class Dog extends Animal{

   public void sound(){
        System.out.println("Woof");
   }
}
  • Any class inheriting the current class must either override the abstract method or declare itself as abstract.
  • So, if we are not overriding, we can't inherit/extend.

Answer - Question: 2

  • Yes, we can have an abstract class without Abstract Methods as both are independent concepts.
  • Declaring a class abstract means that it can not be instantiated on its own.
  • Declaring a method abstract means that Method will be defined in the subclass.
public abstract class AbstractClass{

    public String nonAbstractMethod1(int a, int b){
        int c = a + b;
        return c;
    }

    public static void nonAbstractMethod2(int c){
        System.out.println("Value of c is "+ c);
    }
}

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

C++ Programming 1) What is the feature that makes a base class an abstract class? 2)...
C++ Programming 1) What is the feature that makes a base class an abstract class? 2) When designing a class hierarchy involving classes A and B, what are the two questions that must be asked to determine whether class B should be derived from class A?
Explain why you cannot inherit an oncogene gene mutation but can inherit a tumor suppressor gene...
Explain why you cannot inherit an oncogene gene mutation but can inherit a tumor suppressor gene mutation.
Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's...
Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's cart. Items of Type T can be added * or removed from the cart. A hashmap is used to keep track of the number of items * that have been added to the cart example 2 apples or 4 shirts. * @author Your friendly CS Profs * @param -Type of items that will be placed in the Cart. */ public abstract class AbstractCart {...
You inherit $1000 from your Mother. 1) What do you do with the money? What do...
You inherit $1000 from your Mother. 1) What do you do with the money? What do you spend it on? Do you save any of it? 2) Calculate your MPS and MPC. Show your work. 3) Calculate your Multiplier.    Show your work. 4) What effect does your spending have on GDP - how much additional spending is credited?
A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.
1. Java Abstract Class (2 pts) () • A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.• Following is an example of abstract class:abstract class Book{String title;abstract void setTitle(String s);String getTitle(){return title;}}If you try to create an instance of this class like the following line you will get an error:Book...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters minimum and percent. It should define the method computeDiscount so that if the quantity purchased of an item is more then minimum, the discount is percent percent.
Part I: The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class.
  Part I:   The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class. * Update the function display() to be a virtual function     * Add function, double weeklyEarning() to be a pure virtual function. This will make Employee an abstract class, so your main will not be able to create any Employee objects. Part II: The Derived Classes Add an weeklyEarning() function to each...
// FILE: table1.h // // ABSTRACT BASE CLASS: Table //    1. The number of records...
// FILE: table1.h // // ABSTRACT BASE CLASS: Table //    1. The number of records in the Table is stored in total_records // 2. The hashcode function returns a location in the table for the // input key. It calls hash function in functional library. // 3. insert and print are two virtual functions to be overridden // insert: Add a new record into the Table; //           If key is already in the table, do nothing //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT