DEV Community

vishnu prasath
vishnu prasath

Posted on

Java learning-2

1.Why java is not purely object oriented programming?
->Java is not a purely object oriented programming language because it uses primitive data types such as int,char,bool,etc.
->java is mostly object oriented programming language and not purely object oriented.

Abstract class:
->Java abstract class is a class that cannot be instantiated by itself, it needs to be subclassed by another class to use its properties. An abstract class is declared using the “abstract” keyword in its class definition.

Example:

abstract class Shape
{
  int color;
  //An abstract function
  abstract void draw();
}
Enter fullscreen mode Exit fullscreen mode

Interface:
->Another way to achieve abstraction in Java, is with interfaces.
To access the interface methods, the interface must be "implemented" by another class with the implements keyword

Example for interface:

interface Animal {
  public void animalSound(); // interface method (does not have a body)
  public void run(); // interface method (does not have a body)
}
Enter fullscreen mode Exit fullscreen mode

Example for implements:

public interface Animal {
    void makeSound();
    void eat();
}
public class Dog implements Animal {
    public void makeSound() {
        System.out.println("Woof!");
    }
    public void eat() {
        System.out.println("Dog is eating.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Abstract class vs Interface:
Abstract class->Use when class are closely related,share common code and need to have default behaviour.
Interface->Use when you want to define a contract that unrelated class can implement and when multiple inheritance is needed.

Top comments (0)