Java: A simpler abstract class example

This entry is part 49 of 54 in the series Learn Java

An abstract class provides a contract between a service provider and its clients, but at the same time an abstract class can provide partial implementation.

Methods that must be explicitly overridden can be declared abstract.

package javaapplication41;

public class MainMusic {

    public abstract class Music {

        public abstract int live();            
        
    }

    static class Rock extends Music {

        protected int ls;                              

        public Rock(int l) {
            this.ls = l;
        }          

        public int yearly() {
            return ls * 52;
        }          

        public int live() {
            return ls;
        }          
    }

    public static void main(String[] args) {

        Rock r = new Rock(5);

        System.out.println(r.yearly());
        System.out.println(r.live());
    }
}

abstract1

Series Navigation<< Java: An array of objects
Java: Interface >>

Leave a comment

Leave a Reply