Day 3: Java Notes
Day 3: Java Notes
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.
Heap memory:
Object we stored in heap memory
RAM->JVM-> Heap memory.
Type of inheritance:
Single inheritance
Multilevel inheritance
Multiple inheritance
Hybrid inheritance
Hierarchical inheritance
Types of inheritance:
======================
Single inheritance:
----------------------
Combinations of one parent class and one child class.
Multilevel inheritance:
--------------------------
Child class is accessing more than one parent class in a tree level structure
Hierarchical inheritance
-------------------------------
Combination of one parent and more than one child
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 Multiple and Hierarchical inheritance
Java does not support Multiple Inheritance:
- Syntax
- Priority Problem
Assignment:
QUESTION 1:
-----------
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.
single 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");
}
}
-------------------------------------------------------------------------------------------
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:
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 :
Program:
package org.multi;
public interface RBIBank {
void saving();
void fixed();
}
—-------------------------------------------------------
package org.multi;
public interface WorldBank {
void loan();
void deposit();
}
—---------------------------------
package org.multi;
public class HDFC implements RBIBank, WorldBank {
@Override
public void saving() {
// TODO Auto-generated method stub
System.out.println("saving");
}
@Override
public void deposit() {
// TODO Auto-generated method stub
System.out.println("deposit");
}
@Override
public void fixed() {
// TODO Auto-generated method stub
System.out.println("fixed");
}
@Override
public void loan() {
// TODO Auto-generated method stub
System.out.println("loan");
}
public static void main(String[] args) {
HDFC h= new HDFC();
h.saving();
h.loan();
}
}
—-----------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment