Вы находитесь на странице: 1из 44

IN-HOUSE PRACTICAL TRAINING

On

INHERITANCE AND INTERFACES IN JAVA

Submitted To

Department of Computer Science and Engineering

AMITY SCHOOL OF ENGINEERING AND TECHNOLOGY

Guided By: Submitted By:

Ms. Garima Srivastava Grace Sharon Varghese

Assistant Professor Enroll no: A7605216026

ASET

AMITY UNIVERSITY, UTTAR PRADESH

LUCKNOW CAMPUS
DECLARATION
I understand what plagiarism is and am aware of the University’s policy in this regard. I declare
that

a) The work submitted by me in partial fulfillment of the requirement for the award of degree.
B.Tech (CSE) assessment in this project is my own; it has not previously been presented for
another assessment.

b) I declare that this project is my original work. Whenever work from other source has been
used, all debts (for works, data, arguments and ideas) have been appropriately acknowledged and
referenced in accordance with the requirement of NTCC Regulations and Guidelines.

c) I have not used work previously produced by another student or any other to submit it as my
own.

d) I have not permitted, and will not permit, anybody to copy my work with purpose of
presenting it off as his or her own work.

e) The work conforms to the guidelines for the layout, content and style as set out for the
Regulations and Guidelines.

Date: Grace Sharon Varghese

A7605216026

B.Tech (CSE)
ACKNOWLEDGEMENT
First and foremost, I would like to thank God for his blessings. I would also take this opportunity
to thank the people who helped me in completing the project. I would like to express my
gratitude to Dr. Deepak Arora, HOD, Computer Science and Engineering, ASET, Amity
University, Lucknow, for providing me an opportunity to do my NTCC in Amity University,
Lucknow.

I also take this opportunity to thank Ms. Garima Srivastava, Assistant Professor, Computer
Science and Engineering Department, ASET, Amity University, Lucknow, for her constant
guidance and useful suggestions. I am grateful that my parents too played a major role in the
completion of this project.

Grace Sharon Varghese

A7605216026
CERTIFICATE
This is to certify that Ms.GRACE SHARON VARGHESE, enrollment no: A7605216026,
student of B.Tech in Computer Science and Engineering has carried out the work presented in
the project of NTCC entitle “Inheritance and Interfaces” as a part of 3rd year (5th semester)
programme of Bachelor of Technology in Computer Science and Engineering from Amity
School of Engineering and Technology. This report was made successful under my guidance and
supervision.

Ms. Garima Srivastava

Deptt. Of Computer

Science and Engineering

ASET, Lucknow
ABSTRACT
Inheritance is defined as the way of acquiring the properties of one class by the other. Properties
include the methods and the fields. By using the inheritance, it used to manage the information in
a hierarchical order. When the properties of a class are inherited is known as super class and the
one that inherits these characteristics is referred to as child class. Let’s take a real life example to
learn inheritance easily. In the family tree, the child acquires the characteristics or we can say the
traits from the parents. The same concept is used in classes. In inheritance, we use a keyword
that is the extends keyword. This helps in inheriting the characteristics of a class. If constructor is
present, they cannot be inherited by the child class, but it can call the constructor in the parent
class.

Interfaces and classes are same by syntax, but they have few instance variables and their
declaration is without any body.Interface can be implemented by any number of times classes
once it is defined. A multiple of interfaces will be implemented by a class. A class should be
having a proper set of methods, which are defined by the interface for implementing an interface.
But each class has the freedom to decide the content of its own implementation. The aspect of
polymorphism, “one interface, and multiple methods” can be utilized because of the interface
keyword provided by Java. Multiple interfaces are implemented with the help of Java class. Like
in inheritance, interface also uses a keyword “implements” after the class name. In interface, we
can send messages without the concern of the respective class. Instantiation is not seen in
interface. Implementation of an interface from another interface is not possible. When an
interface is declared inside another, it is known as nested interface. The interface is identified
from the .java extension. The appearance of byte code is in .class file.
TABLE OF CONTENTS

Declaration………………………………………………………………...I

Acknowledgment…………………………………………………………..II

Certificate ………………………………………………………………...III

Abstract …………………………………………………………………..VI

Introduction ………………………………………………………………V

