xxxxxxxxxx
// Base Class Vehicle
class Vehicle {
// Private Fields
private String vehicle;
private int year;
private String model;
// Parameterized Constructor
public Vehicle(String vehicle, int year, String model) {
this.vehicle = vehicle;
this.year = year;
this.model = model;
}
// public method to print details
public void printDetails() {
System.out.println("Vehicle Type: " + vehicle);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
}
}
// Derived Class Car
class Car extends Vehicle {
// Private field
// Parameterized Constructor
public Car(String vehicle, int year, String model) {
super(vehicle, year, model); // calling parent class constructor
}
public void carDetails() { // details of car
printDetails(); // calling method from parent class
}
}
class Sample {
public static void main(String[] args) {
Car elantraSedan = new Car("Car", 2019, "Elantra"); // creation of car Object
elantraSedan.carDetails(); // calling method to print details
}
}
xxxxxxxxxx
- "is a" relationship
- All class extends `Object` class
- extended or derived class has same logic as base class (`public` and `protected`)
- Allows code re-use
- `protected` = access modifier that acts like public in a package
- `private` members are not inherited by subclasses / derived class
xxxxxxxxxx
class left {
public:
void foo();
};
class right {
public:
void foo();
};
class bottom : public left, public right {
public:
void foo()
{
//base::foo();// ambiguous
left::foo();
right::foo();
// and when foo() is not called for 'this':
bottom b;
b.left::foo(); // calls b.foo() from 'left'
b.right::foo(); // call b.foo() from 'right'
}
};
Inheritance provides a way to create a new class from an existing class. The new class is a specialized version of the existing class such that it inherits all the non-private fields (variables) and methods of the existing class. The existing class is used as a starting point or as a base to create the new class.
The IS A Relationship
After reading the above definition, the next question that comes to mind is this: when do we use inheritance? Wherever we come across an IS A relationship between objects, we can use inheritance.
xxxxxxxxxx
class Base {
public Base() {
System.out.println("Base");
}
}
class Derived extends Base {
public Derived() {
System.out.println("Derived");
}
}
class DeriDerived extends Derived {
public DeriDerived() {
System.out.println("DeriDerived");
}
}
public class Test {
public static void main(String[] args) {
Derived b = new DeriDerived();
}
}
xxxxxxxxxx
// C++ Implementation to show that a derived class
// doesn’t inherit access to private data members.
// However, it does inherit a full parent object.
class A{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A{ // 'private' is default for classes
// x is private
// y is private
// z is not accessible from D
};
xxxxxxxxxx
class Parent {
// methods
// fields
// ……
}
class Child extends Parent {
// already supports the methods and fields in Parent class
// additional features
}
xxxxxxxxxx
public class Main {
public static void main(String[] args) {
Door d1 = new BankVaultDoor();
Door d2 = new HouseFrontDoor();
Door d3 = new CarDoor();
}
if (arg[0] == "car") {
d3.open();
} else if (arg[0] == "bank") {
d1.open();
} else {
d2.open();
}
}
Inheritance provides an object with the ability to acquire the fields and methods of another class, called base class. Inheritance provides reusability of code and can be used to add additional features to an existing class, without modifying it.
Sample class Mammal is shown below which has a constructor.Mammal Class
xxxxxxxxxx
public class Mammal{
public Mammal()
{
System.out.println("Mammal created");
}
}
Man class extends Mammal which has a default constructor. The sample code is shown below.Man class
public class Man extends Mammal{
public Man()
{
System.out.println("Man is created");
}
}
Inheritance is tested by creating an instance of Man using default constructor. The sample code is shown to demonstrate the inheritance.TestInheritance Class
public class TestInheritance{
public static void main(String args[])
{
Man man = new Man();
}
}
xxxxxxxxxx
@Inheritance(strategy = InheritanceType.JOINED)
public class Personne {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)