Example6:-
class Teacher
{
protected
void disp()
{
System.out.println("Parent implemented disp
method");
}
}
class Student extends Teacher
{
protected
void disp()
{
System.out.println("Child implemented disp
method");
}
}
class Test
{
public static void main(String[]args)
{
Student s=new Student();
s.disp();
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Child
implemented disp method
Example7:-
class Teacher
{
void
disp()
{
System.out.println("Parent implemented disp
method");
}
}
class Student extends Teacher
{
public
void disp()
{
System.out.println("Child implemented disp
method");
}
}
class Test
{
public static void main(String[]args)
{
Student s=new Student();
s.disp();
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Child
implemented disp method
Example8:-
class Teacher
{
protected void
disp()
{
System.out.println("Parent implemented disp
method");
}
}
class Student extends Teacher
{
void disp()
{
System.out.println("Child implemented disp
method");
}
}
class Test
{
public static void main(String[]args)
{
Student s=new Student();
s.disp();
}
}
Output:-
C:\JAVATECH>javac Test.java
Test.java:10:
error: disp() in Student cannot override disp() in Teacher
void
disp()
^
attempting to assign weaker access
privileges; was protected
1
error
Example9:-
class Teacher
{
void disp()
{
System.out.println("Parent implemented disp
method");
}
}
class Student extends Teacher
{
void disp()
{
System.out.println("Child implemented disp
method");
}
}
class Test
{
public static void main(String[]args)
{
Teacher
t=new Teacher();
t.disp();
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Parent
implemented disp method
Example10:-
class Teacher
{
void disp()
{
System.out.println("Parent implemented disp
method");
}
}
class Student extends Teacher
{
void disp()
{
System.out.println("Child implemented disp
method");
}
}
class Test
{
public static void main(String[]args)
{
Teacher t=new
Student();
t.disp();
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
Child
implemented disp method
Example11:-
class
Teacher
{
}
class Student extends Teacher
{
void disp()
{
System.out.println("Child implemented disp
method");
}
}
class Test
{
public static void main(String[]args)
{
Teacher t=new Student();
t.disp();
}
}
Output:-
C:\JAVATECH>javac Test.java
Test.java:17:
error: cannot find symbol
t.disp();
^
symbol:
method disp() location: variable t of type Teacher
1
error
Comments
Post a Comment