Java applets-Implementation of abstract methods in interfaces (solving the problem that JAVA cannot inherit more) and java applets

Source: Internet
Author: User

Java applets-Implementation of abstract methods in interfaces (solving the problem that JAVA cannot inherit more) and java applets

public interface Sing {
public static final String eyecolor="black";
public void sleep();
public void sing();
}

public interface Print {
public static final String sex="woman";
public void print();
public void eat();
}

Public class Student implements Sing {
String name;
Public Student (String name ){
This. name = name;
}
Public String getName (){
Return name;
}
Public void sleep (){
System. out. println ("Student is sleeping ");
}
Public void sing (){
System. out. println ("Student is singsong ");
}
Public void all (){
System. out. println ("Student named" + name + "is a singer ");
}
}

Public class Teacher implements Sing, Print {
Int age;
Public Teacher (int age ){
This. age = age;
}
Public int getAge (){
Return age;
}
Public void print (){
System. out. println ("Teacher is print ");
}
Public void eat (){
System. out. println ("Teacher is eating ");
}
Public void sleep (){
System. out. println ("Teacher is sleeping ");
}
Public void sing (){
System. out. println ("Teacher is singsong ");
}
Public void info (){
System. out. println ("this" + age + "year old teacher can sing and draw ");
}
}

Public class Test {
Public static void main (String [] args ){
Student s1 = new Student ("LiQing ");
S1.sleep ();
S1.sing ();
S1.all ();
Sing t1 = new Teacher (25 );
T1.sing ();
T1.sleep ();
Print p1 = (Print) t1; // (forced conversion of object types)
P1.print ();
P1.eat ();
Teacher t2 = (Teacher) t1;
T2.info ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.