Abstract classes are "half-implementations" of a class. They can be partially implemented with some generic functionality, but leave part of the implementation to the inheriting classes. You could have an abstract class called Animal that has implemented some generic behavior/values such as Age, Name, SetAge(...). You can also have methods that are not implemented (they are abstract), much like an interface.
Interfaces are simply contracts that specify behaviors that should be available for a class. You could have an interface such as IWalker that requires public method Walk(), but no specifics on how it is implemented.
oop - What are some practical examples of abstract classes in java? - Stack Overflow
[Java] Can anyone give an example of an abstract class, and why it is important to use?
public abstract class Character {
...
public abstract void attack();
...
}
public class Warrior extends Character {
...
public void attack() {
// do something specific to warriors
}
...
}
public class Archer extends Character {
...
public void attack() {
// do something specific to archers
}
...
}
And after this, you can have a List<Character>, and call attack regardless of what the actual class is. Abstract classes, unlike interfaces are allowed to have their own fields. They are basically like "rich" interfaces.
java - Abstract Class:-Real Time Example - Stack Overflow
Can you give an example of why exactly a Java abstract method cannot exist in a non-abstract class? - Stack Overflow
Videos
Abstract classes are "half-implementations" of a class. They can be partially implemented with some generic functionality, but leave part of the implementation to the inheriting classes. You could have an abstract class called Animal that has implemented some generic behavior/values such as Age, Name, SetAge(...). You can also have methods that are not implemented (they are abstract), much like an interface.
Interfaces are simply contracts that specify behaviors that should be available for a class. You could have an interface such as IWalker that requires public method Walk(), but no specifics on how it is implemented.
Classes that are entirely abstract (all methods are abstract) are (almost) the same as interfaces (the major difference being they can contain fields and non-public abstract methods, which interfaces cannot). The difference is when you have an abstract class which contains a method which has some common functionality which will be the same for all derived children.
If you want to model a Filesystem, for example, you know that, regardless of the object type, you will have a path for an item. You'd want to have a common implementation for getting that path (no point in writing the same thing over and over again), and leave anything special for the children to implement.
public abstract class Character {
...
public abstract void attack();
...
}
public class Warrior extends Character {
...
public void attack() {
// do something specific to warriors
}
...
}
public class Archer extends Character {
...
public void attack() {
// do something specific to archers
}
...
}
And after this, you can have a List<Character>, and call attack regardless of what the actual class is. Abstract classes, unlike interfaces are allowed to have their own fields. They are basically like "rich" interfaces.
An abstract class is a class that has some methods implemented and some not (well, generally true). When you subclass (or extend) an abstract class you are required to implement all the abstract methods (or mark yourself as abstract) and may choose to override any of the implemented methods. When subclassing (or implementing) and interface you are required to implement all the methods (or, again, mark yourself abstract).
An abstract class is useful when a group of things all perform very similar functions except for a few methods. That way you don't have to repeat the implementation that is common among them.
A good example of real time found from here:-
A concrete example of an abstract class would be a class called Animal. You see many animals in real life, but there are only kinds of animals. That is, you never look at something purple and furry and say "that is an animal and there is no more specific way of defining it". Instead, you see a dog or a cat or a pig... all animals. The point is, that you can never see an animal walking around that isn't more specifically something else (duck, pig, etc.). The Animal is the abstract class and Duck/Pig/Cat are all classes that derive from that base class. Animals might provide a function called "Age" that adds 1 year of life to the animals. It might also provide an abstract method called "IsDead" that, when called, will tell you if the animal has died. Since IsDead is abstract, each animal must implement it. So, a Cat might decide it is dead after it reaches 14 years of age, but a Duck might decide it dies after 5 years of age. The abstract class Animal provides the Age function to all classes that derive from it, but each of those classes has to implement IsDead on their own.
A business example:
I have a persistance engine that will work against any data sourcer (XML, ASCII (delimited and fixed-length), various JDBC sources (Oracle, SQL, ODBC, etc.) I created a base, abstract class to provide common functionality in this persistance, but instantiate the appropriate "Port" (subclass) when persisting my objects. (This makes development of new "Ports" much easier, since most of the work is done in the superclasses; especially the various JDBC ones; since I not only do persistance but other things [like table generation], I have to provide the various differences for each database.) The best business examples of Interfaces are the Collections. I can work with a java.util.List without caring how it is implemented; having the List as an abstract class does not make sense because there are fundamental differences in how anArrayList works as opposed to a LinkedList. Likewise, Map and Set. And if I am just working with a group of objects and don't care if it's a List, Map, or Set, I can just use the Collection interface.
Here, Something about abstract class...
- Abstract class is an incomplete class so we can't instantiate it.
- If methods are abstract, class must be abstract.
- In abstract class, we use abstract and concrete method both.
- It is illegal to define a class abstract and final both.
Real time example--
If you want to make a new car(WagonX) in which all the another car's properties are included like color,size, engine etc.and you want to add some another features like model,baseEngine in your car.Then simply you create a abstract class WagonX where you use all the predefined functionality as abstract and another functionalities are concrete, which is is defined by you.
Another sub class which extend the abstract class WagonX,By default it also access the abstract methods which is instantiated in abstract class.SubClasses also access the concrete methods by creating the subclass's object.
For reusability the code, the developers use abstract class mostly.
abstract class WagonX
{
public abstract void model();
public abstract void color();
public static void baseEngine()
{
// your logic here
}
public static void size()
{
// logic here
}
}
class Car extends WagonX
{
public void model()
{
// logic here
}
public void color()
{
// logic here
}
}
An abstract class means that the class can't be directly instantiated. If a class has an abstract method, then it can't be directly instantiated since there would be no definition for that method. Therefore, any class with an abstract method IS an abstract class that can't be instantiated. Java simply makes you label it as such.
One of the key differences between an abstract class and a non-abstract class is the fact that you cannot create an instance of an abstract class. This prevents a situation where a method with no definition gets called.
So if we had the following abstract class:
abstract class Elephant { // abstract class
public String getName() {
return "Ollie";
}
public abstract void walk(); // abstract method
}
Then the following would be illegal:
Elephant e = new Elephant(); // error
Because Elephant is abstract and it cannot be instantiated.
But say for argument sake, that we make Elephant non-abstract but it is allowed to still have the abstract method walk(). i.e. we change the definition of Elephant to:
class Elephant { // non-abstract class
public String getName() {
return "Ollie";
}
public abstract void walk(); // abstract method
}
Now what should happen if I do the following:
Elephant e = new Elephant(); // legal since Elephant is non-abstract
e.walk(); // oops, we didn't define this anywhere
Java compiler won't allow this. You could argue that there are other ways to handle a situation like this, but this simply how the Java language has decided to implement the feature.
I should preface this post with this: I'm a beginner in Java.
I don't get interfaces. Yes, they enable us to do multiple inheritance, but that's it. I don't see any other use they have (please do let me know some of the uses they exclusively have).
As far as I know, anything I can do with interfaces, I can do with abstract classes (other than multiple inheritance of course). Should I use interfaces only when I need to do multiple inheritance?
Very sorry if I offended any of you interface lovers our there.
Thanks
Edit: Would really appreciate it if you could explain to me why all the attributes and methods in interfaces are public by default.