Solved

abstract class with all non abstract mentods

Posted on 2016-08-13
6
75 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 26

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 26

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
groupSum5 challenge 5 68
Path to Python 9 104
How to remove numeric and alpha from an alphanumeric field? 5 70
This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now