39
IMPLEMENTATION OF INHERITANCE IN JAVA AND C#

Inheritance in java

  • Upload
    techmx

  • View
    3.863

  • Download
    5

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Inheritance in java

IMPLEMENTATION OF INHERITANCE

IN JAVA AND C#

Page 2: Inheritance in java

INHERITANCE..

As the name suggests, inheritance means to take something that is already made.

It is one of the most important feature of Object Oriented Programming.

It is the concept that is used for reusability purpose. Inheritance is the mechanism through which we can derived

classes from other classes. The derived class is called as child class or the subclass or  we can

say the extended class and the class from which we are deriving the subclass is called the base class or the parent class.

Page 3: Inheritance in java

TYPES OF INHERITANCE

The following kinds of inheritance are there in java.   Simple Inheritance   Multilevel Inheritance Hierarchical inheritance Use case of Interfaces (Multiple,Hybrid Inheritance)

Page 4: Inheritance in java

INHERITANCE

Simple Inheritance

When a  subclass is derived simply from it's parent class then this mechanism is known as simple inheritance.

Multilevel Inheritance The process of a subclass is derived from a derived class. In multilevel, one-to-one ladder increases. Multiple classes are involved in inheritance, but one class

extends only one. The lowermost subclass can make use of all its super classes'

members.

Page 5: Inheritance in java

INHERITANCE

Hierarchical Inheritance In hierarchical type of inheritance, one class is extended by many

subclasses. It is one-to-many relationship.

Multiple Inheritance The process of more than one subclass is derived from a same

base class. In multiple, many-to-one ladder increases. Multiple classes are involved in inheritance, but one class extends

only one.

Page 6: Inheritance in java

INHERITANCE

Hybrid InheritanceHybrid Inheritance is the

combination of multiple, hierarchical inheritance.

Page 7: Inheritance in java

IMPLEMENTATION OF INHERITANCE IN JAVA

Page 8: Inheritance in java

INHERITANCE IN JAVA

To know about the concept of inheritance in java we should know about the following concepts,methods,

data members, access controls, constructors, Key words

‘ this ’,

‘ super ‘etc.

To derive a class in java the keyword extends is used.

Page 9: Inheritance in java

‘SUPER’ KEYWORD

. As the name suggest super is used to access the members of the super

class.

It is used for two purposes in java.

1. The first use of keyword super is to access the hidden data variables of the super class hidden by the sub class.

e.g. Suppose class A is the super class that has two instance variables as  int a and float b.

class B is the subclass that also contains its own data members named a and b.

Then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command.

super.member;

Page 10: Inheritance in java

CONT..

2.Use of super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass.

This functionality can be achieved just by using the following command.

super(param-list); Here parameter list is the list of the parameter requires by the

constructor in the super class. super must be the first statement executed inside a super class

constructor. If we want to call the default constructor then we pass the empty

parameter list.

Page 11: Inheritance in java

‘THIS’ KEYWORD

The keyword ’this’ is useful when we need to refer to instance of the class from its method.

‘this’ keyword helps us to avoid name conflicts. As we can see in the program that we have declare the name of

instance variable and local variables same. The keyword this will reference the current class the word appears

in. It will allow you to use the classes methods if used like this

this.methodName();

Page 12: Inheritance in java

ACCESS SPECIFIERS IN JAVA There are four Access Specifiers in Java

1. Public: When a member of a class is declared as public specifier, it can be accessed from any code.

2. Protected: Protected is only applicable in case of Inheritance. When a member of a class is declared as protected, it can only be accessed by the members of its class or subclass.

3. Private: A member of a class is declared as private specifier, can only be accessed by the member of its class.

4. Default: When you don't specify a access specifier to a member, Java automatically specifies a default. And the members modified by default can only be accessed by any other code in the package, but can't be accessed outside of a package.

Page 13: Inheritance in java

AN EXAMPLE OF MULTI LEVEL INHERITANCE

A Scenario where one class is inheriting/extending the behaviour of another class which in turn is inheriting behavior from yet another class.

Ex: public class Automobile {…} Public class Car extends Automobile {…} Public class Ferrari extends Car {…}

This multilevel inheritance actually has no limitations on the number of levels it can go.

So as far as java goes, it is limitless. But for maintenance and ease of use sakes it is better to keep the inheritance levels to a single digit number.

Page 14: Inheritance in java

EXAMPLE:

class Aves

