Inheritance Part 1

Inheritance Part 1
Overview
Difficulty
Beginner
Duration
2h 28m
Students
13
Description

In this course, we'll learn about object-oriented programming and its implementations.

Learning Objectives

  • Inheritance
  • Method Overriding
  • Super and This Keywords
  • Final Keyword
  • Abstract Classes
  • Interfaces
  • Polymorphism 
  • Encapsulation

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this section, we'll learn about object oriented programming and its implementations. Let's start with inheritance. So, what is the inheritance in Java, inheritance is a fundamental concept of object oriented programming. It's used when we have is-a relationship between objects. For example, a mountain bike is a bicycle, a road bike is a bicycle, a tandem bike is a bicycle. Different kinds of objects often have a certain amount in common with each other. Let's talk about inheritance through a little real life example. For example, mountain bikes, road bikes and tandem bikes have some common characteristics, such as current speed, current pedal cadence and current gear of the bicycle. 

Yet each of them also can define additional features that make them different. For example, tandem bicycles have two seats and two sets of handlebars, road bikes have dropped handlebars, some mountain bikes have an additional chain ring giving them a lower gear ratio. As a result, object oriented programming allows classes to inherit commonly used states and behavior from other classes. In this example, bicycle now becomes the super class of mountain bike, road bike and tandem bike. In the Java programming language, each class is allowed to have one direct super class and each super class has the potential for an unlimited number of subclasses. The syntax for creating a subclass is simple. 

After your class name use the extends keyword followed by the name of the class to inherit from. You can see the syntax on the slide, this gives mountain bike all the same fields and methods as bicycle, yet it allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. Also, inheritance enables us to reuse existing code or we can say that inheritance supports the concept of reusability I.e. when we want to create a new class and there is already a class that includes some of the code we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class. Now let's look at the other concepts of inheritance and the object class. In Java, all classes can inherit variables and methods from another class. 

A class that is derived from another class is called a subclass. Also, a derived class, extended class or child class. The class from which the subclass is derived is called a super class. Also, a base class or a parent class. Except for object which has no super class every class has one and only one direct super class; Single inheritance. In the absence of any other explicit super class every class is implicitly a subclass of object. Classes can be derived from classes that are derived from classes, that are derived from classes and so on and ultimately derived from the top most class, object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to object. The idea of inheritance is simple but powerful. 

When you want to create a new class and there was already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write and debug them yourself. A subclass inherits all the members, fields, methods and nested classes from its super class. Constructors are not members, so they are not inherited by sub classes, but the constructor of the subclass can be invoked from the subclass. The object class, which is defined in the java.dotlaying package, defines and implements behavior common to all classes, including the ones that you write. In the Java platform, many classes derive directly from object, other classes derive from some of those classes and so on, forming a hierarchy of classes. 

At the top of the hierarchy, object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior. Well, what can we do in a subclass? A subclass inherits all of the public and protected members of its parent no matter what package the subclass is in. If the subclass is the same package as its parent, it also inherits the package private members of the parent. A subclass does not inherit the private members of its parent class. However, if the subclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class, both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all the private members of the super class. 

You can use the inherited members as is replace them, hide them or supplement them with the new members. The inherited fields can be used directly just like any other fields. You can declare a field in the subclass with the same name as the one in the super class, thus hiding it; not recommended. You can declare new fields in the subclass that are not in the super class. The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the super class, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the super class, thus hiding it. 

You can declare new methods in the subclass that are not in the super class. You can write a subclass constructor that invokes the constructor of the super class either implicitly or by using the keyword super. Now let's look at the casting of an object in inheritance. We have seen that an object is the data type of the class from which it was instantiated. For example, if we write mountain bike, my bike equals new mountain bike, then my bike is of type mountain bike. Mountain bike is descended from bicycle and object therefore a mountain bike is a bicycle and is also an object and it can be used wherever bicycle or object objects are called for. But the reverse is not necessarily true. A bicycle, maybe a mountain bike but it isn't necessarily similarly an object maybe a bicycle or a mountain bike, but it isn't necessarily. 

Casting shows the use of an object of one type in one place of another type among the objects permitted by inheritance and implementations. For example, if we write object myObject equal new mountainBike, then myObject is both an object and a mountainBike until such time as myObject is assigned another object that is not a mountainBike. This is called implicit casting. If on the other hand, we write mountainBike, myBike equal myObject, we would get a compile time error because myObject is not known to the compiler to be a mountainBike. However, we can tell the computer that we promised to assign a mountainBike to myObject by explicit casting. You can see it on the slide. This cast inserts a runtime check that myObject is assigned a mountainBike so that the compiler can safely assume that myObject is a mountainBike. 

If the myObject is not a mountainBike at runtime an exception will be thrown. In this case, you can make a logical test as to the type of a particular object using the instance of operator. This can save you from a runtime error owing to an improper cast. For example, you can create an if statement like that. If myObject is an instance of the mountainBike, you can assign myObject to myBike with the explicit casting. Here, the instance of operator verifies that myObject refers to a mountainBike so that we can make the cast with the knowledge that there will be no runtime exception thrown. Yes, we have given a lot of information about inheritance so far but we haven't put it into practice yet. Let's take a short break here, in our next lesson, we'll practice inheritance. See you in the next video.

 

About the Author
Students
72
Courses
18
Learning Paths
1

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics