abstract class defina code example
Example 1: abstraction in java
Abstraction is defined as hiding internal implementation and showing only
necessary information.
// abstract class
abstract class Addition
{
// abstract methods
public abstract int addTwoNumbers(int number1, int number2);
public abstract int addFourNumbers(int number1, int number2, int number3, int number4);
// non-abstract method
public void printValues()
{
System.out.println("abstract class printValues() method");
}
}
class AbstractMethodExample extends Addition
{
public int addTwoNumbers(int number1, int number2)
{
return number1 + number2;
}
public int addFourNumbers(int number1, int number2, int number3, int number4)
{
return number1 + number2 + number3 + number4;
}
public static void main(String[] args)
{
Addition add = new AbstractMethodExample();
System.out.println(add.addTwoNumbers(6, 6));
System.out.println(add.addFourNumbers(8, 8, 3, 2));
add.printValues();
}
}
Example 2: abstract class example in java
//abstract parent class
abstract class Animal{
//abstract method
public abstract void sound();
}
//Dog class extends Animal class
public class Dog extends Animal{
public void sound(){
System.out.println("Woof");
}
public static void main(String args[]){
Animal obj = new Dog();
obj.sound();
}
}