2013-12-23 3 views
-7
  1. Person 클래스
    {
    개인 문자열 이름;
    공공
    문자열 toString()
    {
    반환 this.name;
    }
    }파생 클래스에서 수퍼 클래스의 public 메서드를 오버라이드 할 수 있습니까?

    class Student extends Person 
    { 
        private int no_of_courses; 
        public 
         @Override String toString() { return "Student " + super.toString(); } 
    } 
    class SuperDemo 
    { 
        public static void main(String[] args) 
        { 
         Person p = new Student("ABC,"XYZ"); 
         System.out.println(p.toString()); 
        } 
    } 
    
    Can we @Override a public method of super class in the derived class? 
    Person class has Constructor Person(String name); 
    Student class has constructor Student(String name) { super(name); } 
    
+0

질문을 게시하기 전에 시도해보십시오. –

+0

질문을 편집하려면 노력을 기울이십시오. 여전히 끔찍한 모양입니다. –

+0

나는 약한 액세스 권한에 액세스하려고 시도하는 오류 코드를 추가하려고합니다. public이었다 – user3128633

답변

2

예.toString()Object에서 무시할 때 이미 자연 스럽 게 수행합니다.

당신은 무시할 수 없습니다 :

  • 아무것도 표시 final
  • 아무것도 static
  • 당신은 또한 재정의 된 메서드의 가시성을 줄일 수 없습니다 private

아무것도; 즉 public 메소드를 protected으로 재정의 할 수 없습니다.

+0

고맙습니다. 좋은 도움 – user3128633

관련 문제