Java Program using Multiple Classes

Java is an object-oriented programming language. You can create multiple classes in Java and create a class hierarchy such that one class can call another class. Do not confuse this with Java inheritance because, in class inheritance, there is a parent-child relationship between classes.

Advertisements

The derived or the child class always inherit the properties and methods from the parent or the base class.

Inheritance is not the scope of this article.

In general, Java has a main public class with a name that should match with the Java class file name and it calls other classes from this main class. The second approach is to write each class in different files and link them together with a package. In other words, all class files should be in the same class.

This program is a basic Java program to demonstrate the structure of the Java program. It is intended for beginner level learner of Java programming. It is written using Netbeans 8.2 installed on a Windows 7 64-bit PC. You may also want to install the suitable Java SDK 8.2.

Advertisements

Problem Definition

Our aim is to create a Java program that has three class, the 1st class will add two number and display results, and 2nd class will subtract two numbers and display results. The 3rd class will call the other two classes.

Program Code

The program code for add.java to add two numbers and display the results is given below.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package JavaExamples;

/**
*
* @author Admin
*/
<span style="font-family: andale mono, monospace;">
public class Add {

int a;
int b;
int c;

void add(int x , int y ){

a = x;
y = b;
c = a + b;
System.out.println("C = " + c);
}

}

The program code for subtract.java to subtract two numbers and display the results is given below.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package JavaExamples;

/**
*
* @author Admin
*/
public class Subtract {

 

int m;
int n;
int p;

 

void subtraction(int r, int s){


m = r;
n = s;
p = m + n;
System.out.println("P =" + p);
}

}

The program code for Results.java to call the previous two classes is given below.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package JavaExamples;

/**
*
* @author Admin
*/
public class Results {

 

public static void main(String[] args){


Add obj = new Add();
Subtract obj2 = new Subtract();


obj.add(10,30);
obj2.subtraction(30,10);

}

}

Output

C = 10

P = 40

Related Articles:-

Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version