• Save
Dacj 2-1 a
Upcoming SlideShare
Loading in...5
×
 

Dacj 2-1 a

on

  • 960 views

 

Statistics

Views

Total Views
960
Slideshare-icon Views on SlideShare
908
Embed Views
52

Actions

Likes
0
Downloads
0
Comments
0

2 Embeds 52

http://niitcourseslides.blogspot.in 50
http://niitcourseslides.blogspot.com 2

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Dacj 2-1 a Dacj 2-1 a Presentation Transcript

    • Inheritance and InterfacesObjectives In this lesson, you will learn to: • Implement the concept of inheritance in Java • Override methods • Create Interfaces ©NIIT Programming in Java Lesson 1A / Slide 1 of 23
    • Inheritance and InterfacesInheritance in Java • Introduction to Inheritance • Inheritance enables a class to: • Inherit data members and methods from another class. • Reuse the functionalities and capabilities of the existing class by extending a new class from the existing class and adding new features to it. ©NIIT Programming in Java Lesson 1A / Slide 2 of 23
    • Inheritance and InterfacesInheritance in Java (Contd.) • Introduction to Inheritance (Contd.) • The class that inherits the data members and methods from another class is known as the subclass. • The class from which the subclass inherits is known as the superclass. • The superclass is also referred to as the base class, and the subclass is referred to as the derived class. • You can create additional data members and methods to add more features in a subclass. • A superclass can also be a subclass of another class. ©NIIT Programming in Java Lesson 1A / Slide 3 of 23
    • Inheritance and InterfacesInheritance in Java (Contd.) • Implementing Different Types of Inheritance • Single level inheritance • Derives a subclass from a single superclass. For example, subclasses B and C inherit the properties of a single superclass, A. The following figure shows the structure of single level inheritance: ©NIIT Programming in Java Lesson 1A / Slide 4 of 23
    • Inheritance and InterfacesInheritance in Java (Contd.) • Implementing Different Types of Inheritance (Contd.) • Single level inheritance • The following syntax shows how to implement single level inheritance: class A { } class B extends A { } class C extends A { } In the preceding syntax, the extends keyword is used to derive a subclass from a superclass. ©NIIT Programming in Java Lesson 1A / Slide 5 of 23
    • Inheritance and InterfacesDemonstration-ImplementingInheritance • Problem Statement • MoneySaver Corporation bank is conducting interviews for the posts of clerks and Probationary Officers. Various applicants are applying for the required post by filling an application form. The bank performs the selection process of the applicants and the selected applicants are chosen as suitable candidates. The bank wants to keep track of the applicants and candidates by creating an application in Java. Steve, the programmer in the bank is assigned the task of developing the application. The application must accept the applicantID, applicantName, and interviewDate as command-line arguments. Help Steve to develop an application that enables MoneySaver Corporation bank to manage the data. ©NIIT Programming in Java Lesson 1A / Slide 6 of 23
    • Inheritance and InterfacesDemonstration-ImplementingInheritance (Contd.) • Solution • Steve needs to develop the application by implementing the inheritance feature and creating two classes, Applicant and Candidate. The Applicant class is the superclass of the Candidate class. The Applicant superclass enables Steve to enter details, such as applicantID, applicantName, applicantAddress, and applicantPosition. The details of the selected applicants, such as candidateID, candidateName, candidateAddress, candidatePosition, dateOfInterview, and candidateStatus are entered in the Candidate subclass. To solve the given problem, perform the following tasks: 1. Code the application. 2. Compile and execute the application. ©NIIT Programming in Java Lesson 1A / Slide 7 of 23
    • Inheritance and InterfacesInheritance in Java (Contd.) • Implementing Different Types of Inheritance (Contd.) • Multilevel inheritance • Inherits the properties of another subclass. For example, Class A is a superclass for the Class B; and Class B is a superclass for the subclass, Class C. You can include any number of levels in multilevel inheritance. The following figure shows the structure of multilevel inheritance: ©NIIT Programming in Java Lesson 1A / Slide 8 of 23
    • Inheritance and InterfacesInheritance in Java (Contd.) • Implementing Different Types of Inheritance (Contd.) • Multilevel inheritance • The following syntax shows how to implement multilevel inheritance: class A { } class B extends A { } class C extends B { } In the preceding syntax, class A is the superclass and class C is the subclass. The class B acts as a subclass for the class A and superclass for the class C. ©NIIT Programming in Java Lesson 1A / Slide 9 of 23
    • Inheritance and InterfacesImplementing Method Overriding • Method overriding • Method overriding is defined as creating a method in the subclass that has the same return type and signature as a method defined in the superclass. • Signature of a method includes the name, number, sequence, and type of arguments of a method. • The created method of the subclass hides the method defined in the superclass. ©NIIT Programming in Java Lesson 1A / Slide 10 of 23
    • Inheritance and InterfacesImplementing Method Overriding(Contd.) • Method overriding (Contd.) • Method overriding enables you to create objects that respond to the same method as defined in the superclass. • A subclass must override the abstract methods of a superclass. • You cannot override the static and final methods of a superclass. ©NIIT Programming in Java Lesson 1A / Slide 11 of 23
    • Inheritance and InterfacesImplementing Interfaces • Overview of Interface • Interfaces contain a set of abstract methods and static data members. • Interface is known as a prototype for a class. • Methods defined in an interface are only abstract methods. • An abstract method contains only the declaration for a method without any implementation details. • The implementation of an abstract method is defined in the class implementing the interface. • You can implement multiple interfaces in a single class. ©NIIT Programming in Java Lesson 1A / Slide 12 of 23
    • Inheritance and InterfacesImplementing Interfaces (Contd.) • Overview of Interface (Contd.) • The following syntax shows how to define an interface: interface <interfacename> { //interface body static final data members return type public methods(parameters); } You can implement an interface in one or more than one class before defining it. The public access specifier must be specified with the methods declared in the interface. ©NIIT Programming in Java Lesson 1A / Slide 13 of 23
    • Inheritance and InterfacesImplementing Interfaces(Contd.) • Overview of Interface (Contd.) • The following syntax shows how to implement an interface in a class: class <class_name> extends [superclass] implements [interfacename] { //Defining the method declared in the interface. return type public methods(parameters) { } }In the preceding syntax, a class that extends from a superclass implements aninterface using the implements keyword. ©NIIT Programming in Java Lesson 1A / Slide 14 of 23
    • Inheritance and Interfaces Implementing Interfaces (Contd.) • Overview of Interface (Contd.) • Interfaces also enable you to declare constants that can be imported into multiple classes. • The constant values declared in an interface can be implemented in any class.  • The constants defined in an interface are declared using the final keyword. ©NIIT Programming in Java Lesson 1A / Slide 15 of 23
    • Inheritance and Interfaces Demonstration-Implementing Multiple Inheritance using Interfaces • Problem Statement • The management of the LearnMore University is planning to automate the student management system. The University offers various curriculums to the students across the globe.  Therefore, Steve Wilkinson, the programmer, has decided to create a Java application. In the application, Steve needs to store student details, such as ID, name, date of birth, blood group, height, and marks details of the students. As a future proposal for the automated system, the management wants to extend the system to manage its employee details too. ©NIIT Programming in Java Lesson 1A / Slide 16 of 23
    • Inheritance and Interfaces Demonstration-Implementing Multiple Inheritance using Interfaces (Contd.) • Problem Statement (Contd.) • To provide medical benefits to the employees, the system needs to store the blood group and the height of the employee. The application should accept student ID and name of the student as  command-line parameters and display the student details. Help Steve to develop the proposed application. ©NIIT Programming in Java Lesson 1A / Slide 17 of 23
    • Inheritance and Interfaces Demonstration-Implementing Multiple Inheritance using Interfaces (Contd.) • Solution • Steve can develop the application by applying the concept of inheritance. Steve needs to create two classes, StudentInformation and MarksInformation. The  StudentInformation class is the superclass of the MarksInformation class. The StudentInformation class enables Steve to enter details, such as studentID and studentName. He can include the showName() method to display the student details. ©NIIT Programming in Java Lesson 1A / Slide 18 of 23
    • Inheritance and Interfaces Demonstration-Implementing Multiple Inheritance using Interfaces (Contd.) • Solution (Contd.) • The marks details of the student can be stored in the MarksInfo class that is extended from the StudentInfo class. The MarksInformation class contains the details of marks for two  subjects and a method that displays marks details. In addition, Steve can create an interface, MedDetails, which stores the blood group and height. He can use this interface later in the employee management system. To solve the given problem, perform the following tasks: 1. Code an application with an expression having multiple operators of different precedence. 2. Compile and execute the code. ©NIIT Programming in Java Lesson 1A / Slide 19 of 23
    • Inheritance and InterfacesImplementing Interfaces(Contd.) • Implementing Multiple Interfaces in a class interface Bank { public void bankDeposit(); } interface FinancialInstitute { public void securityDeposit(); } ©NIIT Programming in Java Lesson 1A / Slide 20 of 23
    • Inheritance and Interfaces Implementing Interfaces (Contd.) • Implementing Multiple Interfaces in a class (Contd.) class Deposit implements Bank, FinancialInstitute {  int sd = 125; int bd = 256; public void bankDeposit() { System.out.println(" "); System.out.println("t The money deposited in the bank is $" + bd); System.out.println(" "); } ©NIIT Programming in Java Lesson 1A / Slide 21 of 23
    • Inheritance and InterfacesImplementing Interfaces(Contd.) • Implementing Multiple Interfaces in a class (Contd.) public void securityDeposit() { System.out.println(" "); System.out.println("t The money deposited in the financial institute is $" + sd); System.out.println(" "); } public static void main(String args[]) { Deposit d = new Deposit(); d.bankDeposit(); d.securityDeposit(); } } ©NIIT Programming in Java Lesson 1A / Slide 22 of 23
    • Inheritance and InterfacesSummary In this lesson, you learned: • Inheritance is the concept of extending data members and methods of a superclass in a subclass. • You can derive data members and methods from a single superclass that is a subclass of another superclass. • Java does not support multiple inheritance directly. • You can use the concept of method overriding to override the superclass method with the subclass method having same names. • Interface is a concept of creating data members and methods that can be derived by multiple classes in Java. • Interfaces also allow you to declare set of constants that can be imported into multiple classes. • The methods declared in the interface are defined in the class implementing that interface. • The methods in an interface are only abstract methods. ©NIIT Programming in Java Lesson 1A / Slide 23 of 23