1

I'm struggling to understand what is a concrete class.

Currently I have an empty class as so:

public class xList<E> extends ArrayList<E> {

}

In a different class and using main method i use:

xList list = new xList();

and then add to it using:

list.add("example");

What I am trying to do is create the xList class which will satisfy my class diagram as being concrete by inheriting from ArrayList. using the xList class i can create an list of students (so it has its own list of objects from a student class). The student class consists of id and name with setters and getters.

Is my way of thinking correct or am i going down the wrong route?

JMa
  • 76
  • 1
  • 7
  • 3
    I've always heard "concrete" class be referred to as implementations of an interface or abtract class. An `ArrayList` is itself already a concrete class by that logic – OneCricketeer Jan 11 '17 at 22:09
  • The class name 'xList' should match java convention and be called 'XList'. What is the key behavior of ArrayList that you are trying to override?. Maybe look at this http://stackoverflow.com/questions/18395005/why-linkedlist-and-arraylist-extends-abstractlist-in-java answer, which outlines the differences between an AbstractList and concrete ArrayList. I suspect rather than extending from ArrayList, your class should encapsulate a List object as a studentClassList variable. – emeraldjava Jan 11 '17 at 22:26
  • 1
    The entire point of `List` is that you can specify the type on creation. Such as `List students = new ArrayList<>();` – Powerlord Jan 11 '17 at 22:31
  • 2
    What @cricket_007 said is correct. A class can be called "concrete" if it is simply not abstract. Both `xList`, and the extended class, `ArrayList`, are not abstract, thus concrete. – MC Emperor Jan 11 '17 at 22:35
  • This sounds like a case where you really want composition over inheritance. – chrylis -cautiouslyoptimistic- Jan 11 '17 at 22:46
  • Hi all, ty I understand now extending ArrayList is not the way i should do this @emeraldjava Can you explain a little further pls. To satisfy my class diagram i need an abstract class which contains the add() etc methods. the XList would then act as concrete class to add student information to an array or linkedlist even. To make it clearer this is what i am trying to code: https://gyazo.com/dcbe9ebfc878cac881ff0957eb788916 – JMa Jan 11 '17 at 22:46
  • Why do you need "an abstract class"? Is this a class assignment? If so, you're probably meant to write the implementation yourself. Otherwise, use the `List` interface. – chrylis -cautiouslyoptimistic- Jan 11 '17 at 22:47
  • First of all, `xList` (bad class name - should start with an uppercase letter) is not "empty" because it inherits a whole mess of machinery from `ArrayList`. Since all classes inherit from `Object`, there is no such thing as an "empty" class in Java. Second, any class that is not abstract is concrete. The term only appears once in the JLS, without definition, but "concrete method" is defined as a method that is not abstract. The industry consensus is that a concrete class is a non-abstract class. – Lew Bloch Jan 12 '17 at 00:13
  • @JMa Your diagram doesn't mention an abstract class at all. Note that a non-abstract class can also be extended. – MC Emperor Jan 12 '17 at 07:57

1 Answers1

1

Any class that is not abstract is concrete.

Extending ArrayList or any other concrete collections class is usually not the best idea however. Why create your xList class instead of just using ArrayList?

puhlen
  • 8,400
  • 1
  • 16
  • 31
  • 1
    Well, I don't see the problem of extending an `ArrayList`. See [this post](http://stackoverflow.com/questions/5306887/should-i-extend-an-arraylist-is-a-or-should-i-include-it-as-a-member-has-a). – MC Emperor Jan 11 '17 at 22:34
  • @MCEmperor that post is discussing composition vs inheritance; neither is necessary, ArrayList can be used as-is. – Andy Turner Jan 11 '17 at 23:24
  • There could be a reason to extend `ArrayList`, conceivably, but usually it is not necessary, and this answer explains the term "concrete class". – Lew Bloch Jan 12 '17 at 00:14
  • By the way, I would use the term 'non-abstract' in context of Java classes, but that's a matter of taste. However, I guess that that will cause less confusion. – MC Emperor Jan 12 '17 at 07:39