Inheritance and Type of Inheritance
INHERITANCE :
Inheritance Slide
What is Inheritance:
We can access one class properties into another class using extends keyword and reusable code purpose.
Memory wastage is low.
To reduce Object memory we go for inheritance.
Type Of Inheritance:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
Single Inheritance
Combination Of one Parent class and one child class
Single Inheritance program:
package org.employee;
public class Client {
public void clientName() {
System.out.println("swiggy");
}
}
package org.employee;
public class Company extends Client {
public void companyName() {
System.out.println("HCL");
}
public void companyID() {
System.out.println("hc293l");
}
}
Multilevel Inheritance
Child class is accessing more than one parent class in a tree level structure.
multilevel inheritance:
package org.employee;
public class Client {
public void clientName() {
System.out.println("swiggy");
}
}
package org.employee;
public class Company extends Client {
public void companyName() {
System.out.println("HCL");
}
public void companyID() {
System.out.println("hc293l");
}
}
package org.employee;
public class Emp extends Company {
public void empName() {
System.out.println("jeba");
}
public void empID() {
System.out.println("h1002");
}
public static void main(String[] args) {
Emp s= new Emp();
s.empName();
s.empID();
s.companyName();
s.companyID();
s.clientName();
}
}
Hierarchical Inheritance
Combination of one parent class and more than one child class
Hierarchical inheritance:
package org.bank;
public class RBI {
public void rule() {
System.out.println("rules");
}
}
package org.bank;
public class HDFC extends RBI{
public void feature() {
System.out.println("loan");
System.out.println("saving");
System.out.println("fixed");
}
public static void main(String[] args) {
HDFC h= new HDFC();
h.feature();
h.rule();
}
}
package org.bank;
public class SBI extends RBI {
public void feature() {
System.out.println("loan");
System.out.println("saving");
System.out.println("fixed");
}
public static void main(String[] args) {
SBI s= new SBI();
s.feature();
s.rule();
}
}
Multiple Inheritance
Child class is accessing more than one parent class parallelly.
Java doesn’t support multiple inheritance.
Priority problem.
Syntax error/ compilation error
Hybrid Inheritance
Combination of any Two or more Inheritance.
Assignment:
---------------------
Project :COmputer
Class :Computer
Methods :computerModel()
Class :Desktop
Methods :desktopSize()
Description:
create above 2 class and call all your class methods into the Desktop using single inheritance.
QUESTION 2:
------------
Project :EducationInformation
Package :org.edu
Class :Education
Methods :ug(),pg()
Class :Arts
Methods :bsc(),bEd(),bA(),bBA()
Class :Engineering
Methods :bE(),bTech()
Class :Medicine
Methods :physiyo(),dental(),mbbs()
Description:
create above 4 class and call all your class methods into the Education using multilevel inheritance.
QUESTION 3:
------------
package name: org.india
Project name: SouthIndia
Class name : India
Methods : india
Class name : TamiladuN
Methods : tamillanguage
Class name : kerala
Methods : malayalam
Class name : AndhraPradesh
Methods : telugu
Description:
create above 4 class and call india methods into all the sub class using Hierarchical inheritance.
Comments
Post a Comment