Inheritance ……………………………………………………………….VI-XII
Types of inheritance

Super keyword............................................................................................XIV-XVII
Usage of Java super keyword

Overriding………………………………………………………………...XVIII

Packages………………………………………………………………….XIX-XXII
Advantages of packages

Interfaces…………………………………………………………………XXIII-XXXII
Implementing interfaces
Nested interfaces
Interfaces can be extended
Interface inheritance
Multiple inheritance in Java interface
Static method in interface
Default method in interface

Conclusion………………………………………………………………..XXXIII

References

Plagiarism report
INTRODUCTION
Inheritance is a way in which class which is to be created from a class already existing by using
or extending the possible data members and methods of that class. The codes can be reused, thus
reusability of code is possible. The code in the parent class need not be rewritten in the child
class. This makes the data members and methods to be used in the child class. Polymorphism is
allowed because of the method overriding in inheritance. The tight coupling of two classes occur
which a disadvantage of inheritance. That is, if we make any change in the parent class, the child
class will also get affected. Therefore it cannot be independent. If constructor is present, they
cannot be inherited by the child class, but it can call the constructor in the parent class. The
important terms used are:

1. Super Class: The properties of this class are inherited by other classes.

2. Sub Class: This class inherits the properties of an already existing class.

3. Reusability: It means that when we create a new class, we can take the code of the existing
class containing the methods and fields.

Interface and class almost similar, but it contains abstract method and static constants. It is a
blueprint of the class. In Java, we can have multiple interfaces. Multiple interfaces are
implemented with the help of Java class. Like in inheritance, interface also uses a keyword
“implements” after the class name. In interface, we can send messages without the concern of the
respective class. Instantiation is not seen in interface. Implementation of an interface from
another interface is not possible. When an interface is declared inside another, it is known as
nested interface. The interface is identified from the .java extension. The appearance of byte
code is in .class file.

Why interface?

1. Abstraction

2. Multiple Inheritance

3. Loose coupling
INHERITANCE
Inheritance is defined as the way of acquiring the properties of one class by the other. Properties
include the methods and the fields. By using the inheritance, it used to manage the information in
a hierarchical order. When the properties of a class are inherited is known as super class and the
one that inherits these characteristics is referred to as child class. Let’s take a real life example to
learn inheritance easily. In the family tree, the child acquires the characteristics or we can say the
traits from the parents. The same concept is used in classes.

In inheritance, we use a keyword that is the extends keyword. This helps in inheriting the
characteristics of a class. The format is:

