Академический Документы
Профессиональный Документы
Культура Документы
On
Submitted To
ASET
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.
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.
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.
Deptt. Of Computer
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{
-----------
-----------
Example:
class Parent {
System.out.println(“Parent method”);
System.out.println(“Child method”);
}
public static void main(String args[])
cobj.c1();
cobj.p1();
Example 2:
class Calculation {
int z;
z=x+y;
z=x-y;
z=x*y;
}
public static void main(String args[]){
demo.addition(a,b);
demo.subtraction(a,b);
demo.multiplication(a,b);
Output:
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()
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:
System.out.println(“Class X method”);
class Y extends X
System.out.println(“Class Y method”);
class Z extends Y
System.out.println(“Class Z method”);
Z obj=new Z();
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
class B extends A
{
System.out.println(“method of Class B”);
class C extends A
class D extends A
class JavaExample
B obj1=new B();
C obj2=new C();
D obj3=new D();
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;
String name;
super.name = “Dad”;
name = “Son”:
sobj.details();
Example:
class Animal{
void printColor(){
class TestSuper1{
c.printColor();
Output:
black
white
Example:
class Animal{
void eat(){
System.out.println(“eating…”);
void eat(){
System.out.println(“mewing…”);
void work(){
super.eat();
mew();
class TestSuper2{
c.work();
Output:
eating…
mewing…
Example:
class Animal{
Animal(){
System.out.println(“animal is created”);
Cat();
super();
System.out.println(“cat is created”);
class TestSuper3{
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.
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()”);
@Override
void show()
{
System.out.println(“Child’s show()”);
class Main
obj1.show();
//if a parent type reference refers to a child object Child’s show() is called run time
polymorphism.
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.
Program:
//A.java
package.pack;
public class A
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
A.obj=new A();
Obj.msg();
Program 2:
//Pclass.java
package pg;
public int x;
x=l;
return x;
//Pclass1.java
import com.pg.*;
}
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;
//save as B.java
package javatpoint;
The three ways to access the package from outside the package are:
1. import package.*;
2. import package.classname;
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:
Example:
interface Fee{
float fine();
class TestInterface2{
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{
}
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:
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.
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();
}
System.out.println(“drawing rectangle”);
System.out.println(“drawing circle”);
class TestInterface1
d.draw();
}
NESTED INTERFACE
interface interface_name {
…….
interface nested_interface_name {
…….
Example:
interface Showable{
void show();
interface Message {
void msg();
message.msg();
}
}
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();
interface B extends A
void meth3();
System.out.println(“Implements meth1().”);
System.out.println(“Implements meth2().”);
class IFExtend {
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();
void show();
System.out.println(“Hello”);
}
System.out.println(“Welcome”);
obj.print();
obj.show();
Output:
Hello
Welcome
interface Printable{
void print();
interface Showable{
void show();
System.out.println(“Hello”);
System.out.println(“Welcome”);
A7 obj=new A7();
obj.print();
obj.show();
Output:
Hello
Welcome
interface Drawable {
void draw();
return x*x*x;
class TestInterfaceStatic{
d.draw();
System.out.println(Drawable.cube(3));
Output:
drawing rectangle
27
interface Drawable{
void draw();
System.out.println(“default method”);
System.out.println(“drawing rectangle”);
class TestInterfaceDefault{
public static void main(String args[])
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
Signature:
WEEKLY PROGRESS REPORT
ON
Submitted to
Program: B.tech(CSE)
Semester: 5
Signature of Student:
Semester: 5
Signature of Student:
Semester: 5
Signature of Student:
Semester: 5
Signature of Student: