3.1 Inheritance Basics
Inheritance
Inheritance is the process of acquiring properties and methods from another class.
Advantages of Inheritance
- Code reusability
- Easy maintenance
- Method extension
- Reduces code duplication
- Supports polymorphism
Syntax
class Parent{
}
class Child extends Parent{
}
Explanation
| Class | Meaning |
|---|---|
| Parent | superclass/base class |
| Child | subclass/derived class |
Example of Inheritance
class Animal{
void eat(){
System.out.println("Eating");
}
}
class Dog extends Animal{
void bark(){
System.out.println("Barking");
}
public static void main(String args[]){
Dog d = new Dog();
d.eat();
d.bark();
}
}
Types of Inheritance
1. Single Inheritance
class A{
}
class B extends A{
}
One child inherits one parent.
2. Multilevel Inheritance
class A{
}
class B extends A{
}
class C extends B{
}
Inheritance chain.
3. Hierarchical Inheritance
class A{
}
class B extends A{
}
class C extends A{
}
Multiple child classes inherit same parent.
4. Multiple Inheritance
Java does not support multiple inheritance using classes.
class A{
}
class B{
}
// Not allowed
class C extends A,B{
}
Reason: ambiguity problem.
Multiple Inheritance using Interface
interface A{
}
interface B{
}
class C implements A,B{
}
3.2 Inheritance and Constructors
Important Rule
Parent constructor executes before child constructor.
Example
class A{
A(){
System.out.println("Parent Constructor");
}
}
class B extends A{
B(){
System.out.println("Child Constructor");
}
public static void main(String args[]){
B obj = new B();
}
}
Output
Parent Constructor
Child Constructor
3.3 super Keyword
Definition
super refers to immediate parent class object.
Uses of super Keyword
- Call parent constructor
- Access parent variable
- Call parent method
1. Calling Parent Constructor
class A{
A(){
System.out.println("Parent");
}
}
class B extends A{
B(){
super();
System.out.println("Child");
}
}
2. Accessing Parent Variable
class A{
int x = 10;
}
class B extends A{
int x = 20;
void show(){
System.out.println(super.x);
}
}
3. Calling Parent Method
class A{
void show(){
System.out.println("Parent Method");
}
}
class B extends A{
void show(){
super.show();
System.out.println("Child Method");
}
}
Difference Between this and super
| this | super |
|---|---|
| refers current object | refers parent object |
| access current class members | access parent members |
3.4 Method Overriding
Definition
When child class provides its own implementation of parent method.
Rules of Overriding
- Method name must be same
- Parameter list must be same
- Inheritance required
Example
class A{
void show(){
System.out.println("Parent");
}
}
class B extends A{
void show(){
System.out.println("Child");
}
public static void main(String args[]){
B obj = new B();
obj.show();
}
}
Method Overloading vs Method Overriding
| Overloading | Overriding |
|---|---|
| same method name | same method name |
| different parameters | same parameters |
| same class | different classes |
| compile time polymorphism | runtime polymorphism |
3.5 Polymorphism
Definition
Polymorphism means one thing having many forms.
Types of Polymorphism
| Type | Meaning |
|---|---|
| Compile Time | Method overloading |
| Runtime | Method overriding |
Compile Time Polymorphism
class Test{
void add(int a,int b){
}
void add(int a,int b,int c){
}
}
Runtime Polymorphism
class A{
void show(){
System.out.println("Parent");
}
}
class B extends A{
void show(){
System.out.println("Child");
}
}
class Main{
public static void main(String args[]){
A obj = new B();
obj.show();
}
}
3.6 Dynamic Binding
Definition
Method call resolved during runtime is called dynamic binding.
Example
A obj = new B();
obj.show();
JVM decides which method to call during runtime.
Static Binding vs Dynamic Binding
| Static Binding | Dynamic Binding |
|---|---|
| compile time | runtime |
| faster | slower |
| method overloading | method overriding |
3.7 final Keyword
Definition
final restricts modification.
1. final Variable
final int x = 10;
Cannot change value.
2. final Method
final void show(){
}
Cannot override.
3. final Class
final class A{
}
Cannot inherit.
3.8 Abstract Classes
Abstract Class
Class declared using abstract keyword.
Properties
- Cannot create object
- Can contain abstract and normal methods
- Used for abstraction
Abstract Method
abstract void run();
No body.
Example
abstract class Animal{
abstract void sound();
}
class Dog extends Animal{
void sound(){
System.out.println("Bark");
}
public static void main(String args[]){
Dog d = new Dog();
d.sound();
}
}
3.9 Access Specifiers
Definition
Access specifiers control accessibility of members.
Types of Access Specifiers
| Specifier | Access |
|---|---|
| public | accessible everywhere |
| private | inside same class only |
| protected | same package + subclass |
| default | same package only |
private Example
class Test{
private int x = 10;
}
Accessible only inside class.
3.10 Interfaces
Interface
Interface is a collection of abstract methods.
Properties
- Supports full abstraction
- Cannot create object
- Methods are public and abstract by default
- Variables are public static final by default
Syntax
interface Shape{
void draw();
}
Implementing Interface
interface Shape{
void draw();
}
class Circle implements Shape{
public void draw(){
System.out.println("Drawing");
}
}
Multiple Interface Example
interface A{
void show();
}
interface B{
void display();
}
class Test implements A,B{
public void show(){
}
public void display(){
}
}
Abstract Class vs Interface
| Abstract Class | Interface |
|---|---|
| partial abstraction | full abstraction |
| can have normal methods | mainly abstract methods |
| extends keyword | implements keyword |
| single inheritance | multiple inheritance possible |
Most Important Practical Programs
- Single inheritance program
- Multilevel inheritance
- Hierarchical inheritance
- Method overriding
- Runtime polymorphism
- super keyword
- Abstract class program
- Interface implementation
- Multiple interface program
Most Important Theory Questions
- Define inheritance with advantages.
- Explain types of inheritance.
- Explain super keyword.
- Difference between overloading and overriding.
- Explain polymorphism.
- Explain dynamic binding.
- Explain final keyword.
- What is abstract class?
- Difference between abstract class and interface.
- Explain access specifiers.
Important Viva Questions
Does Java support multiple inheritance?
No, through classes. Yes, through interfaces.
Can we create object of abstract class?
No.
Can abstract class have constructor?
Yes.
Can interface have constructor?
No.
Can final method be overridden?
No.
Can private method be overridden?
No.
Which keyword is used for inheritance?
extends
Which keyword is used for interface implementation?
implements
0 comments:
Post a Comment