Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
First day using Java. Very basic question: I have three base classes, multiply-inherited by many classes. Each of these base classes has lots of implementation with few abstract methods:
class Consumer { /* lots of stuff */ };
class Producer { /* lots of stuff */ };
class Logger { /* lots of stuff */ };
class A : public Consumer, Producer, Logger {}
...
class Z : public Consumer, Producer, Logger {}
Now I know I can turn two of three base classes into interfaces and multiple-inherit the 2 interfaces and extend one class. Inadequate because of all the implementation in these base classes that doesn't make sense to duplicate into many subclasses.
I'm reading some stuff on delegation or composition, and I don't know how to apply this here. Basically my thought processes have been patterned by my C++ experience. What is the path of least resistance to convert my C++ class structure to Java?
I can fill out the base classes more if needed, but hopefully not necessary.
What is the path of least resistance to convert my C++ class structure to Java?
take a pen and paper, rethink your problem and make a design that's closer to how Java works.
There's no magical rule that will transform any given problem with multiple inheritance into another without it. Given the situation, you may want to separate concerns by creating multiple classes with a reference to each other, whereas you had only one in C++… But then it's up to the specific problem you're dealing with, how you'd build your hierarchy of classes and interfaces and how the resulting different class instances will relate to each other.
you have two options:
Create 3 interfaces for Consumer, Producer and Logger and make the classes A and Z implementing them. Of course for each class you have to provide an implementation. If the implementation is the same make a more generic class (it could be abstract, you probably won't need to instantiate it) implementing the 3 interfaces and then make the classes A and Z extending the abstract class.
As you said in your post it seems this solutions don't fit your needs , so use delegation instead, in the classes A and Z put 3 new fields (as instance variable) of Consumer, Producer and Logger type (you can instantiate them in the constructor) and then put methods which are wrappers for the Consumer, producer and Logger methods.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Hello I'm a freshman in college. And just did a research about object oriented programming and the language that i'm studying is Kotlin, But i couldn't find a real reason why we need to use abstract class or methods at all.
For example :
abstract class Student(name: String, age: Int) {
init {
println("Student name is: $name")
println("Student age is: $age")
}
//non-abstract function
fun demo() {
println("Non-abstract function of abstract class")
}
//abstract function
abstract fun func(message: String)
}
class College(name: String, age: Int): Student(name, age) {
override fun func(message: String) {
println(message)
}
}
fun main(args: Array<String>) {
val obj = College("Chaitanya", 31)
obj.func("I'm a Blogger")
obj.demo()
}
reference : https://beginnersbook.com/2019/03/kotlin-abstract-class/
How is this showing only the essential data?
The purpose of abstraction is so you can ignore details that aren't relevant to you. When I'm working with a list of objects, I normally want to add, remove, and iterate. I don't want to worry about resizing an underlying array or adding and removing linked nodes. By using the abstract interface List, I can put those details inside a box and not have to think about them.
Abstract classes are only one kind of abstraction and are usually less important than interfaces. Their main usefulness is to allow you to collect common characteristics in one shared place to reduce duplication. For example, if I have a List, I always need a way to iterate over it, and there's no more efficient way to implement the contains method than to iterate over the list to look for the item. I can put this method in an abstract class, and then the actual implementations of the list (array, linked, something else) only have to provide a way to iterate.
Simply
Actually purpose of abstraction is selecting data from a larger pool to show only the relevant details to the object. It helps to reduce programming complexity and effort
Abstract class is that it allows you to group several related classes as siblings and it helps to reduce the complexity of the design and implementation process of software.
And abstract classes help to describe generic types of behaviors and object-oriented programming class hierarchy. Also it describes sub classes to offer implementation details of the abstract class.
Abstract methods are mostly declared where two or more sub classes are also doing the same thing in different ways through different implementations.Also it extends the same Abstract class and offers different implementations of the abstract methods.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So someone tell me if i'm correct or not.
Encapsulation is Data hiding, allowing yourself only to view the
attributes and othering methods in a class privately, while you could you use these methods and abbritures in other classes,
Inheritance is extending a class, like taking some of the methods in the “super class” and pass it in “child class” and modify it or use it there.
Polymorphism is the same thing as inheritance but it's just formatted differently, like if i had an animal class, every animal has a different sound so, from there I would have something like this
Animal cat = new Cat();
overriding & overloading I’m not sure about this one
Abstract classes is taking methods or variables from the super class and pass those methods and variables as “Abstract” so that in the sub class you modify them and edit them.
Does that make sense? Or I misunderstood something?
These things all work together.
An object is something that is self-sufficient, it keeps track of its own state. Encapsulation enforces that separation, the object publishes methods that other objects call, but those methods are responsible for modifying the object's state.
In oo systems that use classes the class is a template for creating objects. Subclassing means creating a new class that is a more specific version of the subclassed class, where subclassed objects inherit the class definitions that specify the methods and fields of the superclasses.
Abstract classes defer some method implementations, leaving them to the subclasses to implement. If the superclass knows something has to happen at some particular point but wants to leave exactly what happens to the discretion of the specific objects, that's what abstract methods are for.
There's a pattern emerging here: objects taking responsibility for themselves, and a hierarchy of types from most abstract/general to most concrete/specific. Polymorphism is about objects' behavior being determined at the time the program runs based on what methods are overridden. Overriding means the subtype has a more specific version of a method that is substituted for the superclass version.
(Overloading otoh is a convenience for allowing a class to have methods with the same name but different parameters.)
The result of this can be a system that at a high level deals with abstract types and lets the objects themselves work out the exact details. The idea is that that way the details can be confined to the subclasses and the program can be modified by creating new subclasses without disrupting the rest of the program. In theory anyway, see Wadler's Expression Problem for where this all goes to hell.
And for examples: read the source that comes with the Jdk. The packages java.lang and java.util have a lot of classes that are examples of OO design.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I know that the byteValue() and shortValue() have implementations unlike the other abstract methods and were added in JDK1.1. This wouldn't have been possible if it was an Interface. But when the developers were writing Number class why did they make it an abstract class? Was it only because they expected that they might add more methods later on? I would only need answers supported by authoritative citations. thanks a lot everyone for the time you are taking to review my question and give answers.
Nobody here will know what went on in the minds of the designers, however abstract classes and interfaces are used for different purposes.
Classes (in Java) inherit in a strict hierarchy, and this hierarchy is a tool that can be used to ensure seperation of unrelated classes of objects. Classes are also more logical when the core functionality of the entire hierarchy is similar.
For example, with abstract classes Number and Letter it would not be possible to have a class that is both. With interfaces one could create a class that implements both which would make no sense.
Interfaces on the other hand are often used to expose a (usually) small piece of a class in a formal way so they can be used by reusable logic that uses only the functionality specified in the interface. They're more often used for adding supporting functionality, like Serializable, Comparable or Runnable.
An example, Printable and Comparable would be terrible abstract classes, as it would make it impossible to have a Comparable object also be Printable.
So the designers may have specifically chosen to make Number an abstract class to ensure that only one hierarchy of classes can be numbers, and nothing else. Perhaps it may allow for future optimizations where the JDK treats these classes as special cases, just like it does for String.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm looking for a definition of an abstract object. I cannot find it in my textbook and I've tried googling. Abstract classes and methods I understand. Anyone know what an abstract object is?
There is no such thing as an abstract object, because an object would imply something is real, and an abstraction cannot be real. Because it is abstract.
In layman's terms, you cannot instantiate abstract classes, and an instantiated class is an object.
There are abstract classes and methods, there is no such thing as an Abstract object.
You cannot instantiate an Object from an Abstract class.
You have to make another class extend it and override all the abstract methods it has.
This is why abstract classes are used, to put some constraints on a set of classes that have to extend it.
You should look into inheritance to see why abstract methods are used.
The fact that the notion of Abstract Object is not modeled in the language doesn't necessarily mean that it makes little sense. When we use objects and messages to represent elements of a given reality or fantasy some of them will have a more concrete meaning than others. For instance, if classes are first class objects in your system (cf. Smalltalk,) these objects could be deemed abstract when compared to their instances because they do not represent the thing but the concept associated to such a thing.
For example, the class WaterMolecule would be concrete as a class, its instances -representing H2O molecules- would also be concrete (as stated in the previous answers to this question) but the class WaterMolecule itself, when considered as an object, would fit the notion of an abstract object.
We could say that a class is an abstraction of level 1. An example of abstract object of level 2 would be the class of the class, namely the metaclass. If the metaclass is represented in the language as an object, this would qualify as an abstract object of level 2. Note that you can go a step further and consider the class of these objects, which would qualify as a concrete class (its instances are metaclasses) and, at the same time, as an abstract object (of level 3.)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am working on a java class for parsing HTML and generating RDF (which I think I will eventually split into two classes - one for parsing and one for generating RDF).
At the moment I am creating a lot of methods for checking HTML data and converting it into a more uniform representation. Some of the methods I have created so far are:
public boolean isInteger(String str) { }
public boolean isTime(String str) { }
public boolean isDate(String str) { }
public String dateConverter(String[] date) { } //Converts a Norwegian date into mmddYYYY
Should I put methods like these into a util class? At the moment they are only being used by this specific class, but I think that they might need to be use them by more than this one class at a later point in time.
Well, yes, the methods you listed look like good candidates for public static methods in a util class. (The last 2 or 3 would fit nicely in a class called "DateUtils", for example.)
Of course, if you only use them in one place, they can just as well remain "private helpers" there, but as soon as you have multiple places using them, a util class makes sense.
(Edit: overuse of static methods can be problematic, but I think these methods could well be static utils because they are pure functions.)
They seem generic enough to be appropriate to put in a util class. I would, at the very least. Basically any class that parses String in your code would need to use those methods. I would make them static before adding them to the class though to avoid unnecessary construction.
Decomposition is a good habit to get into. If you are unsure about whether or not you need to use them, then go ahead and do so. It will give you practice with using utility classes.
It is a good practice to collect such methods in a utility class. Even if they are called from a single class at the moment, these kind of methods will be potentially reused by other clients in a near feature.
However, you must pay attention to make this utility class easy to be reused. To do this, the method signatures, actually their arguments, must be as generic as possible. They should not take inputs specific to a class.
Another advice of mine is dividing this utility class into more than one classes, if it begins to contain many incoherent methods. You can do this by grouping the relevant methods in a separate class. For example, methods you wrote can be moved to TypeUtils class and you can collect conversion related methods in Html2RdfUtils class for instance.
Finally, if you feel that these utility classes can be benefical to your other projects, you can collect them in a distinct library.
if you eventually want to split them into two classes, you may want to define the baseclass as interface or abstract class.
Base on your requirement, abstract class should be the right choose.
Put something in common from the child class to the abstract class.(You can implement method in the abstract class)
from another answer, I want to ask a question?
the answer suggest putting static in front of the method to avoid unnecessary construction, it very make sense.
I know the abstract class does not require to be instantiated to call its method.
it also can avoid construction.
There is the question?
Which way is more legit? or better? or just same.