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

1. when you create a class by making it inherit from another class, the new class...
1. when you create a class by making it inherit from another class, the new class automatically, contains the data fields and _____ of the original class a. fonts b. methods c. class names d. arrays 2. if a programming language does not support ________, the language is not considered object oriented a. syntax b. applets c. loops d. polymorphism 3. when you create a class and do not provide a ______, java automatically supplies you with a default one...
Which of the following situations described is a good situation to use an abstract class, and...
Which of the following situations described is a good situation to use an abstract class, and where it would be better to use than either a concrete class or an interface? When some methods in the class are abstract and some are concrete, and some variables in the class need to be private, while other variables need to be public When the inheriting class is closely related to the abstract class, such as with an "is-a" relationship When the objects...
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.
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?
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 {...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT