0
Can someone tell me about inheritance in brief?
c++
2 Answers
0
I can't do it in C++, but I can try to do it in such a way in Java that you'll be able to get it:
In OOP when a class inherits from another it gains the methods and properties of the base class [somewhat dependent on some advanced things I'm not going to get into] and can add it's own properties/methods or even override methods of the base class. Something like this:
public class Human {
public String gender;
public String name;
public Human( gender, name ) {
gender = gender;
name = name;
}
public String introduce() {
return "Hi, I'm " + name + ", and I am a" + gender + ".";
}
}
Great, so now we have a human that can introduce their self by name and creepily mention their should-be-obvious gender. What if we want someone a bit cooler though?
public class TheDude extends Human {
public int cool;
public TheDude() {
super( "male", "Lebowski" ); // Call the constructor of the class we're inheriting from to instantiate the base object.
}
public String introduce() { // Override the introduce method, and be less (and at the same time more) weird.
return "Hi, I'm The Dude, man.";
}
}
}
0
It's like you get property from your parents.
Same thing here Derived or child class get all the properties of Base or parent class.