Java Object-oriented notation (2)

Source: Internet
Author: User

1. Inheritance


Use an example to illustrate inheritance

Class person{private string name;private int age;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 void tell () {System.out.println ("Name:" + this.getname () + " " + "Age:" +this.getage ());}} Class student extends person{private int score;public int getscore ()  { Return score;} Public void setscore (Int score)  {this.score = score;} Public void tell () {Super.tell ();   //uses the parent method System.out.println ("Score:" +score);}} public class extenddemo {/** *  @param  args */public static void  main (String[] args)  {// todo auto-generated method stubstudent std  = new Student (Std.setage); Std.setname ("Thystar"); Std.setscore (+); Std.tell ();}} 


2 Abstract classes and excuses

The final keyword declares classes, methods, and properties

    • Classes that use Final declaration cannot be inherited

    • Methods that use Final declaration cannot be overridden

    • Use final declared variables to become constants, constants cannot be modified


Abstract class: A class that contains abstract methods

Abstract class classname{

}


Abstract method: A method declared without being implemented, declared with the abstract keyword

Abstract class Quilt class inheritance, to override all methods in an abstract class

Abstract classes cannot be instantiated directly, but are instantiated by their subclasses

Abstract class abs{private int age;public int getage () {return age;} public void Setage (int.) {this.age = age;} public void Tell () {}public abstract void Say ();p ublic abstract void print (); Class Absclass extends Abs{public void Say () {System.out.println (This.getage ());} public void print () {}}public class Absdemo {public static void main (string[] args) {Absclass ab = new Absclass (); Ab.setage ( Ab.say ();}}


Interface:

Interface intername{

}


Interface inter{public static final int age=10;//Common global constant public abstract void tell (); Interface Inter2{public abstract void Say ();} Abstract class Abs1{public abstract void Print ();} Class A extends Abs implements Inter, Inter2{public void Tell () {}public void Say () {System.out.println ("say");} public void print () {System.out.println ("Abs");}} public class InterDemo01 {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stuba A = new A (); A.tell ();    System.out.println (Inter.age); Global constants Call A.say () with the class name; A.print ();}}



Geek College Course: http://www.jikexueyuan.com/path/javaweb/











Java Object-oriented notation (2)

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.