Inheritance: It is one of the object oriented features of java where properties and behaviours of one class is used in other class. child class can use data and methods of parent class
e.g.:
Example:
Address.java
Employee.java
Developer.java
Output:
Tom has 4.5 of experience
Id: 111, Name: Tom, Salary: 20000.0, Mail Id: tom@gmail.com, Address: 16th Cross Main Road, BTM 1st Stage Bengaluru-560076 Karnataka India
4.5 Java
Miichal has 5.5 of experience
Id: 222, Name: Miichal, Salary: 30000.0, Mail Id: michal@gmail.com, Address: Mg Road Mumbai-400001 Maharashtraya India
5.5 .Net
e.g.:
- Create a class Employee with related properties and behaviours.
- Create classes Developer, Engineer, Manager, Lead and Architect that extends(inherit) Employee class
- Developer, Engineer, Manager, Lead and Architect all are Employee means Developer is a Employee. This type of relation is called IS-A relationship and Employee is parent class and Developer is child class
- Code usability
- Method Overriding
- Easy code maintenance
Syntax:
class Child_Class extends Parent_Class {
//Data members and Behaviours
}
Types of Inheritance:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance (Only for Interface)
- Hybrid Inheritance
Example:
Address.java
package com.java.inheritance;
public class Address {
String street;
String city;
String state;
int pincode;
String country;
public String toString() {
return street+" "+city+"-"+pincode+" "+state+" "+country;
}
public Address() {
street = "16th Cross Main Road, BTM 1st Stage";
city = "Bengaluru";
state = "Karnataka";
pincode = 560076;
country = "India";
}
public Address(String street, String city, String state, int pincode, String country) {
this.street = street;
this.city = city;
this.state = state;
this.pincode = pincode;
this.country = country;
}
}
public class Address {
String street;
String city;
String state;
int pincode;
String country;
public String toString() {
return street+" "+city+"-"+pincode+" "+state+" "+country;
}
public Address() {
street = "16th Cross Main Road, BTM 1st Stage";
city = "Bengaluru";
state = "Karnataka";
pincode = 560076;
country = "India";
}
public Address(String street, String city, String state, int pincode, String country) {
this.street = street;
this.city = city;
this.state = state;
this.pincode = pincode;
this.country = country;
}
}
Employee.java
package com.java.inheritance;
public class Employee {
int id;
String name;
double salary;
String email;
Address address;
public Employee() {
id = 111;
name = "Tom";
salary = 20000.0;
email = name.toLowerCase()+"@gmail.com";
address = new Address();
}
public Employee(int id, String name, double salary, String email, Address address) {
this.id = id;
this.name = name;
this.salary = salary;
this.email = email;
this.address = address;
}
void display() {
System.out.println("Id: "+id +", Name: "+name+", Salary: "+salary+", Mail Id: "+email+", Address: " + address);
}
}
public class Employee {
int id;
String name;
double salary;
String email;
Address address;
public Employee() {
id = 111;
name = "Tom";
salary = 20000.0;
email = name.toLowerCase()+"@gmail.com";
address = new Address();
}
public Employee(int id, String name, double salary, String email, Address address) {
this.id = id;
this.name = name;
this.salary = salary;
this.email = email;
this.address = address;
}
void display() {
System.out.println("Id: "+id +", Name: "+name+", Salary: "+salary+", Mail Id: "+email+", Address: " + address);
}
}
Developer.java
package com.java.inheritance;
public class Developer extends Employee {
double experience;
String language;
public Developer() {
super();
experience = 4.5;
language = "Java";
}
public Developer(Employee e, double exp, String lang){
super(e.id, e.name, e.salary, e.email, e.address);
experience = exp;
language = lang;
}
public void show(){
System.out.println(experience+" "+language);
}
public static void main(String[] args) {
Developer dev = new Developer();
System.out.println(dev.name + " has " + dev.experience + " of experience");
dev.display();
dev.show();
Address add = new Address("Mg Road", "Mumbai", "Maharashtraya", 400001, "India");
Employee emp = new Employee(222, "Miichal", 30000, "michal@gmail.com", add);
Developer dev2 = new Developer(emp, 5.5, ".Net");
System.out.println(dev2.name + " has " + dev2.experience + " of experience");
dev2.display();
dev2.show();
}
}
public class Developer extends Employee {
double experience;
String language;
public Developer() {
super();
experience = 4.5;
language = "Java";
}
public Developer(Employee e, double exp, String lang){
super(e.id, e.name, e.salary, e.email, e.address);
experience = exp;
language = lang;
}
public void show(){
System.out.println(experience+" "+language);
}
public static void main(String[] args) {
Developer dev = new Developer();
System.out.println(dev.name + " has " + dev.experience + " of experience");
dev.display();
dev.show();
Address add = new Address("Mg Road", "Mumbai", "Maharashtraya", 400001, "India");
Employee emp = new Employee(222, "Miichal", 30000, "michal@gmail.com", add);
Developer dev2 = new Developer(emp, 5.5, ".Net");
System.out.println(dev2.name + " has " + dev2.experience + " of experience");
dev2.display();
dev2.show();
}
}
Output:
Tom has 4.5 of experience
Id: 111, Name: Tom, Salary: 20000.0, Mail Id: tom@gmail.com, Address: 16th Cross Main Road, BTM 1st Stage Bengaluru-560076 Karnataka India
4.5 Java
Miichal has 5.5 of experience
Id: 222, Name: Miichal, Salary: 30000.0, Mail Id: michal@gmail.com, Address: Mg Road Mumbai-400001 Maharashtraya India
5.5 .Net
No comments:
Post a Comment