This is the presentation file about inheritance in java. You can learn details about inheritance and method overriding in inheritance in java. I think it's can help your. Thank you.
Size: 77.11 KB
Language: en
Added: Nov 16, 2020
Slides: 15 pages
Slide Content
I nheritance I n J ava presented by: Rahul Paul
I nheritance : In java inheritance is a mechanism which one object can get all the properties and behaviours of a parent object. In inheritance there is a relationship between two classes. That’s why it is-a relationship. For example : Bike, car, bus this are vehicle. That’s why the child of vehicle are bike, car, bus and vehicle is the parent of this objects. Vehicle Bike, car, bus
J ava inheritance : The important of inheritance in java program is code reusability. We can reuse the function of parent class at child class. In java we use Extends keyword for inheritance. Subclass and Superclass in inheritance.
I nheritance Syntax : class Parent { //code } class Child extends Parent { //code } Parent Child
S imple program class super { Public void display() { System.out.println (“I am parent class”); } } class sub extends super { public static void main(String args []) { sub message = new sub() message.display (); } } Super I am from parent class sub Parent class Child classs Output: I am parent class
T ypes of inheritance: Single inheritance. Multiple inheritance. Multi-level inheritance. Hierarchical inheritance. Hybrid inheritance. Note: In java Multiple inheritance is not supported.
S ingle inheritance: Single inheritance is the most easy type to understand. In single inheritance a class extends another only one class. The child class inherit only parents class. The subclass inherit the objects of superclass. Example: A cat is a animal. So a cat only can inherit the objects of animal class. Animal (superclass) Cat (subclass)
S ample program of single inheritance : class message_super { Public void display() { System.out.println (“I am from superclass”); } } class message_sub extends message_super { public static void main(String args []) { Message_sub message = new message_sub () Message.display (); } } Output: I am from superclass message_super I am from superclass message_sub
M ultilevel inheritance When a class extends a class, and that class extends another class and its object then it call multilevel inheritance. Example: a class A is parent class. Class B is inherit the objects of class A. Another one class c is child of class B. class c can inherit the objects of class C. A B C Base class Intermediary class Child class
S ample program of multilevel inheritance : class a { int data= 15; } class b extends a { } class c extends b { public void display() { System.out.println (“number is:”+data); } public static void main(String args []) { c num = new c() num.display (); } } Output: number is: 15 a Data=15 b c
H ierarchical inheritance: When one superclass can be inherited by more than one subclass then it is called hierarchical inheritance. Example: a class A is parent class. Class B is inherit the objects of class A. Another one class c also can inherit the objects of class A. A B C
Syntax of Hierarchical inheritance: Class Food { //code } class Rice extends Food{ //code } class Fruit extends Food{ //code } Food Rice Fruit
Inheritance Method overriding: When a method is already in parent class but also declaring in child class, then it is known as Method Overriding. Note: But when it is ‘Final’ method it cannot overriding. Example: A parent class of animal has method ‘eat’. Also a child class tiger has method ‘eat’. Animal eat() Tiger eat()
Sample program of method overriding: class animal { public void display() { System.out.println (“I am animal”); } } class tiger extends animal { public void display() { System.out.println (“I am tiger”); } public static void main(String args []) { tiger t = new tiger(); t.display (); } } Output: I am tiger