Java Inheritance, Java Abstract Classes and Methods, Java Interfaces, Final Keyword

Inheritance

It is the ability to extract the properties of one class into another class.

The main objective is to provide code reusability. It reduces the code redundancy to a greater extent which saves lot of developer time.

Types of Inheritance:

  1. Single
  2. Multi level
  3. Hierarchical

Multiple and Hybrid inheritances can be achieved through Interfaces but not through classes.

In order to inherit one class to another class we use the keyword “extends”.

Single Inheritance

Here only two classes are involved in which one is parent class and the other one is the child class.

Once A is inherited into B we need not create object for class A. All the public methods of A can be accessed through object of class B.

Syntax:

class SuperClass{       //body;
}
class SubClass extends SuperClass{
//body;
}

An example program for single Inheritance,

class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}

class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {

// Create a myCar object
Car myCar = new Car();

// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();

// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}

}

Constructors Using Inheritance

class CSE{
private int tc;
public CSE(int tc){
this.tc = tc;
System.out.print(this.tc);
}
}
class CSE-A extends CSE{
int ac;
public CSE-A(int ac,int tc){
super(tc);
this.ac=ac;
System.out.print(this.ac);
}
}
public class CSEDemo{
public static void main(String args[]){
new CSE-A(64,2500;
}
}

In order to call a super class method, the “Super” keyword is used.

Multi-Level Inheritance

In multilevel, a chain of classes are involved in Inheritance.

class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{

public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}

Output:

Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph

Abstract Classes and Methods

Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces.

A class is a set of complete and incomplete methods is an abstract class.

The abstract keyword is a non-access modifier, used for classes and methods:

  • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Abstract is the keyword to be prefixed before the method name and the class name as well. If at least one method is incomplete the entire class becomes incomplete. The definition to the incomplete method can be given in corresponding subclasses.

Each subclass can have its own definition to the abstract methods in the super class. The purpose of the abstract classes is to act as a base class. As it is abstract class we cannot create object for it. How ever an object reference can be created.

Most often the abstract classes are preferable to use in hierarchy inheritance.

An abstract class can have both abstract and regular methods:

abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}

From the example above, it is not possible to create an object of the Animal class:

Animal myObj = new Animal(); // will generate an error

To access the abstract class, it must be inherited from another class. To use the abstract class it must be inherited from another class.

Example

// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}

Output:

The pig says: wee wee
Zzz

Interfaces

Another way to achieve abstraction in Java, is with interfaces.

An interface is a completely "abstract class" that is used to group related methods with empty bodies:

Example

// interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}

To access the interface methods, the interface must be “implemented” (kind like inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class:

Example

// Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
// Pig "implements" the Animal interface
class Pig implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
}
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}

Output:

The pig says: wee wee
Zzz

Multiple Interfaces

To implement multiple interfaces, separate them with a comma:

Example

interface FirstInterface {
public void myMethod(); // interface method
}
interface SecondInterface {
public void myOtherMethod(); // interface method
}
class DemoClass implements FirstInterface, SecondInterface {
public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}
}
class MyMainClass {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}

Output

some text..
Some other Text...

Final Keyword

In Java, data member of a class, a method of a class and the class itself can be prefixed by a keyword “final”. The keyword plays a significant role in defining the three different components in the class.

  1. In case of a data member as final it behaves like a constant.
final int MAX=5;
final define PI=3.14

2. A method is declared as final then that method cannot be overwritten.

class superA{
final public void show(){
System.out.println("Hello CSE-A");}
}
class SubB extends SuperA{
public void show(){
System.out.println("Hi CSE-A");}
}
public class OverRidingDemo{
public static void main(String args[]){
SuperA x = new SuperA();
x.show();
SubB y = new subB();
y.show();
}
}

The above program gives compilation error as the final method is overridden in the subclass. In the absence of final keywords, the code runs successfully.

3. In case of class is declared as final it cannot be inherited. It means that the class becomes an independent class.

final class superA{
public void show(){
System.out.println("Hello CSE-A");}
}
class SubB extends SuperA{
public void show(){
System.out.println("Hi CSE-A");}
}
public class OverRidingDemo{
public static void main(String args[]){
SuperA x = new SuperA();
x.show();
SubB y = new subB();
y.show();
}
}

In case of method over riding the following access privilege's should be followed.

  1. A public method can be overridden by a public method only.
  2. A protected method can be overridden by either a protected or public method only.
  3. A default method can be overridden by either a default or protected or public method only.
  4. A private method can be overridden by either a private or default or protected or public method only.

--

--