inheritance Flashcards
Inheritance 1
Inheritance is the process by which the new child subclass automatically.
You need to use the keywords “extends” in order to use inheritance
Inheritance 2
There are two classes involved to do inheritance.
One is called the parent class. Also called super class
one is called the child class. Also called the base class
Inheritance demo
Here Childclass is extending the parentclass.
everytime there is an inheritance, the child class can inherit these methods from the parent class.
It only works one way not the oher way around
Single inheritance
Java supports single inheritance, by which a class may inherit from only one direct parent class.
By design, Java doesn’t support
multiple inheritance in the language because studies have shown that multiple inheritance
can lead to complex, often difficult-to-maintain code.
but you can do a work around by implementing multiple interfaces.
Java also supports multiple levels of inheritance, by which one class may extend
another class, which in turn extends another class. You can extend a class any number of
times, allowing each descendent to gain access to its ancestor’s members.
Show DEMO
multiple level of inheritance
Abstraction
an abstract class may include nonabstract methods and variables
In fact, an abstract class is not required to
include any abstract methods.
you make abstract method with with keyword abstract.
you can only make abstract mthod inside an abstract class.
If a subclass fails to override the method, an error will result. Abstract methods are used to
ensure that a subclass implements the method.
Like inheritance, you should extend an abstract class.
Abstract method
Abstract methods and abstract classes are defined with the abstract key word.
* Abstract methods have no body, and their header must end with a semicolon.
* An abstract method must be overridden in a subclass.
* When a class contains an abstract method, it cannot be instantiated. It must serve as
a superclass.
* An abstract class cannot be instantiated. It must serve as a superclass.
interfaces
Although Java doesn’t allow multiple inheritance, it does allow classes to implement any
number of interfaces. An interface is an abstract data type that defi nes a list of abstract
public methods that any class implementing the interface must provide.
You implement interfaces using the keyword implements.
overriding
Sometimes a subclass inherits a method from its superclass, but the method is inadequate
for the subclass’s purpose. Because the subclass is more specialized than the superclass, it is
sometimes necessary for the subclass to replace inadequate superclass methods with more
suitable ones. This is known as method overriding.