Abstraction in Java
- Data abstraction is the property by virtue of which only the essential details are displayed to the user.
- Data abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details.
- In Java, abstraction is achieved by abstract classes and interfaces.
Abstract classes and abstract methods
- An abstract class is a class that is declared with an abstract keyword.
- An abstract method is a method that is declared inside an abstract class with an abstract keyword without implementation.
- An abstract class may or may not have all abstract methods. Some of them can be concrete methods.
- A method defined abstract must always be redefined in the subclass, OR either make the subclass itself abstract.
- There can be no object of an abstract class.
- We can change the value of a variable declared in an abstract class.
- An abstract class can have class members like private, protected etc.
Output :
This is a constructor of GrandParent class.
This is a constructor of Parent class.
I am a single child of my parents, so this all patrimony will be mine.
This patrimony is with me for 10 years.
This is a constructor of GrandParent class.
This is a constructor of Parent class.
This is a constructor of Child1 class.
I will take this home and a small piece of plot from patrimony.
This patrimony is with me for 15 years.
This is a constructor of GrandParent class.
This is a constructor of Parent class.
This is a constructor of Child2 class.
I will take a bigger portion of plot from the patrimony.
This patrimony is with me for 10 years.
Interfaces in Java
- Like a class, an interface can have methods and variables, but methods declared in an interface are by default abstract.
- To declare an interface, use interface keyword.
- To implement interface in any class, use implements keyword (just like extends keyword).
- If a class implements an interface, then it must define all the methods declared in that interface, otherwise the class must be declared abstract.
- Like an abstract class, we can't create any object of an interface.
- We can't change the value of a variable declared in an interface.
- Members of a Java interface are public by default.
- If we declares a method by default and static keyword (i.e. default and static methods) in the interface, then we can also implement it in the interface.
Output :
Realme 8s 5G (Universe Purple)
MediaTek Dimensity 810 5G Processor
128 GB ROM expandable upto 1 TB and 8 GB RAM
64MP + 2MP + 2MP and 16MP front camera
This is a 5G smartphone.
The total quantity of this piece is: 100
Vivo V21e (Sunset Jazz)
MediaTek Dimensity 700 Processor
128 GB ROM expandable upto 1 TB and 8 GB RAM
64MP + 8MP and 32MP front camera
This is a 4G smartphone.
The total quantity of this piece is: 100
Comments
Post a Comment