Java Tutorial 120 lessons · 2:32:01+ of video
Java Home What Java Is, Where Java Is Used and How Java Runs Your Code
What Java Is Where Java Is Used How Java Runs Your Code Installing the JDK, Trying Java in JShell and Your First Program
Installing the JDK Trying Java in JShell Your First Program The main Method, println and print, and Comments
The main Method println and print Comments Variables, Primitive Types, int and long
Variables Primitive Types int and long double and float, Arithmetic, Division and Remainder
double and float Arithmetic Division and Remainder char and boolean, Type Casting, Practice: A Simple Calculator
char and boolean Type Casting Practice: A Simple Calculator Strings, Joining Strings and String Methods
Strings Joining Strings String Methods Comparing Strings, Escapes and Text Blocks, String.format
Comparing Strings Escapes and Text Blocks String.format Reading Input with Scanner, Reading Numbers, Practice: A Receipt
Reading Input with Scanner Reading Numbers Practice: A Receipt Comparison Operators, if, else and else if
Comparison Operators if else and else if Logical Operators, The Ternary Operator and switch
Logical Operators The Ternary Operator switch Switch Expressions, Nested Decisions, Practice: Grade Checker
Switch Expressions Nested Decisions Practice: Grade Checker while Loops, do while and for Loops
while Loops do while for Loops break and continue, Nested Loops and Loop Patterns
break and continue Nested Loops Loop Patterns Counting and Summing, Infinite Loops and Off-by-One, Practice: Times Table
Counting and Summing Infinite Loops and Off-by-One Practice: Times Table What an Array Is, Creating Arrays and Looping Over Arrays
What an Array Is Creating Arrays Looping Over Arrays The Enhanced for Loop, The Arrays Class and Searching an Array
The Enhanced for Loop The Arrays Class Searching an Array Two-Dimensional Arrays, Common Array Mistakes, Practice: Top Scores
Two-Dimensional Arrays Common Array Mistakes Practice: Top Scores Defining Methods, Parameters and Return Values
Defining Methods Parameters Return Values Overloading, Scope and Pass by Value
Overloading Scope Pass by Value Recursion, Varargs, Practice: Temperature Converter
Recursion Varargs Practice: Temperature Converter Classes and Objects, Fields and Constructors
Classes and Objects Fields Constructors The this Keyword, Instance Methods and static
The this Keyword Instance Methods static Access Modifiers, Getters and Setters, and toString
Access Modifiers Getters and Setters toString Encapsulation, Inheritance and super
Encapsulation Inheritance super Overriding, Polymorphism and Abstract Classes
Overriding Polymorphism Abstract Classes Interfaces, Records and Enums
Interfaces Records Enums ArrayList, Generics and Wrapper Classes
ArrayList Generics Wrapper Classes and Autoboxing HashMap, Looping Over a Map and HashSet
HashMap Looping Over a Map HashSet Sorting with Comparator, Removing While Looping, Practice: Word Counter
Sorting with Comparator Removing While Looping Practice: Word Counter What Exceptions Are, try and catch, and finally
What Exceptions Are try and catch finally Checked and Unchecked, throw and Custom Exceptions, try-with-resources
Checked and Unchecked throw and Custom Exceptions try-with-resources Writing Files, Reading Files, Practice: Save and Load
Writing Files Reading Files Practice: Save and Load Lambdas, Functional Interfaces and Method References
Lambdas Functional Interfaces Method References Streams, filter, map and collect, More Stream Operations
Streams filter, map and collect More Stream Operations Optional, var and Pattern Matching
Optional var Pattern Matching StringBuilder, Dates and Times, Packages and Imports
StringBuilder Dates and Times Packages and Imports Project: The Plan, The Task Record and The TaskList Class
Project: The Plan Project: The Task Record Project: The TaskList Class Project: Saving Tasks, Loading Tasks and Reports with Streams
Project: Saving Tasks Project: Loading Tasks Project: Reports with Streams Project: The Menu, Where to Go Next and Course Recap
Project: The Menu Where to Go Next Course Recap Java · Lesson 74 of 120
Inheritance Build on another class.
⏱ 2 min read ▶ Video at 1:32:59 🎓 Free
▶ starts at 1:32:59 Many classes share things. Dogs and cats are both animals. They both have a name, and they both eat. Inheritance lets one class build on another, and reuse its code.
Shared features Dogs and cats are animals Reuse the code Animal is the parent class, also called the superclass. It has a name, and an eat method. The name is protected, so child classes can use it too.
Main.java java Copy
public class Animal {
protected String name;
public Animal (String name ) { this .name = name; }
public void eat () {
System.out. println (name + " is eating." );
}
} Extends Animal means a dog is an animal. Dog gets the name, and eat, for free. It adds its own method, fetch, that only dogs have.
Main.java java Copy
public class Dog extends Animal {
public Dog (String name ) { super (name); }
public void fetch () {
System.out. println (name + " fetches the ball!" );
}
} Rex can eat, inherited from Animal, and fetch, from Dog. A plain animal can eat, but can't fetch. Instance of checks whether an object is a kind of something. Run it. Rex eats, and fetches. And rex is indeed an animal.
Main.java java Copy
public class Main {
public static void main ( String [] args ) {
Dog rex = new Dog ( "Rex" );
rex. eat ();
rex. fetch ();
Animal generic = new Animal ( "Some animal" );
generic. eat ();
System.out. println (rex instanceof Animal);
}
} Output
Rex is eating.
Rex fetches the ball!
Some animal is eating.
true Also uses Animal.java public class Animal {
protected String name;
public Animal (String name ) { this .name = name; }
public void eat () {
System.out. println (name + " is eating." );
}
} Also uses Dog.java public class Dog extends Animal {
public Dog (String name ) { super (name); }
public void fetch () {
System.out. println (name + " fetches the ball!" );
}
} Prefer to watch? The full Java course is free All lessons in one video with animations, real code and chapters.
Watch free A class extends exactly one parent. The child gets the parent's fields and methods, and can add its own. Every class in Java secretly extends a class called Object. That's where to string comes from.
key value extends one parent only Child gets fields and methods Child adds its own extras Top of the tree Object
A child class extends a parent, gets its fields and methods, and adds its own. It's an is a relationship. Next, the super keyword.
Key takeaways class Dog extends Animal Reuse code is-a Next up:
super — talk to the parent