Java was developed by James Gosling in 1995 at Sun microsystems .Java15 was released on Sept 20 and is currently the latest version of java.
Java is a programming language and a platform .It is based on the concept of OOP’s(Object Oriented Programming).OOP’s involves creating objects which contain both the data and method.Here object means a real world entity like pen ,family inheritance etc.Java have classes ,objects,methods.
Classes can be considered as the blueprint for creating objects .For example if we want to build a house or say car then we should have the blueprint of the following house or car .Here car and house are the objects and creating a blueprint of them will be our class. Just like objects we have our another concept called as methods in java .As the name suggests, method is a set of code which can perform a certain task when grouped together.
Why Java?
Java is a popular language which shows no sign of declining in popularity.Most developers pick this language as their first programming language as it is easy to learn and has simple syntax in English.
Example-Here’s a simple Hello World example
Uses of Java
1.Building Android apps and web applications
Most of the applications are written in java using the Android Api of google.Many government ,education institutions,business companies
Have their web application built in java. Spotify,Twitter,gmail are built on Java.
2.Software tools
There are many software tools like Eclipse ,Intellij IDEA that are written and developed in java.
There is a large scope for jobs in Java .According to a report an average salary of java developer is around 10-20Lac per annum.
Inheritance
Inheritance can be defined as a process where one class inherits the property(methods and fields) of another class. With the help of inheritance, We can make the information manageable in hierarchical order.
For example, let us consider a family that includes a father, son, mother. Some properties or say traits of the father will get inherit to son.
Similarly, Some properties of the mother will get inherit by the son. Therefore the son will have some properties of the father and some properties of the mother. Here, father and mother – Super classes and son – subclass i.e class which inherit(son) the properties of another class is the subclass.
whereas the class whose properties are inherited(father, mother) are known as the superclass. We use the extends keyword to inherit the property of a class.
The syntax is as follows-
Now let us implement the above example in our code-
Note:- A subclass inherits all the properties (nested classes, methods, fields) from its superclass. We cannot inherit constructors of the superclass as they are not a member of the superclass but we can invoke the constructors of the superclass using a subclass.
Super Keyword | Inheritance
The super keyword refers to the superclass(parent class). It is use in calling the methods and variables of the superclass in the child class.
we can also access the constructor of the superclass with the help of the super keyword.
Here’s a sample code using the Super keyword.
Let us consider a class car that will have a color variable and sound method. Similarly, let us consider a car company, say Maruti, and assume this as a child class(it will be inheriting all the properties of superclass) of the superclass (car). We can inherit the color of the superclass by the following code.
Types of inheritance
There are three types of inheritance in java based on the class: Single, multilevel, hierarchical.
Single Inheritance
Single inheritance is a type of inheritance in which one class can inherit another class.
For example: Let us assume an animal class as a superclass. As we know that dogs are also animals, therefore let us inherit animal class(superclass) by dog class(subclass).
source-GeeksforGeeks
Multilevel Inheritance
Multilevel inheritance is a type of inheritance in which we can perform multiple inheritances.
For example, our father might have inherited some properties from our grandfather and our grandfather might have inherited some properties from our great grandfather. This is multilevel inheritance.
Hierarchical Inheritance
When two or more classes inherit a single class, it is pronounce to be – hierarchical inheritance.
For example-let, our child class inherits grandfather and our father’s class inherit grandfather. So we have two classes inheriting the class grandfather.
Why java does not support multiple inheritances?
Multiple inheritance is a type of inheritance in which a subclass can inherit properties of two or more than two-parent class. If we try multiple inheritances in java it throws a compilation error, this is due to the fact that the compiler doesn’t know which class method to give priority and which class method to call. So, as to avoid confusion java does not support multiple inheritances.
Why do we need inheritance?
With the help of inheritance we don’t need to write the same code, again and again, we just need to inherit the class and reuse the code, this saves time and makes our code short.
Like in the above example we have not written the code again and again we have reused the code by inheriting the superclass and using the same methods for the subclass.
Abstraction
Abstraction is the process of hiding the implementation details which means we can see the result but not the process or the implementation part.
It is Known – the process of identifying only the require characteristics of an object and ignoring the irrelevant details.
The keyword abstract – to declare the abstract class and method.
For example-In the ATM when we request money, the machine undergoes some process and then returns us the requested money. So only requested money is available to us and the whole implementation part of how aTM process the request money is not visible to us.
Note:-
- We can have abstracts and non-abstracts methods in an abstract class.
- There is no object of abstract class i.e we cannot directly instantiate the abstract method with a new operator.
- Method defined abstract must be redefine in the subclass thus making overriding compulsory.
Why do we need abstraction?
Abstraction basically provides the details that are useful for the user and hides the unnecessary details from the users. Like in the above example ATM will only return the requested money and won’t show the process which is not necessary for the user.
Conclusion
Inheritance and abstraction are important aspects of the java language. Abstraction is one of the key elements in developing a good software design, moreover, abstraction hides the unnecessary details from the user and reduces the complexity of programs. Inheritance on the other hand reduces the code length as we can use the same code again and again by inheriting it from the superclass(parent class).Java doesn’t support multiple inheritance but we can use multiple inheritance in java by using interfaces.
Written by: Aakarsh Vats
Reviewed By: Soutik Maity
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs