Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.

Similar presentations


Presentation on theme: "Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand."— Presentation transcript:

1 Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand on the shoulders of giants" Example: a program that extends class Frame from wheels – inherits the functionality of a Frame window with Quit button drawing of shapes – extends this functionality draws specific shapes

2 Inheritance Hierarchy in Java, a subclass has only one direct superclass – every class has one superclass only class Object has none – in other programming languages there can be several superclasses a subclass inherits all functionality – from its direct superclass from the superclass of its direct superclass, etc. – i.e. there is a chain of superclasses – the subclass inherits from all superclasses in the chain – classes form a tree, its root is the class Object a class declared without extends clause extends Object "is-a" relationship – an instance of a subclass also "is-a" an instance of its superclass

3 Modifiers define visibility of declared elements – i.e. where can they be used there are 4 ways: – public, private, protected, and no modifier public visible everywhere private visible only within the class where declared protected visible only within subclasses no modifier visible only within the package where declared

4 Inheritance: Methods and Overriding all methods in a superclass are inherited – except private methods – they can be directly called e.g., since toString() is defined in Object it can be called on any object subclasses can redefine methods declared in a superclass such method is called overidden subclass "knows better" how to "do it" e.g., we can override toString() it's a good idea since subclass knows better how to describe its objects watch out when you want to override a method! – overidden method must have exactly the same signature – otherwise it's a different method – e.g., tostring() doesn't override toString() lower case s – e.g., toString(String indent) doesn't override it, either different parameter list overriding is different from overloading!

5 Overriding and super what if we want to reuse the method from superclass? – e.g., while overriding toString(), we want to reuse toString() from superclass prefix call with super – e.g., public void toString() { return super.toString() + " more info"; } – this can be done in any method of the subclass we can only call the method being immediately overidden – e.g., super.super.toString() doesn't work!

6 Inheritance: Fields all fields in a superclass are inherited – they can be directly accessed if declared as public if declared as protected – private fields are still inherited, but not directly accessible what if subclass declares a field with the same name? – there are two such fields – subclass can access only the field it declares – it cannot access the field from superclass – prefixing with super doesn't work! – the field in is superclass is shadowed programming style: typically a shadowing a field is a mistake – the new field has a different purpose – so it should have different name

7 Subclass Constructor define a subclass using extends and name of the superclass – e.g. class Circle extends Ellipse {...} the constructor of the subclass must call a constructor of the superclass – this call must be the first statement – e.g. public Circle (Point at) { super (at); } we can leave out call to parameterless constructor of the superclass – Java "inserts" it – e.g. public Circle () { super (); //no need }

8 Polymorphism means many forms – a variable can reference objects of many types – a method can have many implementations variable declared using superclass can be assigned an instance of any subclass – even an indirect subclass e.g. variable declared as Object can be assigned any object! variable declared using interface type can be assigned an instance of any class that implements the interface type of the currently referenced object is the actual type when a method is called the implementation in actual type is executed – e.g. the overriden method is used!

9 Object-Oriented Programmig Object-Oriented Programmig (OOP) has 4 main concepts: objects and classes inheritance – subclasses extend functionality of superclasses encapsulation – visibility restrictions on accessing fields and methods polymorphism – variables hold objects of different types – actual types decides how variables behave

10 Abstract Classes a class can be declared as abstract – an abstract class can have abstract methods abstract method doesn't have a body – its declared like a method in an interface but an abstract method can be called! – e.g., abstract class Function { abstract double f (double x); void plot () { double y = f(x); } } abstract methods are implemented in subclasses – e.g., class Sin extends Function { double f (double x) {return Math.sin(x);} }

11 Use of Abstract Classes an abstract class cannot be instantiated – e.g. new Function(); won't work! but we can declare a variable using an abstract type and assign it an instance of a concrete subclass! – good example of polymorphism – e.g., Function f = new Sin(); f.plot(); f = new Cos(); //where Cos extends Function f.plot(); abstract classes are similar to interfaces – use interfaces if possible – use abstract classes if possible you need some methods that are implemented often you combine it to achieve maximum flexibility – an interface to declare behaviors – an abstract class to implement the general behaviors – concrete subclasses to implement specific behaviors


Download ppt "Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand."

Similar presentations


Ads by Google