{

public void nature() {

System.out.println(”Aves fly"); }

}

class Bird extends Aves

{

public void eat(){

System.out.println("Eats to live"); }

}

class Parrot extends Bird

{

public void food() {

System.out.println("Parrot eats seeds andfruits"); }

Aves

Parrot

Bird

Page 15: Inheritance in java

CONT..

public static void main(String args[])

{

Parrot p1 = new Parrot();

p1.food(); // calling its own

p1.eat(); // calling super class Bird method

p1.nature(); // calling super class Aves method

}

}

Page 16: Inheritance in java

HOW TO DO MULTIPLE INHERITANCE IN JAVA?

Actually, java does not support multiple inheritance

However, you can achieve partial multiple inheritance with the help of interfaces.

Ex: public class FerrariF12011 extends Ferrari implements Car, Automobile {…}

And this is under the assumption that Car and Automobile are interfaces.

Page 17: Inheritance in java

INTERFACES IN JAVA

An interface is a container of abstract methods. It allows Java to implement Multiple Inheritance, because a class

can't have more than one superclass in Java, but can implements many interfaces.

Methods are just declared in interface, but not defined. The class which implements an interface must have to define the method declared in the interface.

Access modifiers and return type must be same as declared in the interface.

Private and static methods can't be declared in the interface.

Page 18: Inheritance in java

WHY DOESN'T JAVA ALLOW MULTIPLE INHERITANCE?

Let us say the Automobile Class has a drive() method and the Car class has a drive() method and the Ferrari class has a drive() method too. Let us say we create a new class FerrariF12011 that looks like below: Public class FerrariF12011 extends Ferrari, Car, Automobile {…}

And at some point of time we need to call the drive() method, what would happen? Our JVM wouldn't know which method to invoke and we may have to instantiate one of the classes that you already inherit in order to call its appropriate method. To avoid this why the creators of java did not include this direct multiple inheritance feature.

Page 19: Inheritance in java

EXAMPLE

interface Suzuki{

public abstract void body(); } interface Ford {

public abstract void engine(); } public class MotorCar implements Suzuki, Ford{

public void body(){ System.out.println("Fit Suzuki body"); }

Suzuki

MotorCar

Ford

Page 20: Inheritance in java

CONT..

public void engine() {

System.out.println("Fit Ford engine"); }

public static void main(String args[]) { MotorCar mc1 = new MotorCar(); mc1.body(); mc1.engine();

} }

Page 21: Inheritance in java

How it works???In the above code there are two interfaces – Suzuki and

Ford.

Both are implemented by MotorCar because it would like to have the features of both interfaces Just to inform there are multiple interfaces and not classes,

tell the compiler by replacing "extends" with "implements”. MotorCar, after implementing both the interfaces, overrides

the abstract methods of the both – body() and engine(); else program does not compile.

Page 22: Inheritance in java

‘FINAL’ KEYWORD

It can also be called as Restricting Inheritance Classes that cant be extended are called final classes. We use the final modifier in the definition of the class to

indicate this.

final class myclass

{// Insert code here

}

Page 23: Inheritance in java

IMPLEMENTATION OF INHERITANCE IN C#

Page 24: Inheritance in java

INHERITANCE IN C#

Syntax for deriving a class from a base class

A derived class inherits most elements of its base class

A derived class cannot be more accessible than its base class

class Token{ ...}class CommentToken: Token{ ...}

class Token{ ...}class CommentToken: Token{ ...}

Derived classDerived class Base classBase class

ColonColon

Page 25: Inheritance in java

CALLING BASE CLASS CONSTRUCTORS Constructor declarations must use the base

keyword

A private base class constructor cannot be accessed by a derived class

Use the base keyword to qualify identifier scope

class Token{ protected Token(string name) { ... } ...}class CommentToken: Token{ public CommentToken(string name) : base(name) { } ...}

class Token{ protected Token(string name) { ... } ...}class CommentToken: Token{ public CommentToken(string name) : base(name) { } ...}

Page 26: Inheritance in java

DEFINING VIRTUAL METHODS

Syntax: Declare as virtual

Virtual methods are polymorphic

class Token{ ... public int LineNumber( ) { ... } public virtual string Name( ) { ... } }

class Token{ ... public int LineNumber( ) { ... } public virtual string Name( ) { ... } }

Page 27: Inheritance in java

OVERRIDING METHODS

Syntax: Use the override keyword

class Token{ ... public virtual string Name( ) { ... } }class CommentToken: Token{ ... public override string Name( ) { ... }}

class Token{ ... public virtual string Name( ) { ... } }class CommentToken: Token{ ... public override string Name( ) { ... }}

Page 28: Inheritance in java

WORKING WITH OVERRIDE METHODS

You can only override identical inherited virtual methods

You must match an override method with its associated virtual method

You can override an override method You cannot explicitly declare an override method as

virtual You cannot declare an override method as static or private

class Token{ ... public int LineNumber( ) { ... } public virtual string Name( ) { ... } }class CommentToken: Token{ ... public override int LineNumber( ) { ... } public override string Name( ) { ... }}

class Token{ ... public int LineNumber( ) { ... } public virtual string Name( ) { ... } }class CommentToken: Token{ ... public override int LineNumber( ) { ... } public override string Name( ) { ... }}

û

Page 29: Inheritance in java

USING NEW TO HIDE METHODS

Syntax: Use the new keyword to hide a method

class Token{ ... public int LineNumber( ) { ... }}class CommentToken: Token{ ... new public int LineNumber( ) { ... } }

class Token{ ... public int LineNumber( ) { ... }}class CommentToken: Token{ ... new public int LineNumber( ) { ... } }

Page 30: Inheritance in java

WORKING WITH THE NEW KEYWORD

Hide both virtual and non-virtual methods

Resolve name clashes in code Hide methods that have identical

signatures

class Token{ ... public int LineNumber( ) { ... } public virtual string Name( ) { ... } }class CommentToken: Token{ ... new public int LineNumber( ) { ... } public override string Name( ) { ... }}

class Token{ ... public int LineNumber( ) { ... } public virtual string Name( ) { ... } }class CommentToken: Token{ ... new public int LineNumber( ) { ... } public override string Name( ) { ... }}

Page 31: Inheritance in java

MULTI LEVEL INHERITANCE

Multi Level Inheritance is supported in C#.

Any level of inheritance is possible to inherit a class “:” is used after the class name.

Like ‘super’ keyword in Java, here we have ‘base’ keyword which can be used during header initialization.

It will invoke the immediate base class of the calling class.

Page 32: Inheritance in java

SAMPLE PROGRAMclass person { string name; int age; public person() { Console.Write("Enter Name : "); name = Console.ReadLine(); Console.Write("Enter age : "); age = Convert.ToInt32(Console.Read()); } public virtual void put_data() { Console.Write("Name : " + name); Console.Write("Age : " + age); } }

Employee

Person

Program

Page 33: Inheritance in java

CONT..

class employee : person { int salary; public employee() { Console.Write("Enter Salary : "); salary = Convert.ToInt32(Console.Read); } public override void put_data() { Console.Write("Salary : " + salary); } }

Page 34: Inheritance in java

CONT..

class Program : employee { static void Main(string[] args) { employee e1 = new employee(); e1.put_data(); Console.WriteLine("Press any key to exit..."); Console.ReadLine();

} }

Page 35: Inheritance in java

MULTIPLE INHERITANCE

Like Java C# does’nt have multiple inheritance.

The reason is same that of java. We can perform that through interfaces

we can implement any number of interfaces.

Page 36: Inheritance in java

DECLARING INTERFACES

interface IToken{ int LineNumber( ); string Name( );}

interface IToken{ int LineNumber( ); string Name( );}

IToken« interface »

IToken« interface »

LineNumber( )Name( )LineNumber( )Name( )

No method bodiesNo method bodies

Interface names should begin with a capital “I”

Interface names should begin with a capital “I”

No access specifiersNo access specifiers

Page 37: Inheritance in java

IMPLEMENTING MULTIPLE INTERFACES

A class can implement zero or more interfaces

An interface can extend zero or more interfaces A class can be more accessible than its base

interfaces An interface cannot be more accessible than its base

interfaces A class must implement all inherited interface

methods

interface IToken{ string Name( );}interface IVisitable{ void Accept(IVisitor v);}class Token: IToken, IVisitable{ ...}

interface IToken{ string Name( );}interface IVisitable{ void Accept(IVisitor v);}class Token: IToken, IVisitable{ ...}

IToken« interface »

IToken« interface »

IVisitable« interface »

IVisitable« interface »

TokenToken

Jon Jagger
All method declarations must name the parameters
Page 38: Inheritance in java

IMPLEMENTING INTERFACE METHODS The implementing method must be the same

as the interface method The implementing method can be virtual or

non-virtual

class Token: IToken, IVisitable{ public virtual string Name( ) { ... } public void Accept(IVisitor v) { ... }}

class Token: IToken, IVisitable{ public virtual string Name( ) { ... } public void Accept(IVisitor v) { ... }}

Same access Same return typeSame nameSame parameters

Same access Same return typeSame nameSame parameters

Page 39: Inheritance in java

THANK YOU