An abstract class is one that has been declared to be abstract. It can support both abstract and non-abstract methods. It needs to be expanded and its method put into action. It cannot be created.
Syntax
abstract class Exam{}
// Java Abstract classes example
abstract class nano{ abstract void Speed(); public void Weight() { System.out.println("250 Kg"); } } class Vehicles extends nano{ @Override void Speed() { System.out.println("90 kmph"); } } class Main{ public static void main(String[] args) { Vehicles vehicles=new Vehicles(); vehicles.Speed(); vehicles.Weight(); } }
Output:
90 kmph
250 Kg
An abstract methods are those methods that is declared with abstract keyword but does not have implementation.
Syntax
abstract void Exam(); // does not have implementation
abstract class Bike{ abstract void MaxSpeed(); } class Hero extends Bike{ @Override void MaxSpeed() { System.out.println("Max Speed is : 140 kmph"); } } class Main{ public static void main(String[] args) { Hero vehicles=new Hero(); vehicles.MaxSpeed(); } }
Output:
Max Speed is : 140 kmph
Case 1
Java permits constructors to be present in abstract classes. When an instance of an inherited class is produced, a constructor of an abstract class is also invoked.
abstract class Bike{ public Bike() { System.out.println("Bike Class Constructor (Base Class)"); } abstract void MaxSpeed(); } class Hero extends Bike{ public Hero(){ System.out.println("Hero Class Constructor (Derived Class)"); } @Override void MaxSpeed() { System.out.println("Max Speed is : 140 kmph"); } } class Main{ public static void main(String[] args) { Hero hero=new Hero(); hero.MaxSpeed(); } }
Output:
Bike Class Constructor (Base Class)
Hero Class Constructor (Derived Class)
Max Speed is : 140 kmph
Case 2
Java allows us to have abstract classes without abstract methods. This enables us to develop classes that can only be inherited rather than instantiated.
abstract class Bike{ //abstract class without abstract method void MaxSpeed() { System.out.println("Max Speed is : 140 kmph"); } } class Hero extends Bike{ //This Hero class only inherits the Bike (Base) class methods } class Main{ public static void main(String[] args) { Hero hero=new Hero(); hero.MaxSpeed(); } }
Output:
Max Speed is : 140 kmph
Case 3
If the Child class is unable to implement all of the Parent class's abstract methods, we should declare that Child class abstract so that the next level Child class can implement the remaining abstract method.
abstract class Bike{ abstract void MaxSpeed1(); abstract void MaxSpeed2(); abstract void MaxSpeed3(); } abstract class Hero extends Bike{ // removing abstract from hero class will cause error // to implement bike abstract method @Override void MaxSpeed1() { System.out.println("Max Speed is : 140 kmph"); } } class Honda extends Hero{ @Override void MaxSpeed2() { System.out.println("Max Speed is : 100 kmph"); } @Override void MaxSpeed3() { System.out.println("Max Speed is : 80 kmph"); } } class Main{ public static void main(String[] args) { Honda honda=new Honda(); honda.MaxSpeed1(); honda.MaxSpeed2(); honda.MaxSpeed3(); } }
Output:
Max Speed is : 140 kmph
Max Speed is : 100 kmph
Max Speed is : 80 kmph
Case 4
The abstract keyword can be used to declare both top-level classes (Outer classes) and inner classes as abstract.
abstract class Bike{ abstract class splender{ abstract void MaxSpeed1(); } } class Hero extends Bike{ class brand extends splender{ // innner class brand void MaxSpeed1() { System.out.println("Max Speed is : 140 kmph"); } } } class Main{ public static void main(String[] args) { Hero hero=new Hero(); // Instantiating outer class Hero.brand herobrand =hero.new brand(); // Instantiating inner class herobrand.MaxSpeed1(); } }
Output:
Max Speed is : 140 kmph
Case 5
In an abstract class, we can define static methods that can be called without the need for an object.
abstract class Bike{ abstract class splender{ abstract void MaxSpeed1(); } static void StaticCheck(){ // Static method in abstract class System.out.println("I am Static method from abstract class"); } } class Hero extends Bike{ class brand extends splender{ // innner class brand void MaxSpeed1() { System.out.println("Max Speed is : 140 kmph"); } } } class Main{ public static void main(String[] args) { Hero hero=new Hero(); // Instantiating outer class Hero.brand herobrand =hero.new brand(); // Instantiating inner class herobrand.MaxSpeed1(); Bike.StaticCheck(); // static method called without object creation } }
Output:
Max Speed is : 140 kmph
I am Static method from abstract class
Case 6
In an abstract class, we can also have final methods but that methods cannot be overridden and abstract calss can not have object if created it will cause C.T Error.
abstract class Bike{ final void MaxSpeed(){ System.out.println("Max Speed is : 140 kmph"); } } class Hero extends Bike{ } class Main{ public static void main(String[] args) { // Bike bike=new Bike() // C.T Errror Bike Bike hero=new Hero(); // Instantiating hero class hero.MaxSpeed(); } }
Output:
Max Speed is : 140 kmph
Difference Between Encapsulation and Data Abstraction are mention below
S.N | Abstraction | Encapsulation |
---|---|---|
1. | The process or method of obtaining information is known as abstraction. | The process or method of containing information is known as encapsulation. |
2. | The class information is used by Early Binding to resolve method calling. | Late Binding resolves method calls using the object. |
3. | Problems are resolved at the design or interface level in abstraction. | Problems are resolved at the implementation level in encapsulation. |
4. | The abstraction-supporting objects are encapsulated. | It is not necessary to abstract the encapsulated objects. |
5. | Access to a specific portion of data is made possible by abstraction. | Encapsulation conceals data so that the user cannot directly access it (data hiding). |
6. | Abstraction is a technique for hiding unwanted information. | Encapsulation is a technique that encapsulated data in a single entity or unit and protects data from the outside world. |
7. | The use of abstract classes and interfaces allows us to implement abstraction. | Encapsulation can be implemented using the private, protected, and public access modifiers. |
8. | In abstraction, "what" sould to be done is the main focus. | The emphasis in encapsulation is on "How" it should be done. |
9. | With abstraction, abstract classes and interfaces are used to hide implementation complexities. | In encapsulation, the data is concealed using getter and setter methods. |
Post your comment