From 6b6f88c64d9a4b2d26de5725dee24f3c459fb93c Mon Sep 17 00:00:00 2001 From: bk2dcradle Date: Sun, 4 Oct 2015 01:47:09 +0530 Subject: Added abstract classes --- java.html.markdown | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/java.html.markdown b/java.html.markdown index 928eb39f..2f41be81 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -451,6 +451,74 @@ public class ExampleClass extends ExampleClassParent implements InterfaceOne, public void InterfaceTwoMethod() { } } + + +// Abstract Classes +// Abstract Class declaration syntax +// abstract extends { +// // Constants and variables +// // Method declarations +// } + +// Methods can't have bodies in an interface, unless the method is +// static. Also variables are NOT final by default, unlike an interface. +// Also abstract classes CAN have the "main" method. +// Abstract classes solve these problems. + +public abstract class Animal +{ + public abstract void makeSound(); + + // Method can have a body + public void eat() + { + System.out.println("I am an animal and I am Eating."); + // Note: We can access private variable here. + age = 30; + } + + // No need to initialise, however in an interface + // a variable is implicitly final and hence has + // to be initialised. + private int age; + + public void printAge() + { + System.out.println(age); + } + + // Abstract classes can have main function. + public static void main(String[] args) + { + System.out.println("I am abstract"); + } +} + +class Dog extends Animal +{ + // Note still have to override the abstract methods in the + // abstract class. + @Override + public void makeSound() + { + System.out.println("Bark"); + // age = 30; ==> ERROR! age is private to Animal + } + + // NOTE: You will get an error if you used the + // @Override annotation here, since java doesn't allow + // overriding of static methods. + // What is happening here is called METHOD HIDING. + // Check out this awesome SO post: (http://stackoverflow.com/questions/16313649/) + public static void main(String[] args) + { + Dog pluto = new Dog(); + pluto.makeSound(); + pluto.eat(); + pluto.printAge(); + } +} + ``` ## Further Reading -- cgit v1.2.3 From d8efd3ba3416669177683887b2822eb5d56c157b Mon Sep 17 00:00:00 2001 From: bk2dcradle Date: Mon, 5 Oct 2015 01:38:02 +0530 Subject: Modified as said to by a Collaborator --- java.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/java.html.markdown b/java.html.markdown index 2f41be81..89a710ee 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -477,9 +477,9 @@ public abstract class Animal age = 30; } - // No need to initialise, however in an interface + // No need to initialize, however in an interface // a variable is implicitly final and hence has - // to be initialised. + // to be initialized. private int age; public void printAge() @@ -509,7 +509,7 @@ class Dog extends Animal // @Override annotation here, since java doesn't allow // overriding of static methods. // What is happening here is called METHOD HIDING. - // Check out this awesome SO post: (http://stackoverflow.com/questions/16313649/) + // Check out this awesome SO post: http://stackoverflow.com/questions/16313649/ public static void main(String[] args) { Dog pluto = new Dog(); -- cgit v1.2.3