Skip to main content

Difference between Abstract Class and Interface

Difference between Abstract Class and Interface

Difference between abstract class and interface ?

Abstract Class:-

1) Can have abstract and non-abstract method both.
2) One class can extend at most one class.
3) can declare non abstract method and also can define body for it.

1) can have only abstract method.
2) One class can implement multiple interfaces.
3) can't delare body to a method.

Please look below example for more understanding:-

package Basics;

public abstract class Animal {

 int  head;
 //no body (unimplemented function need to override in child class as a
 abstract void setLegs();
 //having body
 //may override or may not
 void setHead()

package Basics;

// can have only methods and by default all are abstract need to be override
//by implementing class, unlike abstract no method can havedefinition
public interface Pet {

 void tellWhereAreYouFrom(String place);
 String tellYourWeight(String weight);

package Basics;

// can extend one class but can implement multiple interface
public class Dog extends Animal implements Pet{

 //must override all abstract methods from Parent class
 void setLegs() {
  System.out.println("i have four legs");

 //must override all methods from an interface
 public void tellWhereAreYouFrom(String place) {
  System.out.println("My place is"+place);

 public String tellYourWeight(String weight) {
  return weight;




Popular posts from this blog

Selection Sort in JAVA

What is selection sort? It is a sorting technique that is based on the partitioning of array into two parts, sorted and unsorted. The process is:- 1) Find minimum element in unsorted array. 2) Swap the element at the end of sorted array. when i=0 we don't have any sorted array so element will be replaced from first element later the sorted array end will growas the index. Steps:- underline elements are sorted array, the length is increasing with each minimum element added at the end. minimum element11 11 25 12 22 64 minimum element12 11 12 25 22 64 minimum element22 11 12 22 25 64 minimum element25 11 12 22 25 64 minimum element64 11 12 22 25 64 Program:- package sorting ; public class SelectionSort { //function to print array public static void print ( int [] arr ) { for ( int i = 0 ; i <= arr . length - 1 ; i ++) System . out . print ( " " + arr [ i ]); System . out . printl

Best LeetCode Lists for Interviews

Here is a list of some of the best questions asked in interviews:-  Must do 75 Must do 60 Must do medium:- Must do Easy:- Graph:-  Dynamic Programming:-  FaceBook interviews:-  Amazon Interviews:-  Google Interviews:-