Question

How to create the multiple class in Java? Please elaborate it by using the example.

How to create the multiple class in Java? Please elaborate it by using the example.

Homework Answers

Answer #1

Java Code:

Output Snapshot:

Text Code:

// Class1 Declaration
class Class1 {
// Class1 Member Variable
int class1_variable;
  
    // Class1 Member Functions
public void add(int x, int y) {
class1_variable = x + y;
System.out.println("In Class 1 : Addition result: "+class1_variable);
}
public void sub(int x, int y) {
class1_variable = x - y;
System.out.println("In Class 1 : Subtraction result: "+class1_variable);
}
}

// Class2 Declaration
class Class2 {
// Class2 Member Variable
int class2_variable;
  
   // Class2 Member Functions
public void add(int x, int y) {
class2_variable = x + y;
System.out.println("In Class 2 : Addition result: "+class2_variable);
}
public void sub(int x, int y) {
class2_variable = x - y;
System.out.println("In Class 2 : Subtraction result: "+class2_variable);
}
}

// Main Class - Entry point of code
public class Main
{
   public static void main(String[] args) {
   // Creating object1 object of Class1
       Class1 object1 = new Class1();
       // Calling add() and sub() member function using object1
       object1.add(20,10);
       object1.sub(20,10);
      
       // Creating object2 object of Class1
       Class2 object2 = new Class2();
       // Calling add() and sub() member function using object1
       object2.add(30,20);
       object2.sub(30,20);
   }
}

Explanation:

  • To create multiple class in Java, you need to declare each class independently before using it in the Main class.
  • As to access class member variable and functions, object of the respective class is needed to be created, hence in the main class, object1 and object2 are created for Class1 and Class2 respectively.
  • As the add() and sub() member function is accessed using object1 object, it invokes the function from the Class1 and similarly for object2.
  • Please refer code comments for the understanding purpose.
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using Java: 1. Create a Chapter class with a Chapter title and a number of pages...
Using Java: 1. Create a Chapter class with a Chapter title and a number of pages 2. Create a test chapters class that creates single chapters and arrays of chapters and prints them out
For Java. Let's get some practice using Java libraries! Create a class named Formatter. Formatter should...
For Java. Let's get some practice using Java libraries! Create a class named Formatter. Formatter should provide one public class method formatDate. It should accept a positive long representing milliseconds since 1970 and return a String containing the ISO-8601 representation of this time. Here is one example for a recent timestamp: given 1602106609897 your function should return 2020-10-07T21:36:49.897Z. Do not overthink this. The solution we are after is a single line of code. We suggest that you explore the various...
Java Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks the...
(Please use java to write these questions) Q1. Create a class on an object Computer with...
(Please use java to write these questions) Q1. Create a class on an object Computer with two fields and two methods as follows: (5 points) field: cpu and memory, method: compile( ) and execute( ) Q2. Write a source code to activate a class created in Q1. (5 points)
Can i have multiple example for my health class assignment. create your own example of a...
Can i have multiple example for my health class assignment. create your own example of a Health Belief Model corresponding to a behavior that you want to change
java Give an example of a class that contains at least one class variable and at...
java Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
In Java, create a Square class that can be queried for its perimeter and area. Create...
In Java, create a Square class that can be queried for its perimeter and area. Create a second class , with a main(), to test the Square class. Write code in full detail.
java thread question. Could you please give me a multiple thread program example that all threads...
java thread question. Could you please give me a multiple thread program example that all threads run in parallel?
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT