public virtual with sharing class Animal {
String name {get; private set;}
Integer weight {get; private set;}
Integer age {get; private set;}
public Animal(String name, Integer weight, Integer age){
this.name = name;
this.weight = weight;
this.age = age;
}
public void makeSound(){
System.debug(this.name + ' makes a sound. ');
}
public virtual void eat(){
System.debug('The animal is eating');
}
public virtual void move(){
System.debug('The animal is moving');
}
}
public with sharing class Dog extends Animal{
String breed {get; private set;}
String color {get; private set;}
public Dog(String name, Integer weight, Integer age, String breed, String color){
super(name, weight, age);
this.breed = breed;
this.color = color;
}
public override void eat(){
System.debug('The dog is eating');
}
public override void move(){
super.move();
System.debug('to play fetch');
}
Animal fish = new Animal('goldfish', 1, 5);
fish.move();
Animal dog = new Dog('Nacho', 5,4, 'Origen', 'Tan');
dog.eat();
dog.move();
dog.makeSound();
Q1. Java에서는 abstract나 virtual 키워드가 없어도 extends가 되는걸로 보이는데,, "Java에서 instance method는 final로 선언되지 않은 이상 모두 virtual이다." 이런 이유 때문일까?
--> yes,, java virtual 관련 참고( https://www.javatpoint.com/virtual-function-in-java)
Q2. virtual class가 아니어도 virtual method 저장이 가능.
virtual class가 아니면 상속할 수 없는데 virtual class 없이 virtual method만 있는 경우의 활용법은?