Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 07 08 String Inheritance Relations

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

THIS Keyword

Keyword THIS is a reference variable in Java that refers to


the current object.
The various usages of 'THIS' keyword in Java are as follows:
It can be used to refer instance variable of current class
It can be used to invoke or initiate current class constructor
It can be passed as an argument in the method call
It can be passed as argument in the constructor call
It can be used to return the current class instance
THIS Keyword (Cont…)
THIS Keyword (Cont…)
import java.util.Scanner; // set student record
class student{ public void set_student( int a, String s1,String s2,
private int studentID; teacher s3)
private String studentName; {
private String studentfathersName; this.studentID=a;
private teacher Teacher; this.studentName=s2;
// constructor this.Teacher=s3;
public student() System.out.println("Teachername:
{ "+s3.teacherName);
studentID=1234456; }//set student record
studentName="ABC"; // output
studentfathersName="XYZ"; public void student_output()
}//constructor {
public void student_input() System.out.println("student's Id:" +studentID);
{ System.out.println( "student's name:
Scanner sc=new Scanner( System.in); "+studentName);
System.out.println("Please enter student's Id"); System.out.println("student's father name:
studentID=sc.nextInt(); "+studentfathersName);
System.out.println( "please enter student's name"); System.out.println("Teachername:
studentName=sc.next(); "+Teacher.studentTeacherName());
System.out.println("please enter student's father Teacher.teacher_output();
name"); }//output
studentfathersName =sc.next(); }//class student
}
//class teacher public String studentTeacherName()
class teacher{ {
private int teacherID; return teacherName;
public String teacherName; }
private String teacherfathersName; // output
private student Student;
public void teacher_output()
// constructor
{
public teacher()
System.out.println("teacher's Id:" +teacherID);
{
System.out.println( "teacher's name: "+teacherName);
teacherID=98765;
System.out.println("teacher's father name:
teacherName="LMN";
"+teacherfathersName);
teacherfathersName="RST";
}//output
}//constructor
}//class teacher
public void teacher_input()
{ public class student_teacher
Scanner sc=new Scanner( System.in); {
System.out.println("Please enter teacher's Id"); public static void main(String[] args)
teacherID=sc.nextInt(); {
System.out.println( "please enter teacher's name"); student s1=new student();
teacherName=sc.next(); teacher t1=new teacher();
System.out.println("please enter teacher's father name"); s1.set_student( 5555, "hello","Ram", t1);
teacherfathersName =sc.next(); //s1.student_input();
} s1.student_output();
}//main
}//student_teacher
Inheritance

The objectives of this chapter are:

To explore the concept and implications of


inheritance
Polymorphism
To define the syntax of inheritance in Java
To understand the class hierarchy of Java
To examine the effect of inheritance on
constructors
Terminolo
gy
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
superclass: Person
- name: String
- dob: Date
The subclass can:
Add new functionality
Use inherited functionality
Override inherited functionalitysubclass: Employee
- employeeID: int
- salary: int
- startDate: Date
A very important fact to remember is that Java does not support multiple inheritance. This
means that a class cannot extend more than one class. Therefore following is illegal −
Example
public class extends Animal, Mammal{}
Inheritance
Hierarchy
Each Java class has one (and only one)
superclass.
C++ allows for multiple inheritance

Inheritance creates a class hierarchy


Classes higher in the hierarchy are more general and
more abstract
There is no limit to the Class
Classes of
number lower in the hierarchy
subclasses a are more specific and
concrete
class can have Class Class Class

There is no limit to the


Class Class Class
depth of the class tree.

Class
What really
happens?
When an object is created using new, the system must allocate
enough memory to hold all its instance variables.
This includes any inherited instance variables
With the use of the extends keyword, the subclasses will be able to inherit all the
properties of the superclass except for the private properties of the superclass.

In this example, we can say that an Employee "is a kind of"


Person.
An Employee object inherits all of the attributes, methods and
associations of Person
Person Person
- name: String name = "John Smith"
- dob: Date dob = Jan 13, 1954
Employee
name = "Sally Halls"
is a kind of
dob = Mar 15, 1968
Employee employeeID = 37518
- employeeID: int salary = 65000
- salary: int startDate = Dec 15,
- startDate: Date 2000
15
protected Members


protected access
 Intermediate level of protection between public and
private
 protected members accessible by

superclass members

subclass members

Class members in the same package
 Subclass access to superclass member

Keyword super and a dot (.)
Problems
sheet
Note: You are required to write the program in java using object oriented concept where a small functionality is required to be used as
member function of a class. Also write the abstract of classes.

You are required to create a database that has employee identification, employee name, fathers name, address,
1.

date of birth, designation, hourly payments rate and maximum payments that an employee can be paid in month
for 100 hours. You may use the array concept for storing the records of 100 employees.
You are required to write classes for student details (name, registration number, fathers name), address
2.

(current address, permanent address, contact number, email Id) using inheritance concept where address is
child class.
Further you are required to extend the problem no 2 for addition of the classes, using inherit concept, Courses
3.

Registered (Course name, credit, course number, associated teachers), Academic records( name of degree, year
of passing, university/board, percentage marks), Payments Details ( bank name, transaction id, amount, date).
You are required to create array of string of 10 elements. Further sort the array in ascending order. The string
4.

comparison can be with or without case sensitive.


You are given a paragraph consisting of words, Convert this para graph to tokens (word) to be stored in array
5.

of string in sorted order where sorted array does not have duplicate words.
You are given a paragraph consisting continuous alphabets, Convert this para graph to tokens of fixed size.
6.

Store the tokens in array of string in sorted order where sorted array does not have duplicate words.
You are given a paragraph consisting continuous alphabets, Convert this para graph to tokens based on
7.

occurrence of particular characters. Store the tokens in array of string in sorted order where sorted array does
not have duplicate words.

You might also like