Question

In: Computer Science

How to write a Java program that has a base class with methods and attributes, subclasses...

How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.

Solutions

Expert Solution

CODE:

class Demo2{
   int a=10,b=40;
   void add(){
       System.out.println("Sum of a and b values is "+(a+b)+ "from Demo2");
   }
   void mul(){
       System.out.println("Multiplication of a and b values is "+(a*b)+ "from Demo2");
   }
}

class Demo extends Demo2{
   int a=12,b=20;
   void print()
   {
       System.out.println("Sum of a and b values is"+(a+b));
   }
   void multi(){
       System.out.println("Multiplication of a and b values is "+(a*b));
   }
   public static void main(String[] args) {
       Demo d=new Demo();
       d.print();
       d.multi();
       d.add();
       d.mul();

   }
}

OUTPUT:

If you have any doubts please COMMENT....

If you understand the answer please give THUMBS UP....


Related Solutions

write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Write in Java the following: A.  A bank account class that has three protected attributes: account number...
Write in Java the following: A.  A bank account class that has three protected attributes: account number (string), customer name (string), and balance (float). The class also has a parameterized constructor and a method public void withDraw (float amount) which subtracts the amount provided as a parameter from the balance. B. A saving account class that is a child class of the bank account class with a private attribute: penality rate (float). This class also has a parameterized constructor and a...
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.
1. Java Abstract Class (2 pts) () • A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.• Following is an example of abstract class:abstract class Book{String title;abstract void setTitle(String s);String getTitle(){return title;}}If you try to create an instance of this class like the following line you will get an error:Book...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ /* Hint: This Queue implementation will always dequeue from the first element of the array i.e, elements[0]. Therefore, remember to shift all elements toward front of the queue after each dequeue. */ public class QueueArray<T> { public static int CAPACITY = 100; private final T[] elements; private int...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
In C++ write a program with a base class thet has a pure virtual function SALARY,...
In C++ write a program with a base class thet has a pure virtual function SALARY, and two derived classes. In the first derived class salary is increased by 20%, in the second derived class salary is increased by 30%
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS...
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS COVERING THE FOLLOWING POINTS: 1) COUNTING NUMBER OF OBJECTS CREATED ( NO OBJECT ARRAY TO BE USED) USING ROLE OF STATIC MEMBER 2) SHOWING THE VALID INVALID STATEMENTS IN CASE OF STATIC MEMBER WITH NON STATIC MEMBER FUNCTION, NON STATIC MEMBERS OF CLASS WITH STATIC MEMBER FUNCTION, BOTH STATIC. SHOW THE ERRORS WHERE STATEMENTS ARE INVALID. 3) CALL OF STATIC MEMBER FUNCTION, DECLARATION OF...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT