Abstract Java Example Code
Employee contractor and fulltimeemployee example in this example we create an abstract employee class and which contains abstract calculatesalary method.
Abstract java example code. Public abstract int sumofthree int n1 int n2 int n3. Following is an example of the abstract method. Regular class extends abstract class class demo extends sum if i don t provide the implementation of these two methods the program will throw compilation error.
Abstract void printstatus no method body and abstract. Abstract class abstract class sum these two are abstract methods the child class must implement these methods public abstract int sumoftwo int n1 int n2. A method which is declared as abstract and does not have implementation is known as an abstract method.
Remainder of class definition. Abstract void printstatus. Class main public static void main string args pig mypig new pig.
Example public abstract class employee private string name. Instead of curly braces an abstract method will have a semoi colon at the end. Example of abstract method.
Let the subclasses extend employee class and implement a calculatesalary method. Regular method public void sleep system out println zzz. Subclass inherit from animal class pig extends animal public void animalsound the body of animalsound is provided here system out println the pig says.
Create a pig object mypig. Regular method public void disp system out println method of class sum. Abstract method in java.