Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

itft-Inheritance in java

847 views

Published on

Inheritence, Terminology, Inheritance in java, The class called Object, Super keyword, Example, Method Overriding, Method Overriding example, Abstract Class, Abstract Class Number and the Java Wrapper Classes, Final Method and Classes, Multiple Inheritance

  • Be the first to comment

itft-Inheritance in java

  1. 1. Inheritence
  2. 2. Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another class. The subclass inherits all data attributes of its superclass The subclass inherits all methods of its superclass The subclass inherits all associations of its superclass The subclass can: Add new functionality Use inherited functionality Override inherited functionality Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date superclass: subclass: Terminology
  3. 3. Inheritance is declared using the "extends" keyword If inheritance is not defined, the class extends a class called Object Person - name: String - dob: Date Employee - employeeID: int - salary: int - startDate: Date public class Person { private String name; private Date dob; [...] public class Employee extends Person { private int employeID; private int salary; private Date startDate; [...] Employee anEmployee = new Employee(); Inheritance in java
  4. 4. At the very top of the inheritance tree is a class called Object All Java classes inherit from Object. All objects have a common ancestor This is different from C++ The Object class is defined in the java.lang package Examine it in the Java API Specification The class called Object
  5. 5. Superclass constructors can be called using the "super" keyword. It must be the first line of code in the constructor If a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass. Super keyword
  6. 6. public class BankAccount { private String ownersName; private int accountNumber; private float balance; public BankAccount(int anAccountNumber, String aName) { accountNumber = anAccountNumber; ownersName = aName; } } public class OverdraftAccount extends BankAccount { private float overdraftLimit; public OverdraftAccount(int anAccountNumber, String aName, float aLimit) { super(anAccountNumber, aName); overdraftLimit = aLimit;}} Example
  7. 7. Method Overriding • If a derived or sub class has a method found within its base class or super class, that method will override the base class’s method • The keyword super can be used to gain access to superclass methods overridden by the base class • A subclass method must have the same return type as the corresponding superclass method
  8. 8. public class Abc { void display() { System.out.println(“Hello”); } } public class Xyz extends Abc Output { void display() World { System.out.println(“World”); } public static void main(string str[]) { Xyz obj = new Xyz(); obj.display(); } } Example
  9. 9. public class Abc { void display() { System.out.println(“Hello”); } } public class Xyz extends Abc Output { void display() Hello { World super.display(); //this call Abc display() System.out.println(“World”); } public static void main(string str[]) { Xyz obj = new Xyz(); obj.display(); } } Continue...
  10. 10. Abstract Class • An abstract class can have abstract methods, data fields, and concrete methods • Abstract class differs from a concrete class in that • An abstract class cannot be instantiated • An abstract class can declare abstract methods, which must be implemented in its subclasses
  11. 11. Abstract Class Number and the Java Wrapper Classes Chapter 3: Inheritance and Class Hierarchies 11
  12. 12. Final Method and Classes Methods can be qualified with the final modifier Final methods cannot be overridden. This can be useful for security purposes. Classes can be qualified with the final modifier The class cannot be extended.
  13. 13. Multiple Inheritance • Multiple inheritance: the ability to extend more than one class • Multiple inheritance is a language feature that is difficult to implement and can lead to ambiguity • Therefore, Java does not allow a class to extend more than one class

×