120 likes | 281 Views
Inheritance . Inheritance is the process of extending the functionality of a class by defining a new class that inherit ,all the features of extending class as well as has some features its own. Syntax of inheritance of a class : Class class Name extends base class name {
E N D
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit ,all the features of extending class as well as has some features its own. Syntax of inheritance of a class : Class class Name extends base class name { Additional methods Or Additional data members } Note: Inheritance provide code reusability and runtime polymorphism.
Note: constructor of a class is never inherited ,in its subclass. • Use of super keywords in Inheritance • it is used to perform following function:- • 1) To invoke super class constructor from a subclass constructor . Syntax: super(args if any); Note: super must be the first statement in a subclass constructor. 2) To invoke a super class method from a subclass method. Syntax: super.method(args if any); 3) To identify super class data members in case of name conflict between super and subclass data members.
If a subclass defines a method of the same signature as a method of its base class then subclass is said to be overriding then method of its super class method overriding and one of the means to implements polymorphism. methods are overridden to extends their functionality. • (overriding is the powerful features of inheritance.) it reflect the runtime polymorphism.
Final Keyword • It is used to perform following functions • 1) to define naming constant • as in C # define A 10; • Final type identifier =expression; • Or • Final type identifier ; • Identifier=expression; • 2) to prevent the overriding of a method. • Syntax: final return type method name(type args) • { • } • 3) To prevent the inheritance from a class.
This keyword • It is used to represents current object in the current method . • Usage of this keyword • 1) to identify data members of the object in case of a name conflict between object data members and local variable of the method. • Syntax: this. member. • 2) to pass current object in a method as arguments from the current method. • 3)To invoke another constructor of the same class on the current object from a constructor i.e. to chain constructor of the class. • Syntax: this(Args if any) • Note: if this is used to invoke a constructor of the current class on current current object , then it must be the first statement in the class.
Reference variable of a base class can contains the reference of its subclass object. • If a method call is made using the reference variable of base class and method is overridden in subclasses then method call is dynamically resolve on the basis of the type of object being reference resulting in runtime polymorphism r.p.m. is a facility to invoke different method from a single method call at different time.
Note: when reference variable of a base class is used to refer sub class object then only those member of subclasses can be referred by the super class reference variable which are part of its own class.
Abstract keyword • It is used to define abstract class & method . • An abstract method is a method without implementation it represents , what is to be done ,without specifying ,how it would be done . • If a class contains an abstract method then class is being abstract. • An abstract class is a class that has the following characteristic • 1) it can’t be instantiated . • 2) an abstract class imposes the responsibility of providing implementation of all its abstract method on its subclasses if any of its subclasses fails to provide implementation over a single method of its super class then sub class is also made abstract.
Interface • An interface is a collection of implicit abstract method and static final data members. Concept of interface in java is used to abstract the interface of a class from its implementation. • Syntax of defining an interface: • Interface identifier • { • Method declarations; • Static final data members ; • } • Note: interfaces are implemented in classes.
Syntax of implementing an interface • Class classname implements interface • { • Public definition of all the methods of interface • Additional data members of the class. • } • Note : if a class implements an interface then it has to provide public definition of all the methods of the interface . If a class fails to do so then class is declared as abstract. • Interfaces support run time polymorphism i.e. reference variable of an interface can be used to refer objects of those classes that implements the interface.
An interface can not be instantiated (i.e. we can not create object) • Difference between abstract class and interface • 1)Degree of abstraction • i) in case of abstract class may contain non abstract method and non static and non final data members. • i) An interface can only contain abstract method and static final data members . • 2) Type of inheritance • i) classes(abstract and non abstract ) support implementation inheritance. In java multiple inheritance is not supported . • i) interface support multiple interface inheritance .
3) classing environment • i)classes( abstract and non abstract) support static classing environment • i)interface support dynamic classing environment. • Type of inheritance: 1) implementation inheritance • 2) interface inheritance • Purpose of inheritance : • i) code reusability • ii) run time polymorphism