answersLogoWhite

0


Best Answer

Let us first define a generic Shape class. This will be an abstract class, since the term "shape" is, itself, very abstract.

Let us also assume that this shape is defined in only two dimensions, for the sake of simplicity.

abstract class Shape {

/*

One of the properties of any 2D shape is the area it takes up. Since calculating the area depends on the shape, we'll make this method abstract - all subclasses should implement it differently.

*/

abstract int getArea();

}

Now that we have our base class defined, we can create a rectangle subclass.

class Rectangle extends Shape {

/*

While a rectangle has other properties, we are really only interested in the ones required to calculate the area - width and height.

*/

int width;

int height;

/*

What's the area of a rectangular shape?

area = width x height

*/

int getArea() {

return width*height;

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Example of area of rect inheritance program in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is inheritance in core java?

Inheritance is the ability to derive and use other class's attribute and behavior. It results in the reduction of coding lines and the reuse of same codings in some other places. It is an important concept for java and makes to program easily...


How can you write multiple inheritance program in java?

Java does not support direct multiple inheritance. You can implement partial multiple inheritance using interfaces. ex: public class ExMultInherit implements interface1, interface2, interface 3 { ... .... ...... }


What does java not support?

Java does not support multiple inheritance.......


Where java program started?

If it is already compiled, you can start your Java program from the command line. Just type java myclass replacing "myclass" with the program you want to start - a program with the ".class" extension, for example, myclass.class.


What is ambiguity in multiple inheritance?

Java does not support multiple inheritance


What mutable and immutable in java and example program?

lmutable object


Example of multilevel inheritance program in java having HAS-A relation?

Just create a class that has two fields of object type. For example, to store data about a person, you might store a name (String object) and a birth date (Date or Calendar object).


What kind of inheritance is not allowed in java?

Java does not allow the multiple inheritance of concrete classes, though it does allow a "hybrid" inheritance of one concrete class and multiple interfaces.


What is problem in multiple inheritance?

Java does not support direct multiple Inheritance. Harder to implement, not every language support it: C++ does, Java does not.


Give the structure of multiple inheritance?

Java does not support multiple inheritance. It is done with the help of interfaces in java. a class can implement n number of interfaces, thus showing multiple inheritance. but a class cannot extend multiple classes in java.


What is public inheritance in java?

Inheritance is a Java feature by which we can reuse code and programming logic from one class in another class. We implement Inheritance using the extends keyword.Ex: public class Ferrari extends Car {…}Here the Ferrari Class will extend features from the Car Class.This is Inheritance. The different types of Inheritance are:Single InheritanceMulti-Level InheritanceMultiple Inheritance (Java supports only Partial Multiple Inheritance) andHybrid Inheritance


How ploymorphism and inheritance is different from that in Java and c plus plus?

C++ allows multiple inheritance while Java does not. In my opinion, multiple inheritance is not useful because it can get very confusing very quick. For polymorphism, C++ does early binding by default, while Java does late binding by default. Late binding is more useful than early binding.