Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance CT1513.

Similar presentations


Presentation on theme: "Inheritance CT1513."— Presentation transcript:

1 Inheritance CT1513

2 Modifiers in inheritance
Private(-) data members and methods for a super class cant be accessible to the subclass Protected (#) data members and methods for a super class accessible to the instances of the subclasses . Public (+) data members and methods are accessible to everyone.

3 Using super methods In subclass you can override (or redefine) a method That is already existed in super class. If there is no override you can call a public method of a superclass by its name and appropriate parameter list. If the sub override the super method you should call it by using the reserved key word super.methodsName()

4 Inheritance and Constructors
public class SubClass extends SuperClass { public SubClass(………….) super (………); } **the call of the super constructor must be the first statement. Note : if you do not include statement super(). The default constructor of super will be called if any or there will be an error

5 Not valid No call | super();

6 valid

7 valid

8 valid

9 public class rectangle { private double length; private double width; //defoult constructor public rectangle() {length=width=0; } public rectangle( double w , double l){ setdimension(w,l); } public void setdimension( double w, double l){width=w; length=l; } public double getw(){return width;} public double getl(){return length;} public double area(){ return width*length;} public void print() {System.out.println("length =" + length+"width = " + width );} }

10 Create class called box inherits from rectangle having the following:
Private height ,, ge_height() Default constructor and copy constructor to set Setdimension(); Area();//2*L*W + L * H +W* H Print();

11

12 Polymorphism CT1513

13 Introduction to polymorphism

14 Static Binding Vs. Dynamic Binding
Binding means associating a method definition with its invocation It can be also called early binding

15

16

17 Note ! A A refrence variable from a class A A ref; Can point to an object of class B as well as an object of class C ref = new A(); ref = new B(); ref = new C(); B C

18 100 200 300 1000

19 In same rectangle – box example

20


Download ppt "Inheritance CT1513."

Similar presentations


Ads by Google