Abstract class is the class which can hold fully implemented methods and unimplemented methods. That’s the major difference between Interface vs Abstract class.
Have a look on below section 1.0, which demonstrate template for Abstract class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
Section 1.0 package technical.jungle.com.abstractdemo; public abstract class TechnicalJungleAbstract { void showValue(){ System.out.println("www.technicaljungle.com \t Value = " + someComplexCalculation()); } private int someComplexCalculation(){ /*This method is hidden from classes, who all are going to inherit this abstract class.*/ return (int) (Math.random()*10) * setValue(); } public abstract int setValue(); } Section 1.1 package technical.jungle.com.abstractdemo; public class JungleFirst extends TechnicalJungleAbstract{ public static void main(String[] args) { JungleFirst first = new JungleFirst(); first.showValue(); } @Override public int setValue() { return 1; } } Output : www.technicaljungle.com Value = 5 ( this value will be random) |
Some key points about above demo:
- abstract keyword is used.
- Object can’t be created for abstract class.
- If any method is ‘abstract’ then it’s must to add keyword ‘abstract’ for the class.
- If any class is abstract then it’s doesn’t mandatory that any/all method will be abstract.
- Private method someComplexCalculation() is implemented method.
- setValue() is abstract method.
public abstract int setValue();
That’s the reason the class ‘TechnicalJungleAbstract’ became ‘abstract’.
This method is getting overridden by the class who is inheriting abstract class.
Section 1.1 is inhering abstract class. So this class is only overriding setValue() class. - Section 1.1 is overriding setValue() method and its public. Its must be public only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Section 1.2 package technical.jungle.com.abstractdemo; public abstract class JungleSecond extends TechnicalJungleAbstract { abstract public int setValue(); public void display() { System.out.println("www.technicaljungle.com -> display()"); } } Section 1.3 package technical.jungle.com.abstractdemo; public class JungleFirst extends JungleSecond { public static void main(String[] args) { JungleFirst first = new JungleFirst(); first.showValue(); first.display(); } @Override public int setValue() { return 1; } } |
Some key points about above demo:
If developer doesn’t want to implement/override ‘abstract’ methods ( of abstract class) then add ‘abstract’ before method and class both.
- JungleFirst class is further extending ‘abstract’ class only. So method and class both are abstract.
- Finally JungleSecond class is implementing all available abstract methods.
- Class JungleSecond has main method and object is creating created for JungleSecond class only.
- Mind it, no where, abstract class has been instantiated.
Pingback: Java Made Easy - Technical Jungle
Pingback: ContentObserver and ContentObservable | Android | Details - Technical Jungle