Class Inheritance and Method Overridingclass Animals {When a method has the sameint Age;Animals (int Age) ( this.Age = Age; name as in the superclass, themethod overrides the oneinvoid wish OSystem.out.println ("I want to eat"),superclass. Otherwise thesuperclass methods areclass Rabbit extends Animals inherited with no changeRabbit (int Age, char Color) (The top class in Java issuper (Age);//call superconstructorthis.Color = Color,“object"".Every class is a1descendentofobjectvoid wish 0 (// method over-ride(implicitly inherited)I/ call a super methodsuper.wishO,System.out.println("I want a carrot"),Keyword superreferstothesuperclass'methods orRabbitmyrabbit=newRabbit(3,B'),variables.I/ ? output ?my_rabbit.wishO
Class Inheritance and Method Overriding • When a method has the same name as in the superclass, the method overrides the one in superclass. Otherwise the superclass methods are inherited with no change. • The top class in Java is “object”. Every class is a descendent of object (implicitly inherited). • Keyword super refers to the superclass’ methods or variables. class Animals { int Age; Animals (int Age) { this.Age = Age; } void wish () { System.out.println ("I want to eat"); } } class Rabbit extends Animals { . . . Rabbit (int Age, char Color) { super (Age); // call super constructor this.Color = Color; } void wish () { // method over-ride super.wish(); // call a super method System.out.println("I want a carrot"); } } Rabbit my_rabbit = new Rabbit(3, ‘B’); my_rabbit.wish(); // ? output ?
Modifier: staticA variable or methoddefined as class Rabbit ?staticmeansthatthevariablestatic int rabbits count= O;or the method belongs to theint Age;class, and they are not withRabbit (int Age) {objects.rabbits count++;llaccessstaticvarStatic variables / methods arethis.Age = Age;classvariables/methods.There人is only one copy of them in thestatic int count() (ll only use class variablesystem.returnrabbits count:Class methods can onlyaccess1class variables. It cannot use"this""reference,cannotcallObject Templatenormal methods, etc. Butstatic vars,normal methods can accessclass variables.objectobject
Modifier: static • A variable or method defined as staticmeans that the variable or the method belongs to the class, and they are not with objects. • Static variables / methods are class variables / methods. There is only one copy of them in the system. • Class methods can only access class variables. It cannot use “this” reference, cannot call normal methods, etc. But normal methods can access class variables. class Rabbit { static int rabbits_count = 0; int Age; Rabbit (int Age) { rabbits_count++; // access static var this.Age = Age; } static int count() {// only use class variable return rabbits_count; } } object object Object Template static vars,
Modifiers: public, private, protected, finalpublic: can be used everywhereprivate: can be used only in its own class, not even from its subclass.protected: can be accessed within its own packagefinaldefines:-aclass can'tbeextendedamethodcan'tbeoverriddenavariablecannotbemodified (constant)final class Rabbit extends Animal final float 元 = 3.14;final int run(int duration, boolean zigzag) ( .... } // method 11
Modifiers: public, private, protected, final • public: can be used everywhere. • private: can be used only in its own class, not even from its subclass. • protected: can be accessed within its own package. • final defines: − a class can’t be extended − a method can’t be overridden − a variable cannot be modified (constant) final class Rabbit extends Animal { final float = 3.14; . final int run(int duration, boolean zigzag) { . } // method 1 }
Abstract Methods and Abstract ClassesAnabstractmethodisamethodthathasnoimplementation.Itallowssubclassestoimplementthemethod accordingtotheirownneeds.An abstractclass is a class that containsat least one abstractmethodAn abstract class must be FULLY implemented before it can beinstantiated.abstractclass Animals int Age;Animals (int Age) { this.Age = Age; )abstractvoidwish O;class Rabbit extends Animals void wish O :// implementtheabstractmethodSystem.out.println("I want a carrot"); }
Abstract Methods and Abstract Classes • An abstractmethod is a method that has no implementation. It allows subclasses to implement the method according to their own needs. • An abstract class is a class that contains at least one abstract method. • An abstract class must be FULLY implemented before it can be instantiated. abstract class Animals { int Age; Animals (int Age) { this.Age = Age; } abstract void wish (); } class Rabbit extends Animals { . . . void wish () { // implement the abstract method System.out.println("I want a carrot"); } }
Interface and its ImplementationAn interface is a class that all its methods are abstractThere are many interfaces in Java, especially for event handlers, thatrequire implementations.When defining a class which implements an interface, uses theformat:class xxx implements interfacemethod declaration;method declaration;1If a class implements some methodsof an interface, not all of them,thenthisclassbecomesanabstractclass
Interface and its Implementation • An interface is a class that all its methods are abstract. • There are many interfaces in Java, especially for event handlers, that require implementations. • When defining a class which implements an interface, uses the format: class xxx implements interface { method declaration; . method declaration; } • If a class implements some methods of an interface, not all of them, then this class becomes an abstract class