Key Concepts

Review core concepts you need to learn to master this subject

Inheritance in Java

// Parent Class class Animal { // Animal class members } // Child Class class Dog extends Animal { // Dog inherits traits from Animal // additional Dog class members }

Inheritance is an important feature of object-oriented programming in Java. It allows for one class (child class) to inherit the fields and methods of another class (parent class). For instance, we might want a child class Dog to inherent traits from a more general parent class Animal.

When defining a child class in Java, we use the keyword extends to inherit from a parent class.

Main() method in Java

// Parent Class class Animal { // Animal class members } // Child Class class Dog extends Animal { // Dog inherits traits from Animal // additional Dog class members }

In simple Java programs, you may work with just one class and one file. However, as your programs become more complex you will work with multiple classes, each of which requires its own file. Only one of these files in the Java package requires a main() method, and this is the file that will be run in the package.

For example, say we have two files in our Java package for two different classes:

  • Shape, the parent class.
  • Square, the child class.

If the Java file containing our Shape class is the only one with a main() method, this is the file that will be run for our Java package.

super() in Java

// Parent Class class Animal { // Animal class members } // Child Class class Dog extends Animal { // Dog inherits traits from Animal // additional Dog class members }

In Java, a child class inherits its parent’s fields and methods, meaning it also inherits the parent’s constructor. Sometimes we may want to modify the constructor, in which case we can use the super() method, which acts like the parent constructor inside the child class constructor.

Alternatively, we can also completely override a parent class constructor by writing a new constructor for the child class.

Protected and Final keywords in Java

// Parent Class class Animal { // Animal class members } // Child Class class Dog extends Animal { // Dog inherits traits from Animal // additional Dog class members }

When creating classes in Java, sometimes we may want to control child class access to parent class members. We can use the protected and final keywords to do just that.

protected keeps a parent class member accessible to its child classes, to files within its own package, and by subclasses of this class in another package.

Adding final before a parent class method’s access modifier makes it so that any child classes cannot modify that method - it is immutable.

Polymorphism in Java

// Parent Class class Animal { // Animal class members } // Child Class class Dog extends Animal { // Dog inherits traits from Animal // additional Dog class members }

Java incorporates the object-oriented programming principle of polymorphism.

Polymorphism allows a child class to share the information and behavior of its parent class while also incorporating its own functionality. This allows for the benefits of simplified syntax and reduced cognitive overload for developers.

Method Overriding in Java

// Parent Class class Animal { // Animal class members } // Child Class class Dog extends Animal { // Dog inherits traits from Animal // additional Dog class members }

In Java, we can easily override parent class methods in a child class. Overriding a method is useful when we want our child class method to have the same name as a parent class method but behave a bit differently.

In order to override a parent class method in a child class, we need to make sure that the child class method has the following in common with its parent class method:

  • Method name
  • Return type
  • Number and type of parameters

Additionally, we should include the @Override keyword above our child class method to indicate to the compiler that we want to override a method in the parent class.

Child Classes in Arrays and ArrayLists

// Parent Class class Animal { // Animal class members } // Child Class class Dog extends Animal { // Dog inherits traits from Animal // additional Dog class members }

In Java, polymorphism allows us to put instances of different classes that share a parent class together in an array or ArrayList.

For example, if we have an Animal parent class with child classes Cat, Dog, and Pig we can set up an array with instances of each animal and then iterate through the list of animals to perform the same action on each.

Inheritance and Polymorphism
Lesson 1 of 1
  1. 1
    When you hear the word “inheritance”, code may not be the first thing that springs to mind; you’re probably more likely to think of inheriting genetic traits, like eye color from your mother or a s…
  2. 2
    So how do we define a child class so that it inherits from a parent class? We use the keyword extends like this: class Shape { // Shape class members } class Triangle extends Shape { // add…
  3. 3
    Hang on, you might be thinking, if the child class inherits its parent’s fields and methods, does it also inherit the constructor? It does indeed, and sometimes this isn’t quite what we want. Let’…
  4. 4
    You may recall that Java class members use private and public access modifiers to determine whether they can be accessed from outside the class. So does a child class inherit its parent’s private m…
  5. 5
    In Java, if Orange is a Fruit through inheritance, you can then use Orange in the same contexts as Fruit like this: String makeJuice(Fruit fruit) { return “Apple juice and “ + fruit.squeeze(); …
  6. 6
    One common use of polymorphism with Java classes is something we mentioned earlier — overriding parent class methods in a child class. Like the + operator, we can give a single method sligh…
  7. 7
    An important facet of polymorphism is the ability to use a child class object where an object of its parent class is expected. One way to do this explicitly is to instantiate a child class object…
  8. 8
    Usually, when we create an array or an ArrayList, the list items all need to be the same type. But polymorphism puts a new spin on what is considered the same type… In fact, we can put instances…
  9. 9
    Excellent work! You’ve learned quite a bundle about inheritance and polymorphism in Java: - A Java class can inherit fields and methods from another class. - Each Java class requires its own file, …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo