Abstract Classes Flashcards
What is implementation?
Implementing a method refers to defining the functionality of a method within a class. When you implement a method, you provide the code that defines what the method does when it’s called.
How do you inherit methods from a superclass?
The superclass has methods defined that can be inherited by subclasses.
To do so, use the keyword “extends” after naming the subclass.
Example;
Say you have a superclass called “Animal” which has methods “eat” and “sleep”. These methods simply print out “Animal is sleeping or eating”.
The subclasses are dog and cat with specific methods bark and meow printing dog/cat is barking/meowing respectively.
In the main() method, you can create an instance of the subclasses dog and cat, naming them myCat and myDog.
You can access the methods from the superclass Animal which are eat and sleep. You can access the methods bark from dog and meow from cat. This is inheritance. The methods from Animal are passed down to instances of subclasses dog and cat.
Can you create an object or instance of an abstract superclass by calling it an instance of a concrete class?
Yes….. The professor, in MainClass01.java, wrote
Product product01 = new Book();
where Book() is a concrete class of the abstract superclass Product.
How do you implement abstract methods in a concrete child-class?
Since implementations of abstract methods aren’t done in abstract classes, in child-classes, you have to implement them using the @Override annotation.