CodeGym /Java Course /Module 2. Java Core /Abstract class vs. interface

Abstract class vs. interface

Module 2. Java Core
Level 4 , Lesson 5
Available

"Hello, Amigo! Bilaabo will tell you the differences between an abstract class and an interface. There are several."

Abstract class Interface
Inheritance
An abstract class can inherit only one class but it can inherit any number of interfaces. An interface cannot inherit classes, but it can inherit any number of interfaces.
Abstract methods
An abstract class can contain abstract methods. But it may not have any at all. All of an interface's non-static and non-default methods are abstract, i.e. they have no implementation. An interface can have no methods at all.
Methods with an implementation
An abstract class can contain methods with an implementation. An interface may have default methods.
Data
No restrictions. An interface contains only public final static data.
Object creation
You cannot create an instance of an abstract class. You cannot create an instance of an interface.

"That's my understanding. Brief and to the point."

"Thanks, Amigo."

Comments (14)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Jonaskinny Level 25, Redondo Beach, United States
26 February 2022
You cannot create an instance of an abstract class. Sorry but this is not correct. I realize we are learning here but at least footnote that we can instantiate an abstract class if we define all the abstract method along with the new statement. Save below code in file InstantiateAbstract.java compile and run.

abstract class IDareYa {
    abstract void speak();
}
  
public class InstantiateAbstract {
    public static void main(String[] args) {
        IDareYa InstancefromAbstractClass = new IDareYa() {
            void speak() {
                System.out.println("Hello World! from instance of Abstract Class using Java");
            }
        };
        InstancefromAbstractClass.speak();
    }
}
Daniel Bovey Level 26, United Kingdom, United Kingdom
5 February 2023
That's an anonymous class - it isn't an instance of the abstract class. The anonymous class inherits the abstract class
Abhishek Tripathi Level 72, Rewa, India Expert
13 September 2023
Not even a single person liked the fact of anonymous class maybe everyone is busy to prove the content wrong (Which is not wrong). I had studied it already, so I appreciate you brother for correcting @Jonaskinny.
Boris the Llama Level 20, Maidstone, United Kingdom
12 July 2019
I thought this guy was from planet pascal?
TheLordJackMC Level 39, Princeton, idk somewhere
15 July 2021
why is he not preaching pascal superiority? why is he here? isnt this rishi's job?
Oleh Level 32, Kyiv, Ukraine
28 February 2019
Default methods in the interfaces not considered for "methods with an implementation"?
Fadi AlSaidi Level 13, Carrollton, United States
2 March 2019
what do you mean?
Oleh Level 32, Kyiv, Ukraine
2 March 2019
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
Fadi AlSaidi Level 13, Carrollton, United States
3 March 2019
very interesting. Thank you
Mateo Level 26, Zagreb, Croatia
4 April 2019
Was thinking the same thing, I guess they are not considered as such :P
Roy Level 22, Bangkok, Thailand
26 April 2019
So true. i notice since moving to Java Core, should google all things twice to make sure it doesnt get confusing. Things like An interface cannot contain methods with an implementation. can cause a lot of confusion later on, as interfaces can contain methods with implementation by using default methods.
Ops guy Level 17, Round Rock, TX, United States
9 May 2019
Default methods started with Java 8. I think this course was based on Java 7.
Ewerton Level 30, Belo Horizonte, Brasil
30 June 2019
CodeGym - Java interface default method " Similarly, Java 8 differs markedly from Java 7. Most of our lessons were written for the 7th version of the language, but of course we won't ignore important innovations. Since we're already talking about interfaces in this lesson, we'll consider one update — default methods in interfaces. "
Bill Wu Level 29, Wellington, New Zealand
14 February 2019
Very useful information, and it would be better with examples in practice.