Monday, April 28, 2025

Java OO Tutorial - Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit the properties (fields) and behaviors (methods) of another class. In Java, we use the "extends" keyword to create a subclass that inherits from a parent (superclass) class.

A subclass can inherit fields and methods from a superclass and also add its own fields and methods, and override the behavior of inherited methods. The benefit of inheritance is that it allows us to reuse code that already exists in a superclass, reducing the amount of code we need to write. 

//Operations.java 

class BasicOperations {
	
	public void addition(int x, int y) {
		System.out.println(x + y);
	}
}

class MoreOperations extends BasicOperations {
	public void multiplication(int x, int y) {
		System.out.println(x * y);
	}
}

public class Operations extends MoreOperations {
	
	public void substraction(int x, int y) {
		System.out.println(x - y);
	}

	public static void main(String[] args) {
		
		Operations myObj = new Operations();
		
		myObj.addition(10, 5);
		myObj.substraction(10, 5);
		myObj.multiplication(10, 5);	

	}
}

Here's an explanation of the code block by block: 

//Operations.java 

class BasicOperations {
	
	public void addition(int x, int y) {
		System.out.println(x + y);
	}
}

This is the BasicOperations class with a single method addition that takes two integer arguments and prints their sum. 

class MoreOperations extends BasicOperations {
	public void multiplication(int x, int y) {
		System.out.println(x * y);
	}
}

This is the MoreOperations class that extends the BasicOperations class and adds a method multiplication that takes two integer arguments and prints their product. 

public class Operations extends MoreOperations {
	
	public void subtraction(int x, int y) {
		System.out.println(x - y);
	}

	public static void main(String[] args) {
		
		Operations myObj = new Operations();
		
		myObj.addition(10, 5);
		myObj.subtraction(10, 5);
		myObj.multiplication(10, 5);	

	}
}

This is the Operations class that extends the MoreOperations class and adds a method subtraction that takes two integer arguments and prints their difference. It also contains a main method that creates an instance of the Operations class and calls its addition, subtraction, and multiplication methods.

When we run the main method, it creates an instance of the Operations class and calls its addition, subtraction, and multiplication methods.

Since the Operations class extends the MoreOperations class, which in turn extends the BasicOperations class, it has access to all the methods in these classes.

Therefore, it can call the addition, subtraction, and multiplication methods without having to define them in the Operations class itself.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...