Questions tagged as 'classes'

2
answers

Why can this happen in a foreach?

I built two simple classes: import java.util.ArrayList; import java.util.List; public class Aluna { String nome; String idade; String cpf; List<Aluna> listar(){ ArrayList<Aluna> aluns = new ArrayList<>(); Aluna...
asked by 14.02.2016 / 17:34
2
answers

Collision of names between class and namespace

Is it wrong, bad practice or can I have problems with class equal to namespace ? Example: namespace Cielo { public class Cielo { } } It has worked, in some places it gets a bit strange to call Cielo.Cielo.etc . Should...
asked by 29.04.2016 / 20:08
3
answers

How to avoid the use of setters in such cases?

In general object orientation it is advisable to avoid the use of setters. The usual justification for this is that logic that modifies the state of an object must be encapsulated in the object. Therefore, the ideal is the object to expose behav...
asked by 28.07.2016 / 05:43
2
answers

What does a class diagram contain?

The title is a stupid question, because the name of the diagram already responds. But yesterday this question came to my head when a professor of Analysis and Software Projects asked the whole class the class diagram of what would be our project...
asked by 20.10.2016 / 14:27
1
answer

When should I use __init__ in functions within classes?

From the book I'm studying, at times the author uses __init__ as being a first function of a class. This function (and others) always have self as one of the variables (something that I still do not understand why). When should (an...
asked by 19.01.2016 / 03:04
1
answer

___ ___ erkimt I extend an abstract class or concrete? ______ qstntxt ___

When I need to extend a class, following the concept of Object Orientation, should I extend my code from an abstract or non-abstract class? What is the best practice to join?

    
______ azszpr89237 ___

There is no better option, you extend the class you need to extend. Whether it is abstract or not, it makes no difference to its code other than the fact that an abstract will possibly have unimplemented methods and its new class will have the obligation to create an implementation for all abstract methods contained in the class. optional).

If you want to know if it's better to create an abstract class or not, then it depends on what you want. An abstract class can not be instantiated. It is designed to be used as a template for other classes. Non-abstract can be used as models but can also be instantiated directly. You just make it an abstract class if you want to ban its instantiation (which is bound, if it is incomplete).

Of course, if the class has methods without implementation, they act as contracts for the derived classes to follow, that is, they function as if it were an interface, then the class must necessarily be abstract. Unable to instantiate classes with methods without implementation.

For example. If you have a class %code% and the derivatives of it %code% and %code% . Probably you do not need and maybe can not instantiate only the %code% . It is probably incomplete. You just created it to support the two (who knows other) derivatives I mentioned. It is almost an interface, but probably has variables and some methods with implementation. So %code% should probably be abstract.

Remembering that you can only inherit from a class. Abstract or not. Interface can several.

    
___

When I need to extend a class, following the concept of Object Orientation, should I extend my code from an abstract or non-abstract class? What is the best practice to join?     
asked by 27.09.2015 / 00:44
1
answer

How does the class constructor declaration in Qt?

Work with C and a little assembly on Atmel AVR microcontrollers. I'm trying to understand how the framework extends C ++. I created a new project with Qt Creator (Widgets), and generated the following code: MainWindow::MainWindow(QWidge...
asked by 08.08.2015 / 22:44
1
answer

How does the default Java constructor work?

How is the default Java constructor? Is this? public Pessoa(){ super(); }     
asked by 02.10.2014 / 18:16
1
answer

What is explicit in C ++?

I came across the term explicit being used in a C ++ code. How useful is this keyword?     
asked by 02.05.2016 / 13:17
2
answers

What is a destructor for?

In some languages, classes have methods destructors . In the ones I've seen, it's declared as a constructor with the ~ sign on the front. Something like: public class Foo { public ~Foo() { //Fazer algo } } What are...
asked by 20.07.2015 / 13:28