Inheritance basic program
1:-
class Demo
{
void animal ()
{
System.out.println("it is parent class");
}
}
class childDemo extends Demo{
void Dog()
{
System.out.println("this is a child class");
}
public static void main(String arg[])
{
Demo s1= new Demo();
s1.animal();
Ager mai dog method ko call kar to yah pe error aa jata kuki me Demo class ka
obj create kiya hai
}
}
C:\java>javac Testing.java
C:\java>java childDemo
it is parent class
C:\java>
2:-
class Demo
{
void animal ()
{
System.out.println("it is parent class");
}
}
class childDemo extends Demo{
void Dog()
{
System.out.println("this is a child class");
}
public static void main(String arg[])
{
childDemo s1= new childDemo();
s1.animal();
s1.Dog();
// yha pe error nahi aya hai ki childDemo class ke obj create kiya the
or usko inherate kiya hua hai
}
}
C:\java>javac Testing.java
C:\java>java childDemo
it is parent class
this is a child class
C:\java>
3:_
class Demo
{
void animal ()
{
System.out.println("it is parent class");
}
}
class childDemo {
void Dog()
{
System.out.println("this is a child class");
}
public static void main(String arg[])
{
childDemo s1= new childDemo();
// s1.animal();
s1.Dog();
}
}
C:\java>javac Testing.java
C:\java>java childDemo
this is a child class
C:\java>
yha pe error nahi aya kuki childclass ke obj banaya or or usi class ke ander ke method call kiya
3:-
class Demo
{
void animal ()
{
System.out.println("it is parent class");
}
}
class childDemo {
void Dog()
{
System.out.println("this is a child class");
}
public static void main(String arg[])
{
Demo s1= new Demo();
s1.animal();
//s1.Dog();
}
}
C:\java>javac Testing.java
C:\java>java childDemo
it is parent class
C:\java>
yha pe error nahi aya kuki childclass ke obj banaya or or usi class ke ander ke method call kiya
4:-
class Demo
{
static void method ()
{
System.out.println("hello world");
}
}
class Demo2 extends Demo{
void mehtod2 ()
{
System.out.println(" hello2");
}
public static void main(String args[])
{
Demo2.method(); // is program me without obj create parent class me method
call liya hua hai
}
}
C:\java>javac Testing.java
C:\java>java Demo2
hello world
C:\java>
5:-
class Demo
{
static void method ()
{
System.out.println("hello world");
}
}
class Demo2 extends Demo{
void mehtod2 ()
{
System.out.println(" hello2");
}
public static void main(String args[])
{
Demo.method();
}
}
Output
C:\java>javac Testing.java
C:\java>java Demo2
hello world
C:\java>
6:-
class Demo
{
static void method ()
{
System.out.println("hello world");
}
}
class Demo2 extends Demo{
static void method2 ()
{
System.out.println(" hello2");
}
public static void main(String args[])
{
Demo.method(); //yha pe first function ko call kiya hua hai
Demo2.method2(); // secend function ko call kiya hua hai
}
}
C:\java>javac Testing.java
C:\java>java Demo2
hello world
hello2
C:\java>
6:-
class Demo
{
static void method ()
{
System.out.println("hello world");
}
}
class Demo2 { //yha pe inherate ke jarural hi nahi pari
kuki donal class ko call kiya hua hia or method bhi
static void method2 ()
{
System.out.println(" hello2");
}
public static void main(String args[])
{
Demo.method();
Demo2.method2();
}
}
C:\java>javac Testing.java
C:\java>java Demo2
hello world
hello2
C:\java>
7:-
class Demo
{
static void method ()
{
System.out.println("hello world");
}
}
class Demo2 extends Demo {
static void method2 ()
{
System.out.println(" hello2");
}
public static void main(String args[])
{
Demo2.method();
Demo2.method2();
}
}
C:\java>javac Testing.java
C:\java>java Demo2
hello world
hello2
C:\java>
3:-
class Hello
{
// Declare an instance block.
{
show();
}
Hello() // constructor
{
System.out.println("Hello constructor");
show();
}
void show()
{
System.out.println("Hello method");
}
}
class mytest{
public static void main(String args[])
{
Hello obj = new Hello();
obj.show();
}
}
OUTPUT
// C:\java>javac Testing.java
// C:\java>java mytest
// Hello method
// Hello constructor
// Hello method
// Hello method
4:-
class Hello
{
// Declare an instance block.
{
show();
}
Hello() //constructor
{
System.out.println("Hello constructor");
show(); //method
}
void show() //method
{
System.out.println("Hello method");
}
}
class Hi extends Hello
{
Hi() //constructor
{
System.out.println("Hi constructor");
}
void show() { // Override the show() method.
System.out.println("Hi method");
}
}
class TestHelloHi extends Hi
{
public static void main(String[] args)
{
Hi obj = new Hi();
obj.show(); // show() method of Hi class is called.
}
}
OUTPUT
C:\java>javac Testing.java
C:\java>java TestHelloHi
Hi method
Hello constructor
Hi method
Hi constructor
Hi method
5:-
class Hello
{
// Declare an instance block.
{
show();
}
Hello() //constructor
{
System.out.println("Hello constructor");
show(); //method
}
void show() //method
{
System.out.println("Hello method");
}
}
class Hi extends Hello
{
Hi() //constructor
{
System.out.println("Hi constructor");
}
void show() { // Override the show() method.
System.out.println("Hi method");
}
}
class TestHelloHi extends Hi
{
public static void main(String[] args)
{
// Superclass reference is equal to child class object.
Hello obj1 = new Hi();
obj1.show();
}
}
//OUTPUT
C:\java>javac Testing.java
C:\java>java TestHelloHi
Hi method
Hello constructor
Hi method
Hi constructor
Hi method
C:\java>
6:-
8:-
Comments
Post a Comment