code Dumping site

 Practicing inheritance by the example of class name phone and extends the phone class in smartphone.

Basically we are not making the phone with is already made by other we are updating the phone into smartphone.


package com.ritesh;
class phone1{
public void ring(){
System.out.println("The phone is ringing");
}
void call(){
System.out.println("Phone is calling ram");
}
public void sms(){
System.out.println("Phone is sending txt to hari");
}

static class smart_phone extends phone1{
public void play_song(){
System.out.println("Playing a song");
}
public int add(int a, int b){
return a+b;
}
public void youtube(){
System.out.println("Playing youtube");
}
}


}

public class phone {
public static void main(String[] args) {
phone1.smart_phone ph=new phone1.smart_phone();
ph.ring();
ph.call();
ph.sms();
ph.play_song();
System.out.println( ph.add(3,6));
ph.youtube();
}
}

 Constructor in Inheritance


package com.ritesh;

class base1{
public base1(){
System.out.println("I am a base constructor");
}
base1 (int a){

System.out.println("I return the value of a to be " + a);
}
static class derived extends base1{
derived(){
super(10);
System.out.println("I am the constructor of derived class");
}
}
}


public class inheritance_type1 {
public static void main(String[] args) {
//base1 sc=new base1();
base1.derived sc2=new base1.derived();



}
}


Practicing The Constructor in Inheritance


package com.ritesh;
class base2{
base2(){

System.out.println("I am a base class constructor");
}
base2(int x){
System.out.println("I return the value of x to be "+x);
}
base2(int x, int y){
System.out.println(" I return the value of x and y to be "+ x +" and "+ y);
}

static class derived extends base2{
derived(){
super(10,20);
System.out.println("I am a derived class constructor");
}
derived(int x){
super(4);
System.out.println("I return the value of a to be "+ x);
}
}
static class filetype extends derived{
filetype(){
super(10);
System.out.println("I am a filetype constructor");
}
filetype(int x){
System.out.println("I am a filetype constructor And i return the value of a "+ x);

}
}

}

public class type {
public static void main(String[] args) {
// base2 sc=new base2();
// base2.derived sc2=new base2.derived();
base2.derived.filetype s3=new base2.filetype(10);




}
}


Practicing method overriding

creating a class name human and extend the class in boy and girl and giving some similar properties and giving some special properties for only boy or girl should have.


package com.ritesh;

import java.util.Scanner;

class human{ //parent class human
public void walk(){
System.out.println("Human can walk");
}
public String name(String name){
System.out.println("Your name is "+name);
return name;
}

public void eat(){
//this method is for all class.
System.out.println("human can eat");
}
public byte age(byte age){
//age for all
System.out.println("your age is "+age);
return age;
}
public void height(){ //same for all class.
System.out.println("Enter the height in meter");
Scanner sc=new Scanner(System.in);
long height = sc.nextLong();
if(height <=4&& height >=2){
System.out.println("You are so short");
}
else if(height <=6&& height >=4){
System.out.println("You are medium");
}
else if(height <=8&& height >=6){
System.out.println("You are tall");
}
else if(height <=12&& height >=8){
System.out.println("You are extra tall");
}
else{
System.out.println("No more height is avilable");
}



}

public static class boy extends human{ //extend class boy
@Override
public void walk(){
System.out.println("boy can walk");
}

@Override
public String name(String name){
System.out.println("Your name is "+name);
return name;
}

}
static class girl extends boy{ //extend class girl
public void LongHair(){
System.out.println("Girl has long hair");
}

}


}

public class Main {

public static void main(String[] args) {
human s1=new human();
s1.walk();


human.boy s2=new human.boy();
s2.name("Ritesh");

human.girl s3=new human.girl();
s3.LongHair();
s3.height();

s1.eat();


}
}




Comments