Java abstract classes and interfaces

Source: Internet
Author: User

1. Define PET abstract class

public abstract class pet {private string name;private int      age;private int  health;private string love;public pet () {// System.out.println ("Parent class no parameter construction method");} Public pet (string name,int age,int health,string love) {this.name   =    name;this.age    =    age;this.health =  Health;this.love   =   love;//system.out.println ("Parent class has a constructor method");} Get method, set Method Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;} Public int gethealth ()  {return health;} Public void sethealth (Int health)  {this.health = health;} Public string&nbSp;getlove ()  {return love;} Public void setlove (String love)  {this.love = love;} The parent class's printing method Public void print () {System.out.println (name+ "\ t" +age+ "\ T" +health+ "\ T" +love);} Abstract method: Access modifier +abstract keyword + return value + method name//abstract method must be in abstract class, abstract method without method body (no curly brace) public abstract void shout ();// Common Method/*public void shout () {                  system.out.println ("The Barking of Pets!") ");              }*/}//dog sub-class Public class  dog extends pet {public string strain;//public dog ()  {////// System.out.println ("sub-class non-parametric construction method");//}public dog (String name, int age, int health,  string love,string strain)  {super (name, age, health, love);// System.out.println ("Sub-class has a method of construction");  }//public string getstrain ()  {//return stRain;//}////public void setstrain (String strain)  {//this.strain = strain;//}// Subclass method Overrides//public void print () {////system.out.println (Super.getname () + "\ T" +super.getage () + "\ T" + Super.gethealth () + "\ T" +super.getlove () + "\ T" +strain)//}public void shout () {System.out.println ("name" + This.getname () + "The Puppy barking!" ");}} Pet Test Class Public class petdemo {public static void main (String[] args)  { Dog dog = new dog ("Wang Choi", 2,100, "fraternity", "Teddy"),//dog.setname ("Wang Choi");//dog.strain= "Teddy";// Dog.setage (2);//dog.sethealth (+)//dog.setlove ("Fraternity");//system.out.println (Dog.getname () + "\ T" +dog.strain+ "\ T" +dog.getage () + "\ T" +dog.gethealth () + "\ T" +dog.getlove ());//dog.print ();//penguin penguin = new  penguin ();//pet pet1 = new dog ();////pet pet2 = new penguin ();// Pet1.shout ();////pet2.shout ();////petshop petshop = new petshop ();////Pet pet3 = &nbSp;petshop. Buypet ();////pet3.shout ();////if (Pet3 instanceof dog) {////dog dog1 =  (DOG) pet3;// System.out.println (Dog1.getstrain ());////}else{////penguin penguin1 =  (Penguin) pet3;// System.out.println (Penguin1.getsex ());//}//pet dog = new dog ("Wang Choi", 12,100, "Fraternity", "Teddy"); Dog.shout ();}

Result validation:

A puppy named Wong Choy Barking!


2. //Define the interface of the power outlet and have power supply capability;

Public interface Sockets {public void Getpower ();}

// definition class: TV class realizes power socket interface;

public class TV implements Socket{public void Getpower () {System.out.println ("TV plugged in to a power outlet starts to play TV. ");}}

define the test class, including the method that the appliance starts to work, the parameter is the electrical object;

public class Socketdemo {public static void main (string[] args) {Socket TV = new TV (); Tv.getpower ();}}

Test results

The TV is plugged into a power outlet to start playing TV.


3.


//

Public abstract class Person_student {public string name;public person_student (String name) {super (); this.name = name;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}


Public interface Work {public void getwork ();}


Public interface Study {public void Getstudy ();}


public class Graduate_student extends Person_student implements Work,study {public graduate_student (String name) {Super ( name);} public void Getwork () {System.out.println (This.getname () + "graduate students have the ability to work. ");} public void Getstudy () {System.out.println (This.getname () + "graduate students have the ability to learn. ");}}


public class Graduatedemo {public static void main (string[] args) {graduate_student stu = new Graduate_student ("Zhang San"); stu.g    Etstudy (); Stu.getwork (); }}


This article is from the "Advanced Technology" blog, so be sure to keep this source http://vipnoon.blog.51cto.com/7589908/1795087

Java abstract classes and interfaces

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.