xxxxxxxxxx
class BaseClass {
private String name = "base";
public BaseClass(){
// your base class logic
}
public String getName() {
return name;
}
}
class DerivedClassA extends BaseClass {
private String name = "derived1";
public DerivedClassB(){
super(); // always call the base class constructor if that has any parameter
// your derived class logic
}
public String getName() {
return name;
}
}
class DerivedClassB extends BaseClass {
private String name = "derived2";
public DerivedClassA(){
super(); // always call the base class constructor if that has any parameter
// your derived class logic
}
public String getName() {
return name;
}
}
public class Example {
public static void main(String[] args) {
var a = new DerivedClassA();
var b = new DerivedClassB();
System.out.println(a.getName());
System.out.println(b.getName());
}
}
xxxxxxxxxx
Inheritance in Java is a mechanism in which one object acquires
all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).
xxxxxxxxxx
public class Sample {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
Duck duck = new Duck();
System.out.println(dog.getSound());
System.out.println(cat.getSound());
System.out.println(duck.getSound());
}
}
class Animal {
private String sound = "anything";
public void setSound(String sound) {
this.sound = sound;
}
public String getSound() {
return sound;
}
}
class Dog extends Animal {
private String sound = "Aw aw";
public String getSound() {
return sound;
}
}
class Cat extends Animal {
private String sound = "Meow meow";
public String getSound() {
return sound;
}
}
class Duck extends Animal {
private String sound = "Quack Quack";
public String getSound() {
return sound;
}
}
xxxxxxxxxx
Java Inheritance (Subclass and Superclass)
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:
subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
xxxxxxxxxx
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
// Create a myCar object
Car myCar = new Car();
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
xxxxxxxxxx
class Animal {
public void sound() {
System.out.println("Animal is making a sound");
}
}
class Dog extends Animal {
public void sound() {
System.out.println("Dog is barking");
}
}
class Cat extends Animal {
public void sound() {
System.out.println("Cat is meowing");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
animal.sound(); // Output: "Animal is making a sound"
Dog dog = new Dog();
dog.sound(); // Output: "Dog is barking"
Cat cat = new Cat();
cat.sound(); // Output: "Cat is meowing"
}
}
xxxxxxxxxx
// Parent class
class Animal {
public void sound() {
System.out.println("The animal makes a sound");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
public void sound() {
System.out.println("The dog barks");
}
}
// Child class inheriting from Animal
class Cat extends Animal {
public void sound() {
System.out.println("The cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
animal.sound();
Dog dog = new Dog();
dog.sound();
Cat cat = new Cat();
cat.sound();
}
}
xxxxxxxxxx
public class Parent
{
//methods and constructors
}
public class Child extends Parent
{
//inherits methods from Parent class
}
xxxxxxxxxx
Inheritance is the mechanism where the object of one class acquires the property of the object of another class.
xxxxxxxxxx
Super class:
public class Multi(){
}
Sub class:
public class Multiplication extends Maltil(){
}