This article highlights the differences between a class and an interface in Java.
Class:
A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:
1. Modifiers: A class can be public or has default access.
2. Class name: The name should begin with a initial letter (capitalized by convention).
3. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
4. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
5. Body: The class body surrounded by braces, { }.
Constructors are used for initializing new objects. Fields are variables that provides the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.
Example:
// Java program to demonstrate Class
// Class Declaration
public class Dog {
// Instance Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class
public Dog(String name, String breed,
int age, String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
// method 1
public String getName()
{
return name;
}
// method 2
public String getBreed()
{
return breed;
}
// method 3
public int getAge()
{
return age;
}
// method 4
public String getColor()
{
return color;
}
@Override
public String toString()
{
return ("Hi my name is "
+ this.getName()
+ ".\nMy breed, age and color are "
+ this.getBreed() + ", "
+ this.getAge() + ", "
+ this.getColor());
}
public static void main(String[] args)
{
Dog tuffy = new Dog("tuffy", "papillon",
5, "white");
System.out.println(tuffy.toString());
}
}
Interface:
Differences between a Class and an Interface:
Class | Interface |
The keyword used to create a class is “class” | The keyword used to create an interface is “interface” |
A class can be instantiated i.e, objects of a class can be created. | An Interface cannot be instantiated i.e, objects cannot be created. |
Classes does not support multiple inheritance. | Interface supports multiple inheritance. |
It can be inherit another class. | It cannot inherit a class. |
It can be inherited by another class using the keyword ‘extends’. | It can be inherited by a class by using the keyword ‘implements’ and it can be inherited by an interface using the keyword ‘extends’. |
It can contain constructors. | It cannot contain constructors. |
It cannot contain abstract methods. | It contains abstract methods only. |
Variables and methods in a class can be declared using any access specifier(public, private, default, protected) | All variables and methods in a interface are declared as public. |
Variables in a class can be static, final or neither. | All variables are static and final. |
0 comments:
Post a Comment