0% found this document useful (0 votes)
54 views

Programming in Java

The document discusses inheritance in Java programming. It defines key inheritance concepts like subclass, superclass, and types of inheritance such as simple, multiple, multilevel, method overloading, and method overriding. It provides examples of inheritance code in Java. It also includes an example of using inheritance with interfaces in Java.

Uploaded by

Krishna Kishore
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Programming in Java

The document discusses inheritance in Java programming. It defines key inheritance concepts like subclass, superclass, and types of inheritance such as simple, multiple, multilevel, method overloading, and method overriding. It provides examples of inheritance code in Java. It also includes an example of using inheritance with interfaces in Java.

Uploaded by

Krishna Kishore
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAMMING IN JAVA

NAME: P. SANJAY
SECTION: K18NZ
ID No: 11809012
ROLL No: 27
QUESTION No: 01

SUBMITTED TO:
PUNEET KUMAR

INDEX
CONTENT
PAGE No.
INHERITANCE …….3
 SUBCLASS
 SUPERCLASS
 IMPORTANCE OF INHERITANCE

TYPES OF INHERITANCE .….3


-9
 SIMPLE INHERITANCE
 MULTIPLE INHERITANCE
 MULTILEVEL INHERITANCE
 METHOD OVERLOADING
 METHOD OVERRIDING
 JAVA PROGRAMME TO USE INHERITANCE WITH INTERFACE

INHERITANCE:
A mechanism in which one object acquires all the properties and behaviors of a
parent object is called “Inheritance”.
The idea behind Inheritance in java is that you can create new classes that are built
upon existing classes

SUBCLASS:
A class that is derived from another class is called Subclass. It is also called as
derived class, extended class, or child class.

SUPER CLASS:
The class from which the subclass is derived is called a Super class. It is also called
as a base class or a parent class.

IMPORTANTS OF INHERITANCE:
We use Inheritance of java in following reasons-
1. Encapsulation: If we have some common attributes, then we encapsulate
these in a parent class and just give specific attributes to child classes.
2. Polymorphism: We can also use Polymorphism with Inheritance in Java.
So that based on the type of Child class, same class behaves differently.
3. Code Reusability: Using Inheritance helps in reusing code.
4. Efficiency: It is efficient to use Inheritance while writing code. This can
increase the speed of project.

TYPES OF INHERITANCE:
There are 5 types of Inheritance in java
 Simple inheritance
 Multiple inheritance
 Multilevel inheritance
 Method overloading
 Method overriding.

SIMPLE INHERITANCE:
When a subclass is derived simply from its parent class then this mechanism is
known as Simple inheritance
In case of simple inheritance there is only a sub class and its parent class. It is also
called Single Inheritance or one level inheritance
SYNTAX:
Class sub_class_name extends super_class
{ //body of the subclass }
PROGRAM:
Class A
{ int I, j;
Void showij()
{ System.out.print(“I and j:”+i+””+j);
} }
Class B extends A
{ int k;
Void showk()
{ System.out.println(“k:”+k); }
Void sum()
{ System.out.println(“i+j+k:”+(i+j+k);) }
Public static void main(String args[])
{ B r=new B();
r.sum();
}

MULTIPLE INHERITANCE:
The mechanism of inheriting the features of more than one base class into a single
class is known as multiple inheritance
It allows creating several methods with the same name which differ from each
other in the type of the input and the output of the method
PROGRAM:
// First Parent class
class Parent1
{
    void fun()
    {
        System.out.println("Parent1");
    }
}
  
// Second Parent Class
class Parent2
{
    void fun()
    {
        System.out.println("Parent2");
    }
}
  
// Error : Test is inheriting from multiple
// classes
class Test extends Parent1, Parent2
{
   public static void main (String args[])
   {
       Test t = new Test ();
       t.fun(); } }
MULTILEVEL INHERITANCE:
It is the enhancement of the concept of inheritance of the concept of inheritance.
When a subclass is derived from a derived class then this mechanism is known as
the multilevel inheritance.
The derived class is called the subclass for its parent class and this parent class
works as the child class for its parent class
PROGRAM:
{ 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(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
} }

METHOD OVERLOADING:
Method overloading means having two or more methods with the same name but
different signatures in the same scope.
It allows creating several methods with the same name which differ from each
other in the type of the input and the output of the method.
SYNTAX:
public void add(int i, int j) {
}
public void add(int i) {
}
PROGRAM:
class Multiply {
void mul(int a, int b) {
System.out.println("Sum of two=" + (a * b));
} void mul(int a, int b, int c) {
System.out.println("Sum of three=" + (a * b * c));
}
}
public class Polymorphism {
public static void main(String args[]) {
Multiply m = new Multiply();
m.mul(6, 10);
m.mul(10, 6, 5);
} }

METHOD OVERRIDING:
Method overriding means having a different implementation of the same method in
the inherited class
These two methods would have the same signature, but different implementation.
One of these would exist in the base class and another in the derived class. These
cannot exist in the same class.
PROGRAM:
class ABC{
public void disp()
{ System.out.println("disp() method of parent class"); } }
class Demo extends ABC{
public void disp(){
System.out.println("disp() method of Child class"); }
public void newMethod(){
System.out.println("new method of child class"); }
public static void main( String args[]) {
ABC obj = new ABC();
obj.disp();
ABC obj2 = new Demo();
obj2.disp();
}
}

Java program to use inheritance with interfaces:


import java.io.*;
interface intfA
{
void geekName(); }
interface intfB extends intfA
{
void geekInstitute(); }
// class implements both interfaces and provides implementation to the method.
class sample implements intfB
{ public void geekName()
{
System.out.println("Rohit"); }
public void geekInstitute()
{ System.out.println("JIIT"); }
public static void main (String[] args)
{ sample ob1 = new sample();
// calling the method implemented within the class.
ob1.geekName();
ob1.geekInstitute();
} }

You might also like