Inheritance in Java
- Making another class from the existing class is known as inheritance.
- Inheritance is utilized to accomplish dynamic official and reusability.
- The class which is acquired is known as the superclass or parent class and the class got from the parent class is known as a subclass or youngster class.
- Inheritance is executed utilizing broadens catchphrase.
class parent_class_name extends child_class_name
{
//body
}
- Simply investigate sentence structure.
- Every one of the properties of parent class come to kid class in the event that they are not private.
- A model about how inheritance is executed in java is given beneath.
class parent
{
String
name=”Mark”;
}
class child extends parent
{
void
show()
{
System.out.println(name);
}
public
static void main(String…s)
{
child
c=new child();
c.show();
}
}
- In the above model variable name is default along these lines it is open in youngster class.
- In the event that we make the name as private at that point, it will show mistake like “name has private access in the parent”.
Sorts of Inheritance in Java
- There are three sorts of inheritance in java: single, staggered and various levelled inheritance.
- In java, numerous inheritance isn’t upheld by class. Numerous inheritance can be actualized by Interface, later on, we will talk about it.
Why numerous inheritance isn’t upheld in java?
- So as to diminish the multifaceted nature and uncertainty, numerous inheritance isn’t bolstered in java.
- Consider a circumstance where we have three classes A, B and C. Class C acquire class An and B. On the off chance that classes An and B have a strategy with the same model and when we call this technique in class C then it will cause vagueness in light of the fact that the compiler will be befuddled that whether technique from class An is called or strategy from class, B is called.