Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sampath Kumar.S Assistant Professor/IT, SECE

Similar presentations


Presentation on theme: "Sampath Kumar.S Assistant Professor/IT, SECE"— Presentation transcript:

1 Sampath Kumar.S Assistant Professor/IT, SECE
Inheritance in JAVA Sampath Kumar.S Assistant Professor/IT, SECE

2 Inheritance in Java Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent 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.

3 Why use Inheritance? For Method Overriding (So Runtime Polymorphism).
For Code Reusability.

4 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.

5 Example of Inheritance

6 Example Program: class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println("Programmer salary is: “ +p.salary); System.out.println("Bonus of Programmer is: “ +p.bonus); } } Output: Programmer salary is: Bonus of programmer is:10000

7 Types of Inheritance Three (Basic)types of Inheritance are there: Note: Multiple inheritance is not supported in java in case of class.

8 Multiple and Hybrid Note: Multiple and Hybrid is supported through interface only.

9 Why multiple inheritance is not supported in java?
class A { void msg() { System.out.println("Hello"); } } class B { System.out.println("Welcome"); } class C extends A,B { //suppose if it were Public Static void main(String args[]){ C obj=new C(); obj.msg(); //Now which msg() method would be invoked?

10 Sampath Kumar.S, AP/IT

11 Thank You  Sampath Kumar.S, AP/IT


Download ppt "Sampath Kumar.S Assistant Professor/IT, SECE"

Similar presentations


Ads by Google