Inheritance using 100% Abstraction

Note: Please complete the notes below before you can watch the video.

Inheritance :

There are two ways we can achieve inheritance in Java.

  1. Implementing an Interface
  2. Extending a class

Interfaces in Java :

What is an Interface ?

In Java an interface is a reference type just like a class but unlike class an interface can only contain methods without body i.e abstract methods.  To create the interface we use keyword interface  just like we use  keyword class to create a class.

Example :

flyable

The purpose of creating an interface is to inherit in one or other class. We can inherit an interface in a class using the keyword implements. If a concrete class ( a class which is not abstract) implements an interface then it has to provide implementation for all the abstract methods that are declared in an interface or else we will get a compile time error.

For example :

jet

Understanding the Interface Concept :

video

Practical part of the concept explanation of the above video :

video

If a concrete class implements an interface then it has to provide the implementation for all the abstract methods present in that interface because :

aeroplane-class

Important points about interface :

  1. All of the methods in an interface are abstract, so its optional to use the keyword abstract in front of method declaration.
  2. An interface can not have concrete methods but it can have default implementation ( JDK 1.8 feature, Explained in interview questions video ).
  3. All the members inside an interface get public access specifier , hence it is optional to specify the keyword public. Members inside and interface can not be private or protected.
  4. An interface cannot contain instance fields. The only fields that can appear inside an interface must be declared as both static and final. Variable created inside an interface will automatically converted to final static fields internally.
  5. An interface does not contain any constructor.
  6. An interface can not be instantiated but we can create an object of type interface ( Explained in IS-A Relationship lesson ).
  7. An interface can extend another interface just the way a class can extend another class. But a class implements an interface.
  8. An interface can extend multiple interfaces.
  9. A class can implement multiple interface but in Java one class can extend only one class.
  10. If a abstract class implements an interface then it is not necessary to implement the interface methods but the class which extends the abstract class has to provide the implementation for the abstract methods present the that abstract class as well as interface methods if the inheriting class is concrete.
  11. Just like we add a class to an package we can also add an interface to a particular package.
  12. Bytecode (.class file) gets generated for an interface also.
  13. JDK 1.7 and before that static methods were not allowed in an interface.
  14. Java 8 introduced static interface methods, as well as override-able instance methods with a default implementation ( explained in interview questions)

When we need to have abstraction as well as as definitions

Watch below video then note down the notes 🙂

video

Abstract class :

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

For example in below program we have an abstract class which contains abstract methods as well as a concrete method:

flyable-abstract-class

We create abstract class only to inherit in one or other class.

We can not instantiate an abstract class just like we can not instantiate an interface. If you are not inheriting the abstract class then there is no point in having it.

Below is the example of inheriting the abstract class and calling the concrete method through an instance of child class where concrete method is defined in parent.

aeroplane

test-class

More important points about abstract class :

  1. If any one method is abstract in a class then class must be declared abstract.
  2. An abstract class can have data members just like concrete class.
  3. An abstract class do contains a constructor but it can not be instantiated, it can only be inherited.
  4. A class which inherits the abstract class should provide implementations to all the abstract methods unless it is also abstract.
  5. A class which inherits the abstract class can override the concrete methods as well.

    Inheritance using Concrete Class

    Main focus of this lesson is to teach you about super keyword & IS-A Relationship

    Inheritance using Concrete Class & intro to super keyword :

    video

    Real World Example of super keyword :

    video

    Notes:

    Write example programs as it is in the above videos at

    1. 10:05
    2. 10:44
    3. 05:05

    Son IS-A Father & Car HAS-A Engine

    What is IS-A Relationship ?

    video

    More simpler example with explanation of IS-A Relationship :

    video

    Real World Example of IS-A Relationship :

    video

    IS-A Relationship vs HAS-A Relationship :

    video


    Types of Inheritance and diamond problem

    video


    Super class of All

    Object Class :

    video


    Constructor flow and super constructor

    Constructor Flow :

    video

    super :

    video


    Composition and Aggregation in Java

    Composition and Aggregation :


    Inheritance Interview Questions in Java

     

    What is Inheritance?

    What is interface ?

    Which class is super class of every class ?

    Define an abstract class and explain its purpose ?

    What is Object cloning ?

    What is super in Java ?

    What is Composition ?

    What is Inheritance why do we need it ?

    Is Constructor Inherited ?

    to do : find output of the program ( bhanuprakash )

    to do 2 : 11:00 AM bhanuprakash

    Can we use this and super both in one Constructor ?

    What is a Wrapper Class ?

    What is down casting and narrowing , widening , upcasting  ?

    What is pure abstract class ?

    Can we declare constructor inside an interface ?

    Can we create object of type interface or abstract class ?

    Can we instantiate an interface or abstract class ?

    What are the methods present in Object class ?