class Parent{

-----------

class Child extends Parent

-----------

Example:

class Parent {

public void p1()

System.out.println(“Parent method”);

public class Child extends Parent{

public void c1()

System.out.println(“Child method”);

}
public static void main(String args[])

Child cobj=new Child();

cobj.c1();

cobj.p1();

Example 2:

class Calculation {

int z;

public void addition(int x,int y) {

z=x+y;

System.out.println(“The sum of the given numbers:”+z);

public void Subtraction(int x,int y) {

z=x-y;

System.out.println(“The difference between the given numbers:”+z);

public class My_Calculation extends Calculation {

public void multiplication(int x,int y) {

z=x*y;

System.out.println(“The product of the given numbers:”+z);

}
public static void main(String args[]){

int a=20, b=10;

My_Calculation demo=new My_Calculation();

demo.addition(a,b);

demo.subtraction(a,b);

demo.multiplication(a,b);

Output:

The sum of the given numbers: 30

The difference of the given numbers: 10

The product of the given numbers: 200

Types of inheritance in Java:

1. Single Inheritance:

It is very easy concept. It means that when only one class is being inherited from a particular
class. That is, let A be the parent class having some properties and these properties are being
inherited by a child class B.

Program:

Class Father{
public void methodFather()

System.out.println(“Base class method”);

class Son extends Father{

public void methodSon()

System.out.println(“Child class method”);

public static void main(String args[])

Son obj=new Son();

obj.methodFather(); //calling superclass method

obj.methodSon(); //calling local method

2. Multilevel Inheritance:

Multilevel inheritance is a process in which we inherit a class from a parent class and the class
which is derived serve as parent class for the next inherited class. For example, C is the child of
B and B of A.

C
Program:

class public void methodX()

System.out.println(“Class X method”);

class Y extends X

public void methodY()

System.out.println(“Class Y method”);

class Z extends Y

public void methodZ()

System.out.println(“Class Z method”);

public static void main(Strings args[])

Z obj=new Z();

obj.methodX(); //calling grand parent class method


obj.methodY(); //calling parent class method

obj.methodZ(); //calling local method

3. Hierarchical Inheritance:

In hierarchical inheritance, multiple classes can be inherited from a single class. This means that,
there is a single parent class and many child classes. For example A is the parent class and B,C
and D are the child classes.

B C D

Program:

class A

public void methodA()

System.out.println(“method of Class A”)

class B extends A

public void methodB()

{
System.out.println(“method of Class B”);

class C extends A

public void methodC()

System.out.println(“method of Class C”);

class D extends A

public void method()

System.out.println(“method of Class D”);

class JavaExample

public static void main(String args[])

B obj1=new B();

C obj2=new C();

D obj3=new D();

//all classes can access the method of class A


obj1.methodA();

obj2.methodA();

obj3.methodA();

}
SUPER KEYWORD
When we want to mention the parent class of a child class immediately, then we use super
keyword. Or we can say that the subclass uses the super keyword when it wants to mention the
super class.

Class Dad

String name;

public class Son extends Dad {

String name;

public void details()

super.name = “Dad”;

name = “Son”:

System.out.println(super.name+“ and ”+name);

public static void main(String args[])

Son sobj = new Son();

sobj.details();

USAGE OF JAVA SUPER KEYWORD

1. It can be used to mention immediate parent class instance variable.

Example:
class Animal{

String color= “white”;

class Cat extends Animal{

String color= “black”;

void printColor(){

System.out.ptintln(color); //prints color of Cat class

System.out.println(suoer.color); //prints color of Animal class

class TestSuper1{

public static void main(String args[])

Cat c=new Cat();

c.printColor();

Output:

black

white

2. It can be used to invoke parent class method

Example:

class Animal{

void eat(){
System.out.println(“eating…”);

class Car extends Animal{

void eat(){

System.out.println(“mewing…”);

void work(){

super.eat();

mew();

class TestSuper2{

public static void main(String args[])

Cat c= new Cat();

c.work();

Output:

eating…

mewing…

3. Super is used to invoke parent class constructor

Example:

class Animal{
Animal(){

System.out.println(“animal is created”);

class Cat extends Animal{

Cat();

super();

System.out.println(“cat is created”);

class TestSuper3{

public static void main(String args[]){

Cat c=new Cat();

Output:

animal is created

cat is created
OVERRIDING
When the class which is derived is having the same methods same as in the super class, it is
referred to as method overriding.

To achieve Run Time Polymorphism we use method overriding.

The conditions for overriding are given as:

 It should be having the same method name


 It should shows the same return type
 It should will have the same argument list
 A method which is declared as final is not allowed to override
 A static method is able to override but is not allowed to re-declare
 Method that is not inherited will not override.
 Constructors cannot be overridden

A static method will not override. This is so as the static method will be bound to the class. We
also cannot override the java main method as it is a static method.

Program:

class Parent

void show(){

System.out.println(“Parent’s show()”);

class Child extends Parent

//this method overrides show() of Parent

@Override

void show()
{

System.out.println(“Child’s show()”);

class Main

public static main(String args[])

Parent obj1=new Parent();

obj1.show();

//if a parent type reference refers to a child object Child’s show() is called run time
polymorphism.

Parent obj2.new Child();

obj2.show();

}
PACKAGES
It is defined as group of related classes which can be created using the keyword package. The
statement package should be mentioned first in the .java file, only then other comments are
allowed. Also, the package statement should come before the import statement. For importing
the packaging in a program we use the import keyword. When a class is declared as public, then
it can be used globally and the instances of these classes can be created from inside and outside
of the class and package.

To compile a java package we use the synatax:


javac -d directory javafilename

For running a java package program, we need the syntax:


java javafilename

ADVANTAGES OF JAVA PACKAGES:

 Provide access protection


 Avoids name collision
 Group the classes and interfaces for easy maintainance

Program:
//A.java

package.pack;

public class A

public void msg()

System.out.println(“HELLO”);;

We compile and copy the class file(A.class) into a folder named pack. After that we execute
B.java file, we can access all the methods in the A.java file by importing the packages from the
pack.

//B.java

import pack.*;

class B

public static void main(String args[])

A.obj=new A();

Obj.msg();

Program 2:
//Pclass.java

package pg;

public class Pclass

public int x;

public String setX(int i)

x=l;

return x;

//Pclass1.java

import com.pg.*;

public class Pclass1

public static void main(String args[])

Pclass pc=new Pclass();

System.out.println(“The value of x is”+PC.setX(5));

}
For keeping two public classes in a package, we need to have two java source files with one
public class, but should keep the package name same.

Example:

//save as A.java

package javatpoint;

public class A{}

//save as B.java

package javatpoint;

public class B{}

The three ways to access the package from outside the package are:

1. import package.*;

2. import package.classname;

3. fully qualified name.


INTERFACES
Interfaces and classes are same by syntax, but they have few instance variables and their
declaration is without any body.

Interface can be implemented by any number of times classes once it is defined. A multiples of
interfaces will be implemented by a class. A class should be having a proper set of methods,
which are defined by the interface for implementing an interface. But each class have the
freedom to decide the content of its own implementation. The aspect of polymorphism, “one
interface, multiple methods” can be utilized because of the interface keyword provided by Java.

Defining an Interface:

If we want to define an interface, it can be defined in the same way as we define class. The
common format of an interface is:

access interface name{


return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2= value;
//……..
return-type method-name(parameter-list);
type final-varnameN = value;
}

Example:

interface Fee{

float fine();

class TuitionCentre implements Fee{

public float fine(){return 10.50f;}

class College implements Fee{


public float fine(){return 10.25f;}

class TestInterface2{

public static void main(String[] args){

Fee f=new TuitionCentre();

System.out.println("FINE: "+f.fine());

Output:

FINE:10.50

The access modifier can be either public or is not used. When there is no access modifier being
used, it will have default modifier and given interface is given to other members of the package
that is declared.

Public access is used; other code can be used to access the interface. There is a valid identifier,
known as ‘name’. All the methods which declared don’t have a body. It is terminated by a
semicolon which is followed by the parameter list. There will be not any implementation in
default of a given method which in that interface. The interface which is included in every class
will implement all the methods.

Declaration of variables is within the interface. It can be implicitly final and static. The class
which is implementing cannot change variables. A constant value must b initialized. Let’s look
into an example which declares an interface containing a method called callback() which can
take only one parameter.

interface Callback{

void callback(int param);

}
IMPLEMENTING INTERFACES

After the creation of a given interface, it will be implemented by multiple classes. For the
implementation an interface, we will implement class definition which is in the clause and then
the methods are created which is defined by the interface. The format for the implementation of
clauses by class is:

access class classname [extends superclass] [implements


interface1, [interface2….]]
{
//class-body
}

The modifiers will be public, but sometimes is not specified. If there are multiple interfaces
implemented by a class, then it is separated by comma. The method that implements an interface
must be declared public.

Let’s have an example with the Callback method.

class Customer implements Callback {


public void callback(int c) {
System.out.println(“callback called with”+c);
}
}

Example: Drawable

The interface, Drawable has only one method. The classes, Rectangle and Circle provide the
implementation. The implementation provider is different from the one which defines the
interface and is being used by someone else. The user hides the implementation part.

File:TestInterface1.java

interface Drawable

void draw();
}

class Rectangle implements Drawable

public void draw()

System.out.println(“drawing rectangle”);

class Circle implements Drawable

public void draw()

System.out.println(“drawing circle”);

class TestInterface1

public static void main(String args[])

Drawable d=new Circle();

d.draw();

}
NESTED INTERFACE

An interface can be declared as a member of a class or another interface. It is a blueprint of the


class. This type of interface is known as nested interface. A nested interface can have the access
modifiers like public, private, or protected. The name of the class or interface should be
specified when the nested interface is used outside the limits. Since the nested interface is
declared, the outside of the class or interface, its name is qualified fully.

interface interface_name {

…….

interface nested_interface_name {

…….

Example:

interface Showable{

void show();

interface Message {

void msg();

class TestNestedInterface1 implements Showable.Messsage {

public void msg() {

System.out.println(“Hello nested interface”);

public static void main(String args[])

Showable.Message message =new TestNestedInterface();

message.msg();

}
}

INTERFACES CAN BE EXTENDED

The extend keyword is used to inherit an interface from another. It has the same syntax as in the
inheritance of classes. The implementation which is to be included in the methods are to be
given, and are then needed by the chain of interface inheritance. This happens when an interface
inherited by an interface is inherited by a class.

Example:

interface A{

void meth1();

void meth2();

//B now includes meth1() and meth2() –it adds meth3()

interface B extends A

void meth3();

//this class must implement all of A and B

class MyClass implements B{

public void meth1(){

System.out.println(“Implements meth1().”);

public void meth2() {

System.out.println(“Implements meth2().”);

public void meth3() {


System.out.println(“Implements meth3().”);

class IFExtend {

public static void main(String args[])

MyClass ob =new MyClass();

ob.meth1();

ob.meth2();

ob.meth3();

If we try to remove the implementation for meth1() in MyClass, then it will create a compile-
time error.

INTERFACE INHERITANCE

interface Printable{

void print();

interface Showable extends Printable{

void show();

class TestInterface4 implements Showable{

public void print()

System.out.println(“Hello”);
}

public static void main(String args[])

System.out.println(“Welcome”);

public static void main(String args[])

TestInterface4 obj=new TestInterface4();

obj.print();

obj.show();

Output:

Hello

Welcome

MULTIPLE INHERITANCE IN JAVA INTERFACE

interface Printable{

void print();

interface Showable{

void show();

class A7 implements Printable,Showable{

public void print()


{

System.out.println(“Hello”);

public void show()

System.out.println(“Welcome”);

public static void main(Strings args[]){

A7 obj=new A7();

obj.print();

obj.show();

Output:

Hello

Welcome

STATIC METHOD IN INTERFACE

interface Drawable {

void draw();

static int cube(int x)

return x*x*x;

class TestInterfaceStatic{

public static void main(Strings args[])


{

Drawable d=new Rectangle();

d.draw();

System.out.println(Drawable.cube(3));

Output:

drawing rectangle

27

DEFAULT METHOD IN INTERFACE

interface Drawable{

void draw();

default void msg()

System.out.println(“default method”);

class Rectangle implements Drawable {

public void draw()

System.out.println(“drawing rectangle”);

class TestInterfaceDefault{
public static void main(String args[])

Drawable d=new Rectangle();

d.draw();

d.msg();

Output:

drawing rectangle

default method
CONCLUSION
Inheritance is a way in which class which is to be created from a class already existing by using
or extending the possible data members and methods of that class. The codes can be reused, thus
reusability of code is possible. The code in the parent class need not be rewritten in the child
class. This makes the data members and methods to be used in the child class. Polymorphism is
allowed because of the method overriding in inheritance. The tight coupling of two classes occur
which a disadvantage of inheritance. That is, if we make any change in the parent class, the child
class will also get affected. Therefore it cannot be independent. If constructor is present, they
cannot be inherited by the child class, but it can call the constructor in the parent class. The
important terms used are:

1. Super Class: The properties of this class are inherited by other classes.

2. Sub Class: This class inherits the properties of an already existing class.

3. Reusability: It means that when we create a new class, we can take the code of the existing
class containing the methods and fields.

Interface and class almost similar, but it contains abstract method and static constants. It is a
blueprint of the class. In Java, we can have multiple interfaces. Multiple interfaces is
implemented with the help of Java class. Like in inheritance, interface also uses a keyword
“implements” after the class name. In interface, we can send messages without the concern of the
respective class. Instantiation is not seen in interface. Implementation of an interface from
another interface is not possible. When an interface is declared inside another, it is known as
nested interface. The interface is identified from the .java extension. The appearance of byte
code is in .class file.

Why interface?

1. Abstraction

2. Multiple Inheritance

3. Loose coupling
REFERENCES

 Javatpoint
 GreeksforGreeks
 BegineersBook.com
 Wikipedia
Amity School Of Engineering And Technology,

Lucknow Campus

Amity University Uttar Pradesh

STUDENT NTCC DIARY


Student Name: GRACE SHARON VARGHESE

Enrollment No: A7605216026

Course and Semester: B.tech (CSE), 5

NTCC Topic: Inheritance and Interfaces in Java

Commencing Date: 4/06/2018

Completion Date: 30/06/2018

DAY DATE QUERIES SUGGESTIONS GIVEN BY


FACULTY GUIDE
1 19/06/2018 I send my initial documentation for Suggested me some more topics to
approval. add up.
2 22/06/2018 I asked more ideas to work on Provided me some more ideas
related to the topic.
3 27/06/2018 I send my documentation to get She approved it and asked for the
suggestion weekly report.
4 29/06/2018 I send all weekly reports She advised me to fill the
synopsis.

Name of student: Grace Sharon Varghese

Signature:
WEEKLY PROGRESS REPORT

ON

INHERITANCE AND INTERFACES IN JAVA

Submitted to

Department of Computer Science and Engineering

AMITY SCHOOL OF ENGINEERING AND TECHNOLOGY

Guided By: Submitted By:

Ms. Garima Srivastava Grace Sharon Varghese

Assistant Professor Enroll no: A7605216026

ASET B.Tech CSE

AMITY UNIVERSITY, UTTAR PRADESH


WEEKLY PROGRESS REPORT
Student Name: GRACE SHARON VARGHESE WPR NO. : 1

Enrollment No: A7605216026

Program: B.tech(CSE)

Semester: 5

NTCC Topic: Inheritance and Interfaces in Java

Commencing Date: 04/06/2018

Completion Date: 10/06/2018

DAY DATE ACHIEVEMENTS


1 04/06/2018 Decided a topic which is
relevant as a project for
B.Tech Computer Science.
2 05/06/2018 Discussed with the faculty
guide.
3 07/06/2018 Collected matters related to
inheritance.
4 09/06/2017 Wrote the introduction on the
topic.
5 10/06/2018 Inheritance and types of
inheritance.

Signature of Student:

Name of Student: Grace Sharon Varghese


WEEKLY PROGRESS REPORT
Student Name: GRACE SHARON VARGHESE WPR NO. : 2

Enrollment No: A7605216026

Program: B.tech (CSE)

Semester: 5

NTCC Topic: Inheritance and Interfaces in Java

Commencing Date: 11/06/2018

Completion Date: 17/06/2018

DAY DATE ACHIEVEMENTS


1 12/06/2018 . Wrote on super keyword and
its usage.
2 14/06/2018 Description on overriding.
3 15/06/2018 Packages.

4 16/06/2018 Wrote on interfaces.

5 17/06/2018 How to implement interface


and on nested interface.

Signature of Student:

Name of Student: Grace Sharon Varghese


WEEKLY PROGRESS REPORT
Student Name: GRACE SHARON VARGHESE WPR NO. : 3

Enrollment No: A7605216026

Program: B.tech (CSE)

Semester: 5

NTCC Topic: Inheritance and Interfaces in Java

Commencing Date: 18/06/2018

Completion Date: 24/06/2018

DAY DATE ACHIEVEMENTS


1 18/06/2018 How to extend an interface
and on interface inheritance..
2 20/06/2018 Description on multiple
inheritance in Java interface
and on static method in
interface.
3 21/06/2018 Description on static method
in interface.
4 22/06/2018 Use of default method in
interface.
5 23/06/2018 Conclusion of the topic.

Signature of Student:

Name of Student: Grace Sharon Varghese


WEEKLY PROGRESS REPORT
Student Name: GRACE SHARON VARGHESE WPR NO. : 4

Enrollment No: A7605216026

Program: B.tech (CSE)

Semester: 5

NTCC Topic: Inheritance and Interfaces in Java

Commencing Date: 25/06/2018

Completion Date: 30/06/2018

DAY DATE ACHIEVEMENTS

1 25/06/2018 She suggested me to write


more explanation of the terms
which were used.
2 26/05/2018 Suggested me some more
topic to add up.
3 27/06/2018 Provided me some more ideas
related to the topic.
4 228/06/2018 She approved it and asked for
weekly reports.
5 29/06/2018 She advised me fill the
synopsis.

Signature of Student:

Name of Student: Grace Sharon Varghese

Вам также может понравиться