inheritance and java - overriding (copro_2)

4
Inheritance Inheritance can be defined as the process where one object where one object acquires the properties of another. With the use of inheritance the information is made manageable in hierarchical order. When we talk about inheritance the most commonly used keyword be extends and implements”. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object. IS-A Relationship IS-A is a way of saying : This objects is a types of that object. Let us see how the extends keyword is used to achieve inheritance. Now based on the above example, In Object Oriented terms following are true: Animal is the superclass of Mammal class. Animal is the superclass of Reptile class. Mammal and Reptile are sub classes of Animal class. Dogs is the subclass of both Mammal and Animal classes Now if we consider the IS-A relationship we can say Mammals IS –A Animal Reptile IS-A Animal Dogs IS- A Mammal Hence : Dog IS-A Animal as well public class Animal{ } public class Mammal extends Animal{ } public class Reptile extends Animal{ } public class Do extends Mammal{ }

Upload: yuri56

Post on 14-Jan-2015

112 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Inheritance and java - overriding (COPRO_2)

Inheritance

Inheritance can be defined as the process where one object where one object acquires the properties of another. With the use of inheritance the information is made manageable in hierarchical order.

When we talk about inheritance the most commonly used keyword be “extends and implements”. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object.

IS-A Relationship

IS-A is a way of saying : This objects is a types of that object. Let us see how the extends keyword is used to achieve inheritance.

Now based on the above example, In Object Oriented terms following are true: Animal is the superclass of Mammal class. Animal is the superclass of Reptile class. Mammal and Reptile are sub classes of Animal class. Dogs is the subclass of both Mammal and Animal classes

Now if we consider the IS-A relationship we can say Mammals IS –A Animal Reptile IS-A Animal Dogs IS- A Mammal Hence : Dog IS-A Animal as well

With use of the extends keyword the subclasses will be able to inherit all the properties of the subclass except for the private properties of the subclass.

We can assure that mammal is actually an Animal with the use of the instance operator

EXAMPLE:

public class Animal{}public class Mammal extends Animal{}public class Reptile extends Animal{}public class Do extends Mammal{}

public class Dog extends Mammal{public static void main(Strings args[]){

Animal a = new Animal();Mammal m = new mammal();Dog d = new Dog();System.out.println(m. instances of Animal);System.out.println(d. instances of Mammal);System.out.println(d. instances of Animal);

}

}

Page 2: Inheritance and java - overriding (COPRO_2)

Example:

This would produce following result:TrueTrueTrue

Since we have a good understanding of the extends keyword let us look into how the implements keyword is used to get the IS-A relationship.

The implements keyword is used by classes by inherit from interfaces. Interfaces can never be extended by the classes.

EXAMPLE:

The instances of Keyword:

Let us use the instance of operator to check determine whether Mammal is actually an Animal and Dog is actually an Animal.

This would produce following result:TrueTrueTrue

public interface Animal{}public class Mammal implements Animal{}Public class Dogs extends Mammal{}

Interface Animal{}

class Mammal implements Animal()

public class Dog extends Mammal{}

public static void main(String args[]){Mammal m = new Mammal(){Dog d = new Mammal();System.out.println(m.instenceof Animal);System.out.println(d.instenceof Mammal);System.out.println(d.instenceof Animal);

}}

Page 3: Inheritance and java - overriding (COPRO_2)

Java -Overriding

If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final.

The benefits of overriding is: ability to define a behavior that’s specific to the sub class type. which means a subclass can implement a parent class method based on its requirement.

In object oriented terms overriding means to override the functionality of any existing method .

Using the super keyword

When invoking a superclass version of an overridden method the super keyword is used.

EXAMPLE:

This would produce following result:TestDog.java:30: cannot find symbolSymbol: method bark:Location : class Animalb. bark ();

this program will throw a compile time error since b’s reference type Animal doesn’t have a method by the name of bark.