Saturday 23 December 2017

Difference Between Abstract Class And Interface In Java?

Abstract class (Incomplete class)

Abstract class contains abstract (incomplete)and nonabstract(complete) methods

Example: 

public abstract class LearningPoint92
{
Public abstract void show ();         //Abstract method

Void display ()                               //Non Abstract method
{
System.out.println (“Hello”);
}
}

The abstract keyword is used to declare abstract class

extends keyword is used to extend an abstract class and abstract method will be implemented in sub class

Abstract classes methods can have access modifiers as public, private, protected, static

Abstract class can provide the implementation of interface

Abstract class achieves partial abstraction (0 to 100%)

Abstract class can have final, non-final, static and non-static variables.

Abstract class doesn't support multiple inheritance

Example:

abstract class LearningPoint92

public  abstract void show();  //abstract method


void display() //non abstract method
{
System.out.println("Hello");
}


public class Demo extends LearningPoint92

public void show ()
{
System.out.println("Welcome to LP92");

public static void main(String args[]){ 

 Demo Obj = new Demo (); 

// LearningPoint92 Obj=new Demo();

 Obj.display(); 

 Obj.show();
}

Output:

Hello
Welcome to LP92

 Interface

Interface contains  only and only abstract methods

Example:

public interface LearningPoint92
{
void show ();                  //Abstract method
}

The interface keyword is used to declare interface

implements keyword is used to implement interfaces abstract method will be implemented in sub class

Interface methods are implicitly public and abstract (by default all the methods in interface are public)

Interface can't provide the implementation of abstract class

Interface achieves fully abstraction (100%).

Interface has only static and final variables.

Interface supports multiple inheritance.

Example:

interface LearningPoint92
void display(); 

public class Demo implements LearningPoint92

public void display ()
{
System.out.println("Hello,Welcome to LP92");
 
public static void main(String args[]){ 
//Demo obj = new Demo (); 

LearningPoint92 obj = new Demo (); 
 obj.display(); 
 } 

Output:
Hello,Welcome to LP92

Note: 
You can’t create the object of abstract class but create reference.
Superclass reference refer to sub class object is called upcasting and using upcasting you can call methods of super class like

LearningPoint92 obj = new Demo (); 
LearningPoint92 obj ->Superclass reference

new Demo (); -> sub class object

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...