Java Tutorial Series - OOPS - Inheritance

Now we have gone through the basics of Core Java which will set up a base for future concepts to be learned. In this series, we will be covering OOPS concepts, we have understood basic concepts, Collections in Java, now it is high time to dive deep into OOPS.

Inheritance in Java - 

Using the concept of Inheritance, sub class can inherit the properties of base class, in other words sub class can use the methods of parents class and can also override them. We can see it in below example -

public class Machine {
         public void start() {
              System.out.println("Machine Started");
              }

      public void stop() {
         System.out.println("Machine Stopped");
         }
}


public Car extends Machine {

@Override
public void start() {
              System.out.println("Car Started");       //Method overriding
              }

}

Overriding can also be achieved using right click -> Source -> override/implement methods and click on methods which we want to override.

It is recommended to override methods only and not variables.

Types of Inheritance in Java

  1. Single
  2. Multiple
  3. Multilevel
  4. Hybrid
  5. Hierarchical
At class level we have below  implementation possible
  • Single
  • Multilevel
  • Hierarchical
At Interface level
  • Single
  • Multiple
  • Hierarchical
  • Multilevel
Keyword - Extends
  • A class always extends a class
  • An interface always extends an interface
Keyword - Implements
  • A class can implement an interface.

Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD