SlideShare a Scribd company logo
Inheritance
• Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).
used in Inheritance:
• Class: A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived
class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also
called a base class or a parent class.
• Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields
and methods of the existing class when you create a new class.
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Inheritance in java computer programming app
Single Inheritance Example
TestInheritance.java
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Output:
barking... eating.
Multilevel Inheritance
File: TestInheritance2.java
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
• weeping... barking... eating...
Hierarchical Inheritance
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing... eating..
#include <iostream>
using namespace std;
// Base class 1
class Base1
{
public:
void show() {
cout << "Base1::show()" << endl;
}
};
// Base class 2
class Base2
{
public:
void display()
{ cout << "Base2::display()" << endl;
}};
// Derived class
class Derived : public Base1, public Base2
{
};
int main()
{
Derived d;
d.show(); // Access method from Base1
d.display(); // Access method from Base2
return 0;
}
// Class 3
// Trying to be child of both the classes
class Test extends Parent1,Parent2 {
// Main driver method
public static void main(String args[]) {
// Creating object of class in main() method
Test t = new Test();
// Trying to call above functions of class
where
// Error is thrown as this class is inheriting
// multiple classes
t.fun();
}
}
import java.io.*;
class Parent1 {
void fun() {
System.out.println("Parent1");
}
}
class Parent2 {
void fun() {
// Print statement if this method is called
System.out.println("Parent2");
}
}
Java doesn’t support Multiple Inheritance
Java Polymorphism
1. Method Overloading
2. Method Overriding
• If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.
There are two ways to overload the method in java
• By changing number of arguments
• By changing the data type
Method Overloading: changing no. of arguments
class Adder
{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1
{
public static void main(String[] args)
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}
Output:
• 22
• 33
Method Overloading: changing data type of arguments
class Adder
{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class TestOverloading2
{
public static void main(String[] args)
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
Output:
22 24.9
Method Overriding
If subclass (child class) has the same method as declared in the parent
class, it is known as method overriding .
Usage of Method Overriding:
• Method overriding is used to provide the specific implementation of a
method which is already provided by its superclass.
• Method overriding is used for runtime polymorphism.
Rules for Java Method Overriding
• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
• There must be an IS-A relationship (inheritance).
class Vehicle
{
//defining a method
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike2 extends Vehicle
{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}
}
Output:
Bike is running safely
class Bank{
int getRateOfInterest(){return 0;}
}
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
• Output: SBI Rate of Interest: 8 ICICI Rate of Interest: 7 AXIS Rate of Interest: 9
Encapsulation in Java
• Encapsulation in Java is a process of wrapping code and data
together into a single unit, for example, a capsule which is mixed of
several medicines.
• We can create a fully encapsulated class in Java by making all the data
members of the class private. Now we can use setter and getter
methods to set and get the data in it.
• The Java Bean class is the example of a fully encapsulated class.
Advantage of Encapsulation:
• By providing only a setter or getter method, you can make the class read-only or
write-only.
• It provides you the control over the data. Suppose you want to set the value of id
which should be greater than 100 only, you can write the logic inside the setter
method. You can write the logic not to store the negative numbers in the setter
methods.
• It is a way to achieve data hiding in Java because other class will not be able to
access the data through the private data members.
• The encapsulate class is easy to test. So, it is better for unit testing.
• The standard IDE's (integrated development environment) are providing the facility
to generate the getters and setters. So, it is easy and fast to create an encapsulated
class in Java.
// set method for age to access
// private variable geekage
public void setAge(int newAge)
{ geekAge = newAge; }
// set method for name to access
// private variable geekName
public void setName(String newName)
{
geekName = newName;
}
// set method for roll to access
// private variable geekRoll
public void setRoll(int newRoll)
{ geekRoll = newRoll; }
}
class Encapsulate {
// private variables declared
// these can only be accessed by
// public methods of class
private String geekName;
private int geekRoll;
private int geekAge;
// get method for age to access
// private variable geekAge
public int getAge() { return geekAge; }
// get method for name to access
// private variable geekName
public String getName() { return geekName; }
// get method for roll to access
// private variable geekRoll
public int getRoll() { return geekRoll; }
// Class to access variables
// of the class Encapsulate
public class Test {
public static void main(String[]
args)
{
Encapsulate obj = new
Encapsulate();
// setting values of the variables
obj.setName("Harsh");
obj.setAge(19);
obj.setRoll(51);
// Displaying values of the variables
System.out.println("Geek's name: " +
obj.getName());
System.out.println("Geek's age: " +
obj.getAge());
System.out.println("Geek's roll: " +
obj.getRoll());
// Direct access of geekRoll is not possible
// due to encapsulation
// System.out.println(&quot;Geek's roll:
&quot; +
// obj.geekName);
}
}
Package
• A java package is a group of similar types of classes, interfaces and sub-packages.
• Package in java can be categorized in two form, built-in package and user-defined
package.
• There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Advantage of Java Package
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
Inheritance in java computer programming app
//save as Simple.java
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Create the folder mypack
Inside mypack save the program file: File name is Simple
To access package from another package
There are three ways to access the package from outside the package.
• import package.*;
• import package.classname;
• fully qualified name.
Using packagename.*
• If you use package.* then all the classes and interfaces of this package
will be accessible but not subpackages.
• The import keyword is used to make the classes and interface of
another package accessible to the current package.
import package.*;
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
import package.classname;
package pkg;
public class AA
{
public void msg()
{ System.out.println("Hello")
;
}
}
import pkg.AA;
class BB{
public static void main(String args[]){
AA obj = new AA();
obj.msg();
}
}
fully qualified name
package pkg1;
public class AAA
{
public void msg()
{
System.out.println("Hello");
}
}
import pkg1.*;
class BBB{
public static void main(String args[]){
pkg1.AAA obj = new pkg1.AAA();//using
fully qualified name
obj.msg();
}
}
PACKAGE 1:
package student;
public class stud
{
int regno;
String name;
public stud(int r,String n)
{
regno=r;
name=n;
}
public void print()
{
System.out.println("Regino="+regno);
System.out.println("Name="+name);
}
}
package diploma;
public class diplo
{
int regno;
String disp;
String year;
public diplo(String d,String y)
{
disp=d;
year=y;
}
public void print()
{
System.out.println("course="+disp);
System.out.println("year="+year);
}
}
PACKAGE 2:
IMPORT PACKAGE
import student.stud;
import diploma.diplo;
class clg
{
public static void main(String args[])
{
stud s=new stud(111,"Anandh");
diplo d=new diplo(“CSE","IIId year");
s.print();
d.print();
}
}

More Related Content

Similar to Inheritance in java computer programming app (20)

Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
Ye Win
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptx
ShuvrojitMajumder
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in Java is a mechanism in which one object acquires all the prope...Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
COMSATS Institute of Information Technology
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Basics to java programming and concepts of java
Basics to java programming and concepts of javaBasics to java programming and concepts of java
Basics to java programming and concepts of java
1747503gunavardhanre
 
Chapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptxChapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptx
fikadumeuedu
 
object oriented programming unit two ppt
object oriented programming unit two pptobject oriented programming unit two ppt
object oriented programming unit two ppt
isiagnel2
 
Modules 333333333³3444444444444444444.pptx
Modules 333333333³3444444444444444444.pptxModules 333333333³3444444444444444444.pptx
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Lectupopplkmkmkkpompom-0ookoimmire 2.pdf
Lectupopplkmkmkkpompom-0ookoimmire 2.pdfLectupopplkmkmkkpompom-0ookoimmire 2.pdf
Lectupopplkmkmkkpompom-0ookoimmire 2.pdf
rtreduanur247
 
Java_notes.ppt
Java_notes.pptJava_notes.ppt
Java_notes.ppt
tuyambazejeanclaude
 
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
ShuvrojitMajumder
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptxInheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
Abdii Rashid
 
Java PPT
Java PPTJava PPT
Java PPT
Dilip Kr. Jangir
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
Ye Win
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptx
ShuvrojitMajumder
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in Java is a mechanism in which one object acquires all the prope...Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Basics to java programming and concepts of java
Basics to java programming and concepts of javaBasics to java programming and concepts of java
Basics to java programming and concepts of java
1747503gunavardhanre
 
Chapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptxChapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptx
fikadumeuedu
 
object oriented programming unit two ppt
object oriented programming unit two pptobject oriented programming unit two ppt
object oriented programming unit two ppt
isiagnel2
 
Modules 333333333³3444444444444444444.pptx
Modules 333333333³3444444444444444444.pptxModules 333333333³3444444444444444444.pptx
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Lectupopplkmkmkkpompom-0ookoimmire 2.pdf
Lectupopplkmkmkkpompom-0ookoimmire 2.pdfLectupopplkmkmkkpompom-0ookoimmire 2.pdf
Lectupopplkmkmkkpompom-0ookoimmire 2.pdf
rtreduanur247
 
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
ShuvrojitMajumder
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptxInheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 

Recently uploaded (20)

SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Introduction to AI agent development with MCP
Introduction to AI agent development with MCPIntroduction to AI agent development with MCP
Introduction to AI agent development with MCP
Dori Waldman
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)
kjim477n
 
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptxSemi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
studyshubham18
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptxFINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
The first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptxThe first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptx
Mayank Mathur
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
ijfcstjournal
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSWIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Introduction to AI agent development with MCP
Introduction to AI agent development with MCPIntroduction to AI agent development with MCP
Introduction to AI agent development with MCP
Dori Waldman
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagementoperationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)11th International Conference on Data Mining (DaMi 2025)
11th International Conference on Data Mining (DaMi 2025)
kjim477n
 
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptxSemi-Conductor ppt ShubhamSeSemi-Con.pptx
Semi-Conductor ppt ShubhamSeSemi-Con.pptx
studyshubham18
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptxFINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
The first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptxThe first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptx
Mayank Mathur
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
A DECISION SUPPORT SYSTEM FOR ESTIMATING COST OF SOFTWARE PROJECTS USING A HY...
ijfcstjournal
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSWIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior DevelopersDavid Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Ad

Inheritance in java computer programming app

  • 2. • Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). used in Inheritance: • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. The syntax of Java Inheritance class Subclass-name extends Superclass-name { //methods and fields }
  • 4. Single Inheritance Example TestInheritance.java class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class TestInheritance{ public static void main(String args[]){ Dog d=new Dog(); d.bark(); d.eat(); }} Output: barking... eating.
  • 5. Multilevel Inheritance File: TestInheritance2.java class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class BabyDog extends Dog{ void weep(){System.out.println("weeping...");} } class TestInheritance2{ public static void main(String args[]){ BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }} Output: • weeping... barking... eating...
  • 6. Hierarchical Inheritance class Animal{ void eat(){System.out.println("eating...");} } class Dog extends Animal{ void bark(){System.out.println("barking...");} } class Cat extends Animal{ void meow(){System.out.println("meowing...");} } class TestInheritance3{ public static void main(String args[]){ Cat c=new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error }} Output: meowing... eating..
  • 7. #include <iostream> using namespace std; // Base class 1 class Base1 { public: void show() { cout << "Base1::show()" << endl; } }; // Base class 2 class Base2 { public: void display() { cout << "Base2::display()" << endl; }}; // Derived class class Derived : public Base1, public Base2 { }; int main() { Derived d; d.show(); // Access method from Base1 d.display(); // Access method from Base2 return 0; }
  • 8. // Class 3 // Trying to be child of both the classes class Test extends Parent1,Parent2 { // Main driver method public static void main(String args[]) { // Creating object of class in main() method Test t = new Test(); // Trying to call above functions of class where // Error is thrown as this class is inheriting // multiple classes t.fun(); } } import java.io.*; class Parent1 { void fun() { System.out.println("Parent1"); } } class Parent2 { void fun() { // Print statement if this method is called System.out.println("Parent2"); } } Java doesn’t support Multiple Inheritance
  • 9. Java Polymorphism 1. Method Overloading 2. Method Overriding • If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. There are two ways to overload the method in java • By changing number of arguments • By changing the data type
  • 10. Method Overloading: changing no. of arguments class Adder { static int add(int a,int b){return a+b;} static int add(int a,int b,int c){return a+b+c;} } class TestOverloading1 { public static void main(String[] args) { System.out.println(Adder.add(11,11)); System.out.println(Adder.add(11,11,11)); } } Output: • 22 • 33
  • 11. Method Overloading: changing data type of arguments class Adder { static int add(int a, int b){return a+b;} static double add(double a, double b){return a+b;} } class TestOverloading2 { public static void main(String[] args) { System.out.println(Adder.add(11,11)); System.out.println(Adder.add(12.3,12.6)); }} Output: 22 24.9
  • 12. Method Overriding If subclass (child class) has the same method as declared in the parent class, it is known as method overriding . Usage of Method Overriding: • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass. • Method overriding is used for runtime polymorphism. Rules for Java Method Overriding • The method must have the same name as in the parent class • The method must have the same parameter as in the parent class. • There must be an IS-A relationship (inheritance).
  • 13. class Vehicle { //defining a method void run(){System.out.println("Vehicle is running");} } //Creating a child class class Bike2 extends Vehicle { //defining the same method as in the parent class void run(){System.out.println("Bike is running safely"); } public static void main(String args[]) { Bike2 obj = new Bike2();//creating object obj.run();//calling method } } Output: Bike is running safely
  • 14. class Bank{ int getRateOfInterest(){return 0;} } class SBI extends Bank{ int getRateOfInterest(){return 8;} } class ICICI extends Bank{ int getRateOfInterest(){return 7;} } class AXIS extends Bank{ int getRateOfInterest(){return 9;} } class Test2{ public static void main(String args[]){ SBI s=new SBI(); ICICI i=new ICICI(); AXIS a=new AXIS(); System.out.println("SBI Rate of Interest: "+s.getRateOfInterest()); System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest()); System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest()); } } • Output: SBI Rate of Interest: 8 ICICI Rate of Interest: 7 AXIS Rate of Interest: 9
  • 15. Encapsulation in Java • Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines. • We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it. • The Java Bean class is the example of a fully encapsulated class.
  • 16. Advantage of Encapsulation: • By providing only a setter or getter method, you can make the class read-only or write-only. • It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods. • It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members. • The encapsulate class is easy to test. So, it is better for unit testing. • The standard IDE's (integrated development environment) are providing the facility to generate the getters and setters. So, it is easy and fast to create an encapsulated class in Java.
  • 17. // set method for age to access // private variable geekage public void setAge(int newAge) { geekAge = newAge; } // set method for name to access // private variable geekName public void setName(String newName) { geekName = newName; } // set method for roll to access // private variable geekRoll public void setRoll(int newRoll) { geekRoll = newRoll; } } class Encapsulate { // private variables declared // these can only be accessed by // public methods of class private String geekName; private int geekRoll; private int geekAge; // get method for age to access // private variable geekAge public int getAge() { return geekAge; } // get method for name to access // private variable geekName public String getName() { return geekName; } // get method for roll to access // private variable geekRoll public int getRoll() { return geekRoll; }
  • 18. // Class to access variables // of the class Encapsulate public class Test { public static void main(String[] args) { Encapsulate obj = new Encapsulate(); // setting values of the variables obj.setName("Harsh"); obj.setAge(19); obj.setRoll(51); // Displaying values of the variables System.out.println("Geek's name: " + obj.getName()); System.out.println("Geek's age: " + obj.getAge()); System.out.println("Geek's roll: " + obj.getRoll()); // Direct access of geekRoll is not possible // due to encapsulation // System.out.println(&quot;Geek's roll: &quot; + // obj.geekName); } }
  • 19. Package • A java package is a group of similar types of classes, interfaces and sub-packages. • Package in java can be categorized in two form, built-in package and user-defined package. • There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Advantage of Java Package 1) Java package is used to categorize the classes and interfaces so that they can be easily maintained. 2) Java package provides access protection. 3) Java package removes naming collision.
  • 21. //save as Simple.java package mypack; public class Simple{ public static void main(String args[]){ System.out.println("Welcome to package"); } } Create the folder mypack Inside mypack save the program file: File name is Simple
  • 22. To access package from another package There are three ways to access the package from outside the package. • import package.*; • import package.classname; • fully qualified name. Using packagename.* • If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. • The import keyword is used to make the classes and interface of another package accessible to the current package.
  • 23. import package.*; package pack; public class A { public void msg() { System.out.println("Hello"); } } import pack.*; class B { public static void main(String args[]) { A obj = new A(); obj.msg(); } }
  • 24. import package.classname; package pkg; public class AA { public void msg() { System.out.println("Hello") ; } } import pkg.AA; class BB{ public static void main(String args[]){ AA obj = new AA(); obj.msg(); } }
  • 25. fully qualified name package pkg1; public class AAA { public void msg() { System.out.println("Hello"); } } import pkg1.*; class BBB{ public static void main(String args[]){ pkg1.AAA obj = new pkg1.AAA();//using fully qualified name obj.msg(); } }
  • 26. PACKAGE 1: package student; public class stud { int regno; String name; public stud(int r,String n) { regno=r; name=n; } public void print() { System.out.println("Regino="+regno); System.out.println("Name="+name); } } package diploma; public class diplo { int regno; String disp; String year; public diplo(String d,String y) { disp=d; year=y; } public void print() { System.out.println("course="+disp); System.out.println("year="+year); } } PACKAGE 2:
  • 27. IMPORT PACKAGE import student.stud; import diploma.diplo; class clg { public static void main(String args[]) { stud s=new stud(111,"Anandh"); diplo d=new diplo(“CSE","IIId year"); s.print(); d.print(); } }