Solved

abstract class with all non abstract mentods

Posted on 2016-08-13
6
79 Views
Last Modified: 2016-08-14
Hi,
Can a abstract class with all non abstract mentods? Is that is possible. I am under impression that atleast one method of abstract class should be abstract. What are advantages and practical uses of abstract methods compared to interface. I see many cases abstract class is implements a interface. Then a concrete class extends the abstract class. I wonder what is use of this pattern

interface--->abstract class(implements)--->concrete class(extends)
0
Comment
Question by:gudii9
  • 2
  • 2
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 250 total points
ID: 41755192
>> Can a abstract class with all non abstract methods?

Yes there's nothing wrong with that.  It's not common but it's allowed.

>> What are advantages and practical uses of abstract methods compared to interface.

Interfaces specify a list of methods with no implementations.
Abstract classes can include implementations (code) and fields (values).  So you can do more with an abstract class.

Why then not just use abstract classes everywhere?  Because a given concrete class can only inherit from at most 1 abstract class.  But it can implement as many interfaces as it wants.

So interfaces are more flexible.

Doug
0
 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 250 total points
ID: 41755205
Doug is correct except that in the newest version of Java they have introduced the ability to but a default implementation of a method in an interface. This gives a little more flexibility in trying to do multi inheritance in a class.

Usually the benefit of interfaces is it really helps you if you are going to have multiple classes that need to have a common set of methods but don't entirely work the same.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41755544
Doug is correct except that in the newest version of Java they have introduced the ability to but a default implementation of a method in an interface. This gives a little more flexibility in trying to do multi inheritance in a class.

any good links or examples on this?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 27

Expert Comment

by:dpearson
ID: 41755591
Yeah I didn't mention default implementations as I'm concerned it may confuse your understanding if you're not clear on abstract classes vs interfaces yet.

But anywhere here's some info on them:
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

(Oracle added this capability so they could extend some existing interfaces in the Java language without breaking a ton of existing code).

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 41755873
>> Can a abstract class with all non abstract methods?

Yes there's nothing wrong with that.  It's not common but it's allowed.

it is kind of defeating the purpose for which we create abstract class right?
if we do not have abstract methods in it it seems very uncommon to me.
0
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 41755898
Not always, you may just have a whole bunches of functions you want inherited but don't want that base class to ever be instantioated.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question