$show=/label

Java Extends Keyword in Depth for Beginners

SHARE:

A quick guide to extends keyword in java with examples.

1. Overview

In this tutorial, we'll learn how to use extends keyword in java with examples.

Extends keyword is used only in the inheritance chain in java and it cannot be used in any other places.

In Java, Interfaces and classes can use extends keyword.

All examples are shown in this article are available on GitHub at the end of the article.

Java Extends Keyword



2. What is extends keyword?


Extends keyword is one of the reserved words in java and this can not be used as an identifier.

extends is used to define the inheritance relationship between the classes. Inheritance is one of the Object-oriented programming concepts which is used to define the relationship among the classes to acquire the properties of the other class.

Java extends keyword indicates that child class gets all properties and methods of the parent class. So, the child class has the access to all methods and instance variables from the parent class.

Sometimes parent class can be called superclass or base class.

Java extends keyword is used to define the IS-A relationship in java.

3. Java Extends on Class With Examples


Always class name must be provided after extends keyword when extends is used on the class.

Below example, Car is a superclass and HondaCar is the subclass. This child-parent relation is established with the help of extends keyword.


Example
package com.javaprogramto.keywords.extend;

public class ClasesExtendsExamples {

	public static void main(String[] args) {

		HondaCar hondaCar = new HondaCar();
		int noOfWheels = hondaCar.getNoOfWheels();
		System.out.println("Honda car wheels : " + noOfWheels);

	}
}

class Car {

	int noOfWheels = 4;
}

class HondaCar extends Car {

	public int getNoOfWheels() {
		return this.noOfWheels;
	}
}

Output
Honda car wheels : 4

From the above example, noOfWheels is declared and initialized in the parent class and could access the same variable from the child class getNoOfWheels() method.

So, extends keyword could create the connection between Car and HondaCar classes.

A class cannot extend more than one class in java that indicates java does not support multiple inheritances.

Hence, we can not provide multiple class names after extends keyword. If we use it so, it will give compile time error. 

class HybridCar extends Car , HondaCar{

}

The above code does not work.

But, Java provides support for the hierarchical inheritance. So that multiple classes can extends the same class.

class Car {

	int noOfWheels = 4;
}

class HondaCar extends Car {

}

class SkodaCar extends Car {

}

4. Java Extends on Interfaces With Examples


Interfaces also can use the extends keyword. 
Java extends keyword on interfaces provides the extension to the parent interface.
This concept works as similar to the class extends another class.
One interface can use the extends keyword to inherit another interface.

But one class can not extend the interface by using extends keyword.

interface Payment {
	
	boolean processPayment();
	
}

interface CreditCardPayment extends Payment{
	
	void validateCardDetails();
}

interface UPIPayment extends Payment{
	
	void validateUPIAddress();
}


5. Extending Final Classes In Java


As we have seen in the above sections, always we have used extends keyword on non-final classes. 
But we can use the final non-access modifier on the classes.

If a class is declared as final then we can not extend or inherit it using extends keyword.
If we attempt to create the child classes to the final class then it will give the compile time error.

We can declare the class as final when we do not want other classes to inherit its properties.

Example
final class Calculator {

	public int sum(int a, int b) {
		return a + b;
	}

	public int substract(int a, int b) {
		return a - b;
	}

	public int multiply(int a, int b) {
		return a * b;
	}

	public int divide(int a, int b) {
		return a / b;
	}

	public int reminder(int a, int b) {
		return a % b;
	}

}

class MyCalculator extends Calendar{
	
	
}
Output
The type MyCalculator must implement the inherited abstract method Calendar.add(int, int)

6. Conclusion


In this article, we've seen how to use extends keyword with classes and interfaces in java along with examples.


COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java Extends Keyword in Depth for Beginners
Java Extends Keyword in Depth for Beginners
A quick guide to extends keyword in java with examples.
https://blogger.googleusercontent.com/img/a/AVvXsEjNkbCjEwAUQxVIvrjhh0dBBn9jPe2wp1lrn95wsZ5VPsdc-7KoMfQ7linCpIsNOol6SNdKksPvZa5MSYb1qIOhH6rUUANyjxAe-gV7lICIpwDwNAVGnbXGz9CqGOmUJXsgi_eONCUS2tiGTP3Fc7blyGWlbLPVzAiPc7M7eXwcnyLxWMdq9lIgZj-3=w400-h230
https://blogger.googleusercontent.com/img/a/AVvXsEjNkbCjEwAUQxVIvrjhh0dBBn9jPe2wp1lrn95wsZ5VPsdc-7KoMfQ7linCpIsNOol6SNdKksPvZa5MSYb1qIOhH6rUUANyjxAe-gV7lICIpwDwNAVGnbXGz9CqGOmUJXsgi_eONCUS2tiGTP3Fc7blyGWlbLPVzAiPc7M7eXwcnyLxWMdq9lIgZj-3=s72-w400-c-h230
JavaProgramTo.com
https://www.javaprogramto.com/2021/12/java-extends-keyword.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/12/java-extends-keyword.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content