abstract keyword (An abstract method is a method that is declared without an implementation)
An abstract class is a class that is declared abstract—it may or may not include abstract methods. If a class contains any abstract methods, it must be declared an abstract class. Abstract classes cannot be instantiated, but they can be subclassed.
A class that is not abstract is called a concrete class.
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon).
//In Animal.java file
public abstract class Animal
{
private String type;
public abstract void sound(); //it is meaningless to define sound for animal as a general concept
public Animal(String t)
{
type = t;
}
public String getType()
{
return type;
}
public String toString()
{
return "I am a " + type;
}
public boolean equals(Animal other)
{
return type.equals(other.getType());
}
}
//In Cat.java file
public class Cat extends Animal
{
public Cat()
{
super("cat");
}
public void sound()
{
System.out.println("Meow");
}
}
//In Dog.java file
public class Dog extends Animal
{
public Dog()
{
super("dog");
}
public void sound()
{
System.out.println("Woof");
}
}
//In AnimalSound.java file
public class AnimalSound
{
public static void main(String[] args)
{
Animal myCat = new Cat();
Animal myDog = new Dog();
System.out.println(myCat);
myCat.sound();
System.out.println(myDog);
myDog.sound();
System.out.println(myCat.equals(myDog));
}
}