implement abstract class java code example
Example 1: abstract class in java
Sometimes we may come across a situation where we cannot provide
implementation to all the methods in a class. We want to leave the
implementation to a class that extends it. In such case we declare a class
as abstract.To make a class abstract we use key word abstract.
Any class that contains one or more abstract methods is declared as abstract.
If we don’t declare class as abstract which contains abstract methods we get
compile time error.
1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method, concrete methods or both.
3)Any class which extends abstarct class must override all methods of abstract
class
4)An abstarct class can contain either 0 or more abstract method.
Example 2: how to make abstract method in java
public abstract class Account { //abstract class //perent class
protected int accountNumber;
protected Customer customerObj;
protected double balance;
//constructor
public Account(int saccountNumber, Customer scustomerObj,double sbalance){
accountNumber = saccountNumber;
customerObj = scustomerObj;
balance = sbalance;
}
// abstract Function
public abstract boolean withdraw(double amount);
}
public class SavingsAccount extends Account { // child class
private double minimumBalance;
// constructor
public SavingsAccount(int saccountNumber, Customer scustomerObj, double sbalance, double sminimumBalance) {
super(saccountNumber, scustomerObj, sbalance);
minimumBalance = sminimumBalance;
}
// Implementation of abstract function in child class
public boolean withdraw(double amount) {
if (balance() > minimumBalance && balance() - amount > minimumBalance) {
super.setBalance(balance() - amount);
return true;
} else {
return false;
}
}
}
Example 3: java abstract class
// abstract class
abstract class Shape
{
// abstract method
abstract void sides();
}
class Triangle extends Shape
{
void sides()
{
System.out.println("Triangle shape has three sides.");
}
}
class Pentagon extends Shape
{
void sides()
{
System.out.println("Pentagon shape has five sides.");
}
public static void main(String[] args)
{
Triangle obj1 = new Triangle();
obj1.sides();
Pentagon obj2 = new Pentagon();
obj2.sides();
}
}
Example 4: 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();
}
}
Example 5: how to create an abstract method in java
abstract class Scratch{
// cannot be static
abstract public void hello();
abstract String randNum();
}
interface Other{
//all methods in an interface are abstract
public void bye();
String randDouble();
}
Example 6: can abstract class have non abstract methods in java
abstract class AbstractDemo { // Abstract class
private int i = 0;
public void display() { // non-abstract method
System.out.print("Welcome to Tutorials Point");
}
}
public class InheritedClassDemo extends AbstractDemo {
public static void main(String args[]) {
AbstractDemo demo = new InheritedClassDemo();
demo.display();
}
}