Docsity

Multiple and Hierarchical Inheritance in Java programming, Study notes of Object Oriented Programming

Partial preview of the text

Download Multiple and Hierarchical Inheritance in Java programming and more Study notes Object Oriented Programming in PDF only on Docsity!

MULTIPLE

AND

HIERARCHICAL

INHERITANCE

By-Ms.Aquilla Afonso

Assistant Professor

St.Xavier’s College-

Goa

Multiple Inheritance:  In Multiple Inheritance, one class extending more than one class.  Multiple inheritance in java is the capability of creating a single class with multiple superclasses  Java does not support multiple inheritance.

What is an interface in Java?

Interface looks like a class but it is not a class. An interface

can have methods and variables just like the class but the

methods declared in interface are by default abstract (only

method signature, no body.

 Also, the variables declared in an interface are public, static

& final by default.

Syntax:

interface MyInterface

public void method1();

public void method2();

interfaces  Note: Since methods in interfaces do not have body, they have to be implemented by the class before you can access them.The class that implements interface must implement all the methods of that interface.Also, java programming language does not allow you to extend more than one class, However you can implement more than one interfaces in your class.

Implementing multiple inheritance in Java class result extends Student implements sport { public void spmarks() { System.out.println("sports marks"+sp); } void disp() { putmarks(); spmarks(); int total=marks1+marks2+sp; System.out.println("total is"+total); WHAT IS THE OUTPUT??? Create a main and make object of the result class..........

Real world example interface Flour { String weightage="200gm"; void disp(); } interface Butter { String quant="120gm"; String flavours="rich"; void display(); } interface Milk { String quantity="100 ML"; String texture="moist"; void display_info(); } class Chocolate { String colour="chocolate brown"; String taste="sweet"; String flavour="chocolaty"; void display_data() { System.out.println("The chocolate ingredient gives "+colour+" colour,gives "+taste+" taste and "+flavour+" flavour to the ingredient."); }

Real world example public void info() { System.out.println(); System.out.println("The brownie contains "+weightage+" of flour,"+quant+" of butter and "+quantity+" of milk. Butter makes the product "+flavours+",whereas milk gives "+texture+" texture to the product"); } } public class Multiple { public static void main(String[] args) { Brownie b=new Brownie(); b.disp(); b.display(); b.display_info(); b.display_data(); b.info(); }

Hierarchical inheritance  When more than one classes inherit a same class then this is called hierarchical inheritance. For example class B, C and D extends a same class A. B

A

C D

Implementation of hierarchical inheritance Class Main { public static void main(String args[]) { B obj1 = new B(); C obj2 = new C(); D obj3 = new D(); //All classes can access the method of class A obj1.methodA(); obj2.methodA(); obj3.methodA(); } } WHAT IS THE OUTPUT???

Example :hierarchical inheritance Class one { int x=10,y=20; void disp() { System.out.println("method of Class one"); System.out.println(“value of x“+x); System.out.println(“value of y“+y); } } Class two extends one { void add() { System.out.println("method of Class two"); System.out.println(“addition”+ (x+y)); } one.java two.java

Real world example class Book { String title; String author; int no_of_pages; void display_info() { System.out.println("The Book title is "+title+" , of the author "+author+" having number of pages "+no_of_pages); } Book() { title="Harry Potter"; author="J.K. Rowling"; no_of_pages=569; } }

Real world example

class EBook extends Book

String book_type;

int price;

String book_size;

EBook(String b,int p,String

bs)

book_type=b;

price=p;

book_size=bs;

void display() { System.out.println("T his is an "+book_type+" ,of the price "+price+" having file size in mb "+book_size); } }

Real world example public class Hierarchical { public static void main(String[] args) { Book b=new Book(); System.out.println("BASE CLASS PAPER BOOK: "); b.display_info(); EBook e=new EBook("Virtual Book",499,"2 MB"); System.out.println("DERIVED CLASS EBOOK: "); e.display_info(); e.display(); PaperBook p=new PaperBook(); System.out.println("DERIVED CLASS PAPER BOOK: "); p.display_info(); p.display(); } }

THANK YOU !!!