TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Extends: Class Inheritance

Use the extends keyword to implement inheritance on a class. One class extends another.
Extends. A class can be based upon another (derived from it). This requires the "extends" keyword in the class declaration. One class extends another.
No multiple inheritance. Java does not allow a class to extend multiple other classes. This reduces problems with inheriting state, like fields, from many parents.Class
A first example. Let us begin with a simple "extends" example. Here class B extends class A. Usually longer, more descriptive class names are best.

Note: In main we create a new instance of B but treat it as an A reference. We use a method from A, but the B method is selected.

Call: This method is implemented on both A and B. The most derived B implementation is used when an A reference is acted upon.

Java program that uses derived class class A { public void call() { System.out.println("A.call"); } } class B extends A { public void call() { System.out.println("B.call"); } } public class Program { public static void main(String[] args) { // Reference new B class by A type. A a = new B(); // Invoke B.call from A class instance. a.call(); } } Output B.call
Example 2. Here we have an Animal class with one method. We then have a Dog class that derives from Animal. In main() we can use one method on Animal.

Dog: On the Dog class we can use the methods from Animal and Dog. So the dog can breathe() and bark().

Note: We find we can instantiate parent classes, or child classes, directly. The best class depends on what your program is doing.

Java program that uses extends class Animal { public void breathe() { System.out.println("Breathe"); } } class Dog extends Animal { public void bark() { System.out.println("Woof"); } } public class Program { public static void main(String[] args) { // On an animal we can call one method. Animal animal = new Animal(); animal.breathe(); // On a dog we can use Animal or Dog methods. Dog dog = new Dog(); dog.breathe(); dog.bark(); } } Output Breathe Breathe Woof
Multiple inheritance. We cannot use "extends" on multiple classes. A class can only inherit from one class. This avoids problems where fields (state) is inherited from many classes at once.

Quote: One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes.

Multiple Inheritance: oracle.com
In extending classes, we gain the parent's fields and methods. And we add in all that parent's extended classes. We compose complex and powerful programs.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf