Github code sample Abstract
public abstract class Vehicle{ //abstract class
public void drive() {
System.out.println("This is a drive method from Vehicle class");
}
public abstract String drive(String engineType); //abstract method
}
public class AbstractClass {
public void main(String[] args) {
Student IS247=new Student();
}
public abstract class Student{
}
}
// Cat class implementing makeSound()
class Cat extends Animal {
void makeSound() {
System.out.println("Meow! Meow!");
}
}
interface Revenue{} //Interface Revenue
class Marketing implements Revenue{} //Marketing class is
// a concrete class that implements Revenue interface
Github code sample Abstract/Interface
class Cat implements Animal {
public void makeSound() {
System.out.println("Meow! Meow!");
}
}
Github code sample Abstract
Github code sample Interface
source:media.geeksforgeeks.org
abstract class Shape{}
class Circle extends Shape{}
interface Shape{}
class Circle implements Shape{}
Feature | Abstract Class | Interface |
---|---|---|
Definition | A class that can have both abstract and concrete methods. | A blueprint that only contains abstract methods (until Java 8, which introduced default methods). |
Method Implementation | Can have both abstract and fully implemented methods. | Only abstract methods (default and static methods were added in Java 8). |
Variables | Can have instance variables (fields) with any access modifier. | Only public , static , and final variables are allowed. |
Access Modifiers | Methods can be public , protected , or private . | Methods are public by default. |
Multiple Inheritance | A class can extend only one abstract class. | A class can implement multiple interfaces. |
Constructors | Can have constructors. | Cannot have constructors. |
Use Case | Used when classes share behavior but also need customization. | Used for defining contracts that multiple unrelated classes must follow. |
Github code sample Abstract