Inheritance (Object-Oriented Programming Concepts):
Inheritance is a mechanism in which one object acquires all the properties and behaviour of another
object.The idea behind inheritance is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you reuse (or inherit) methods and fields, and you add new methods and fields to adapt your new class to new situations.
Inheritance represents the IS-A relationship.
Why use Inheritance?
>For Method Overriding.
>For Code Reusability.
Syntax of Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Note:The keyword extends indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called a superclass. The new class is called a subclass.
Note:superclass also known as parent class and subclass as child class.
Example of inheritance:
class Employee{
int salary=2000;
}
class Developer extends Employee{
int bonus=300;
public static void main(String []args){
Developer d=new Developer ();
System.out.println("Developer salary is:"+d.salary);
System.out.println("Bonus of Developer is:"+d.bonus);
}
}
Output:Developer salary is:2000
Bonus of Developer:300
Note: In the above example,Developer object can access the field of own class as well as of Employee class i.e. code reusability.
Types of Inheritance:
>1. Single
>2. Multilevel
>3. Hierarchical
>4. Multiple (not supported in Java)
>Single: when class A extends class B.
>Multilevel: when class A extends class B and class B extends class C.
>Hierarchical: when class A extend class B and Class C both.
>Multiple: when class A extend class B,class C both.(supported in C++)
Note: Multiple inheritance is not supported in java in case of class.
Que) Why multiple inheritance is not supported in java ?
Ans) To reduce the complexity(ambiguty) and simplify the language, multiple inheritance is not supported in java.
For Example:
class A{
void show() {System.out.println("Hello");}
}
class B{
void show() {System.out.println("JAVA");}
}
class C extends A,B { //suppose if like that
public static void main(String args[]){
C obj=new C();
obj.show();
}
}
Output: compile time error
Note:Error because JVM will confused which show method have to call either of class A or B.
Inheritance is a mechanism in which one object acquires all the properties and behaviour of another
object.The idea behind inheritance is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you reuse (or inherit) methods and fields, and you add new methods and fields to adapt your new class to new situations.
Inheritance represents the IS-A relationship.
Why use Inheritance?
>For Method Overriding.
>For Code Reusability.
Syntax of Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Note:The keyword extends indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called a superclass. The new class is called a subclass.
Note:superclass also known as parent class and subclass as child class.
Example of inheritance:
class Employee{
int salary=2000;
}
class Developer extends Employee{
int bonus=300;
public static void main(String []args){
Developer d=new Developer ();
System.out.println("Developer salary is:"+d.salary);
System.out.println("Bonus of Developer is:"+d.bonus);
}
}
Output:Developer salary is:2000
Bonus of Developer:300
Note: In the above example,Developer object can access the field of own class as well as of Employee class i.e. code reusability.
Types of Inheritance:
>1. Single
>2. Multilevel
>3. Hierarchical
>4. Multiple (not supported in Java)
>Single: when class A extends class B.
>Multilevel: when class A extends class B and class B extends class C.
>Hierarchical: when class A extend class B and Class C both.
>Multiple: when class A extend class B,class C both.(supported in C++)
Note: Multiple inheritance is not supported in java in case of class.
Que) Why multiple inheritance is not supported in java ?
Ans) To reduce the complexity(ambiguty) and simplify the language, multiple inheritance is not supported in java.
For Example:
class A{
void show() {System.out.println("Hello");}
}
class B{
void show() {System.out.println("JAVA");}
}
class C extends A,B { //suppose if like that
public static void main(String args[]){
C obj=new C();
obj.show();
}
}
Output: compile time error
Note:Error because JVM will confused which show method have to call either of class A or B.
No comments:
Post a Comment