Want to remove duplication of code? Inheritance is for you. Java inheritance features give this opportunity to reduce the redundancy of code.
What is Java Inheritance?
We will know later, before that-By implementing inheritance technique, you can optimize your code in a professional manner. That’s great!
#Lets see the following diagram:
Here, you can see that many states and behaviors (common logic) are the same in Cow and Cat class. It's called repetition of code and this program is not optimized. When implementation class diagram into code, are you copy & past ? you can! But that’s not a good idea to do that.
So, how can we remove duplication of code? Answer is- use Inheritance mechanism in your code.
Let’s start…
It's one kind of play between parents[Here, Animal class is called parent class] and childs[Here,Cat and Cow classes are called child class]! Child can access all the state and behaviours from the parent class. But parents can’t do that.
So, Java interface is-
Inheritance is a mechanism in which one object[child classes] acquires all the states and behaviors of a parent object.
Inheritance uses parent-child relationship (IS-A relationship).
So what exactly is inherited from parent classes?
Visibility or access modifiers impact what gets inherited from one class to another. In Java, we can make instance variable private and instance methods public.
In this case, we can safely say that the following are inherited:
Public instance variable- its not good practice to make instance variable public
public instance methods.
private instance variables (private instance variables can be accessed only through public getter and setter methods)- it’s good practice to make instance variable private.
#Extends and implements keywords are used as inheritance technique in java.
Note. Parents can an interface.
#Advantages of inheritance
Reuse code facilities: child class inherits all instance members of parent class.
More flexible to change code: changing code in place is enough.
To use polymorphism: method overriding requires IS-A relationship.
Now we know about inheritance.
Let’s code,,,,
Animal class
package com.javaInheritance;
public class Animal {
private String name;
private String hairColor;
private int age;
public String eating(){
return "Eating now!!!";
}
public String playing(){
return "Playing now!!!";
}
public boolean isSleep(){
return false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHairColor() {
return hairColor;
}
public void setHairColor(String hairColor) {
this.hairColor = hairColor;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", hairColor='" + hairColor + '\'' +
", age=" + age +
'}';
}
}
Cat class
package com.javaInheritance;
public class Cat extends Animal{
private String favFood; // favourite food
public String getFavFood() {
return favFood;
}
public void setFavFood(String favFood) {
this.favFood = favFood;
}
public Animal catInfo(Animal cat){
return cat; // it uses toString method to print cat info
}
public void catAllInfo(){
System.out.println("Name: "+this.getName());
System.out.println("Hair Color: "+this.getHairColor());
System.out.println("Age: "+this.getAge());
System.out.println("Favourite Food: "+this.getFavFood());
}
}
Cow class
package com.javaInheritance;
public class Cow extends Animal {
private String favFood;
private double weight;
public String getFavFood() {
return favFood;
}
public void setFavFood(String favFood) {
this.favFood = favFood;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Animal cowInfo(Animal cow){
return cow; // it uses toString method to print cow info
}
// print cow's all data
public void cowAllInfo(){
System.out.println("Name: "+this.getName());
System.out.println("Hair Color: "+this.getHairColor());
System.out.println("Age: "+this.getAge());
System.out.println("Weight: "+this.getWeight());
System.out.println("Favourite Food: "+this.getFavFood());
}
}
testInheritance class
package com.javaInheritance;
public class testInheritance {
public static void main(String[] args){
/*
Cat object creation of Cat class
*/
// a cat object is created as cat
// its not mandatory, you can use any relevant name
Cat cat = new Cat();
// set cat info to the cat object
cat.setName("Pupue");
cat.setHairColor("Brown");
cat.setAge(3);
cat.setFavFood("Fish");
// print the cat info without favFood.
System.out.println(cat.catInfo(cat));
// because favFood is not instance variable of Animal class.
// to print cat all data
cat.catAllInfo();
System.out.println("--------------------------");
/*
--------------------------------------
*/
/*
Cow object creation of Cow class
*/
// create object
Cow cow = new Cow();
// set the values
cow.setName("Humbba");
cow.setHairColor("Ash");
cow.setAge(5);
cow.setWeight(450.5);
cow.setFavFood("Grass");
// print cow's info
System.out.println(cow.cowInfo(cow)); //passes cow object
// print all data
cow.cowAllInfo();
}
}
You can call methods of Cat in testInheritance class by
cat.playing()
Output:
Post a Comment
Leave a comment first....