Inheritance in Java
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
It is 'is-a' relationship between a superclass and its subclasses. This means that an object of a subclass can be used wherever an object of the superclass can be used.
Class Inheritance in java mechanism is used to build new classes from existing classes.
When a Class extends another class it inherits all non-private members including fields and methods
The main purpose is to promote the reuse of code.
The inheritance relationship is transitive: class A extends class B which extends class C. So class A will inherit class C also.
Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.
The java.lang.Object is top of any Class inheritance hierarchy.
Types of Inheritance in java:
Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
* The difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden
* If super class method does not declare an Exception then subclass overridden method can not declare checked exception. So you can only use Unchecked exception.
Other option is to allow Super class to declare
* Checked exception thrown by subclass should be same or subclass of checked exception thrown by parent class.
* Unchecked exception could be different in subclass
* Interfaces extended by subclass should be compatible with interfaces of superclass
'this' keyword is used as a reference to the current object which is an instance of the current class.
The keyword 'super' also references the current object, but as an instance of the current class’s super class.
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
It is 'is-a' relationship between a superclass and its subclasses. This means that an object of a subclass can be used wherever an object of the superclass can be used.
Class Inheritance in java mechanism is used to build new classes from existing classes.
When a Class extends another class it inherits all non-private members including fields and methods
The main purpose is to promote the reuse of code.
The inheritance relationship is transitive: class A extends class B which extends class C. So class A will inherit class C also.
Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.
The java.lang.Object is top of any Class inheritance hierarchy.
Types of Inheritance in java:
- Single Inheritance : When a class inherit just one top class example : class A extends class B
- Multilevel Inheritance: When a class inherit class which also extend another class.
- Heirarchical Inheritance : When multiple classes inherit from one class.
Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.
* The difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden
* If super class method does not declare an Exception then subclass overridden method can not declare checked exception. So you can only use Unchecked exception.
Other option is to allow Super class to declare
ParentException and then child overridden methods can declare any exceptions which are child of ParentException* Checked exception thrown by subclass should be same or subclass of checked exception thrown by parent class.
* Unchecked exception could be different in subclass
* Interfaces extended by subclass should be compatible with interfaces of superclass
this and super keywords:
The two keywords, this and super to help you explicitly name the field or method that you want. Using this and super you can decide whether to call a method or field present in the same class or to call from the immediate superclass.'this' keyword is used as a reference to the current object which is an instance of the current class.
The keyword 'super' also references the current object, but as an instance of the current class’s super class.
Comments
Post a Comment