InheritanceTest1
class Parent {
int a;
Parent(){
System.out.printIn("부모 생성");
}
}
class Child extends Parent{
int b;
Child() {
System.out.printIn("자식 생성");
}
public void print() {
System.out.printIn(this.a);
System.out.printIn(this.b;
System.out.printIn(super.a);
}
}
public class inheritanceTest1 {
public static void main(String[] args){
Child obj = new Child();
}
}
InheritanceTest2
class Parent2 {
int a = 10;
public void print(){
System.out.printIn(a);
}
}
class Child2 extends Parent2 {
int b =20;
public void print(){
System.out.printIn(a+b);
}
}
public class IngeritanceTest2 {
public static void main(String[] args){
Child2 obj = new Child2();
obj.print();
}
}
InheritanceTest3
class 도형 {
public void paint() {
}
}
class 삼각형 extends 도형 {
public void paint(){
System.out.printIn("삼각형 출력");
}
}
class 사각형 extends 도형 {
public void paint(){
System.out.printIn("사각형을 그려요");
}
}
class 원 extends 도형 {
public void paint(){
System.out.printIn("원 그리기");
}
}
public class InheritanceTest3{
}
InheritanceTest4
class 부모 {
int a;
}
class 자식 extends 부모{
int b;
}
public class InheritanceTest4 {
public static void main(String[] args){
부모 ob1 = new 부모();
부모 ob2 = new 자식();
자식 ob4 = new 자식();
System.out.printIn(ob2.a);
System.out.printIn(ob2.b;
System.out.printIn(((자식)ob2).b);
}
}
InheritanceTest5
class 고양이 {
public void meow(){
System.out.printIn("야옹~~~~~");
}
}
class 개{
public void bowwow(){
System.out.printIn("멍멍");
}
}
class 동물병원 {
public void 예방접종(개 dog){
dog.bowwow();
}
public void 예방접종(고양이 cat){
cat.meow();
}
}
public class InheritanceTest5 {
public static void main(String[] args){
}
}
InheritanceTest6
class Livestock{
public void cry(){
}
}
class Cat extends Livestock{
public void cry(){
System.out.printIn("야옹~~~~~");
}
}
class Dog extends Livestock{
public void cry(){
System.out.printIn("멍멍");
}
}
class Hospital{
public void injection(Livestock animal){
animal.cry();
}
}
public class InheritanceTest6 {
public static void main(String[] args){
Dog dog = new Dog();
Hospital h = new Hospital();
h.injection(dog);
Livestock animal = new Dog();
h.injection(animal);
}
}
InheritanceTest7
import java.util.*;
class 정규직사원{
public void 월급수령(){
System.out.printIn("호봉과 보너스에 따라 급여가 지급됩니다.");
}
}
class 계약직사원{
public void salary() {
System.out.printIn("정해진 급여가 지급됩니다.");
}
}
class 파트타이머{
public void pay() {
System.out.printIn("근무시간*시급을 지급합니다.");
}
}
class 부서 {
ArrayList list = new ArrayList();
public void init(){
list.add(new 정규직사원());
list.add(new 계약직사원());
list.add(new 파트타이머());
}
public void 급여지급(){
for(Object obj:list){
if(obj instanceof 정규직사원) ((정규직사원)obj).월급수령();
else if(obj instanceof 계약직사원) ((계약직사원)obj).salary();
else if(obj instanceof 파트타이머) ((파트타이머)obj).pay();
}
}
}
public class InheritanceTest7 {
public static void main(String[] args){
}
}
InheritanceTest8
import java.util.*;
abstract class Employee{
public abstract void pay();
}
class FulltimeWoker extends Employee {
@Override
public void pay(){
System.out.printIn("호봉과 보너스에 따라 급여가 지급됩니다.");
}
}
class ContractWoker extends Employee {
public void pay(){
System.out.printIn("호봉과 보너스에 따라 급여가 지급됩니다.");
}
}
class ParTimer extends Employee {
public void pay(){
System.out.printIn("호봉과 보너스에 따라 급여가 지급됩니다.");
}
}
class Dept {
ArrayList<Employee> list = new ArrayList<>();
public void init(){
list.add(new FulltimeWorker());
list.add(new ContractWorker());
list.add(new PartTimer());
}
public void 급여지급() {
for(Employee e:list){
e.pay();
}
}
}
public class InheritanceTest8 {
public static void main(String[] args(){
Dept d = new Dept();
d.init();
d.급여지급();
}
}