0
How to use an abstract class and one concrete class be extending in other concrete class
I have abstract class named shape.java and then 2 concrete class name ls rectangle.java and triangle.java and also other class of outside 2 class i mentioned before named line.java. Rectangle and triangle connected to line.java and ofcourse abstract class named shape.java. How am i connect abstract class and line.java to rectangle.java or triangle.java
7 Answers
0
shape.java (abstract)
rectangle.java and triangle.java (concrete) both extend or import line.java (concrete)??
As per my understanding from your explanation, you would like line.java to extend shape.java, which would force you to implement all the methods that were declared on the abstract class, and from there you would be able to "connect" rectangle.java and triangle.java to line.java, which would let you overload or override as well as use the inherited methods from line.java. It would look like this:
Class line extends shape {
//Implement all declared methods on "shape" plus "line" methods
}
Class rectangle extends line {
//Inherit all shape and line methods
}
Class triangle extends line {
//Inherit all shape and line methods
}
If you just want to use the methods implemented in the class "line", then just import it instead of extending it to the other 2 classes.
Let me know if i got what you wanted to know Dispssin.
0
Thx roberto, i write the all that code from class diagram, can i show you my problem from class diagram?
0
Sure, I'll assume then that I understood correctly your problem, share the diagram and lets see if I can help.
0
Ths for your help. Now i can fix that. But i have another problem about try and catch. Can you make be short this code?
0
/*
* 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 shapecollection;
/**
*
* @author Achmad Yusuf Naution
*/
public class ShapeCollection extends Shape{
private int MAX_EL;
private Shape[] arrShape;
private int indeks;
public ShapeCollection()
{
super();
MAX_EL = 10;
indeks = 0;
arrShape = new Shape[MAX_EL];
}
public void addShape(Shape S)
{
arrShape[indeks] = S;
indeks++;
}
public Shape getShape(int indeks)
{
return arrShape[indeks];
}
public void display()
{
int i;
Triangle a;
Rectangle b;
for (i=0;i<=indeks;i++)
{
try
{
if(getShape(i) == (Rectangle) getShape(i))
{
b = (Rectangle) getShape(i);
System.out.print(b.name);
b.display();
}
}
catch(Exception e)
{
}
try
{
if(getShape(i) == (Triangle) getShape(i))
{
a = (Triangle) getShape(i);
System.out.print(a.name);
a.display();
}
}
catch(Exception e)
{
}
}
}
public int getIndeks()
{
return indeks;
}
@Override
protected double getArea() {
return 0;
}
@Override
protected double getCircumference() {
return 0;
}
}
0
This is another issue, please prepare a new question, and make sure to explain what's blocking you so I can help you. Just post a new question and I'll take a look.
0
Okay. I'm sorry