Multilevel Inheritance

 

class Animal {
void eat() {
System.out.println("An Animal is Created");
System.out.println("Animal eats");
}
}

class Dog extends Animal {

void sound() {
System.out.println("A dog is created");
System.out.println("A dog barks");
}
}

class BullDog extends Dog {
void height() {
System.out.println("A BullDog is created");
System.out.println("A BullDog has small Height");
}
}

public class TestMultiLevelInheritance {
public static void main(String args[]) {

BullDog bd = new BullDog();

bd.eat();
bd.sound();
bd.height();
}
}

Single level Inheritence

 

class Animal {
void eat() {
System.out.println("An Animal is Created");
System.out.println("Animal eats");
}
}

class Dog extends Animal {

void sound() {
System.out.println("A dog is created");
System.out.println("A dog barks");
}
}

public class TestSingleInheritance {
public static void main(String args[]) {
// eat method was not defined in Dog class
// so it was inherited from Animal class
// This is single level inheritance
Dog dog = new Dog();
dog.eat();
dog.sound();
}
}

Real time Use of "This" Keyword

 

class StudentData4{
    int rollNo;
String name;
String course;
float fee;
//constructor to assign value;
StudentData4(int rollNo, String name,String course){
this.rollNo = rollNo;
this.name = name;
this.course = course;
}
StudentData4(int rollNo, String name,String course,float fee){
//this will reuse the upper constructor i.e having three parameters
//this statement should be first statement of constructor
this(rollNo,name,course);
this.fee = fee;
}

void display(){
System.out.println("Roll no of student is :" + rollNo);
System.out.println("Name of student is :" + name);
System.out.println("course of Student is :" + course);
System.out.println("Fee of student is :" + fee);
}
}
public class RealUseOfThis{
public static void main(String args[]){
StudentData4 s1 = new StudentData4(123,"Kalam","LLB");
StudentData4 s2 = new StudentData4(345,"Kejriwal","B.TECH",75000);
System.out.println("---------------------------------------------------");
System.out.println("Details of First Student");
s1.display();//calling display method
System.out.println("---------------------------------------------------");
System.out.println("Details of Second Student");
s2.display();//calling display method
System.out.println("---------------------------------------------------");
}
}

Using THIS keyword


 class StudentData3{
int rollNo;
String name;
static String college = "VCE";
//constructor to assign value;
StudentData3(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}

void display(){
System.out.println("Roll no of student is :" + rollNo);
System.out.println("Name of student is :" + name);
System.out.println("Collge of student is :" + college);
}
}
public class TestStudent3{
public static void main(String args[]){
StudentData3 s1 = new StudentData3(123,"Niharika");
StudentData3 s2 = new StudentData3(345,"Kayna");
System.out.println("---------------------------------------------------");
System.out.println("Details of First Student");
s1.display();//calling display method
System.out.println("---------------------------------------------------");
System.out.println("Details of Second Student");
s2.display();//calling display method
System.out.println("---------------------------------------------------");
}
}

Printing data of an class object without THIS keyword


class StudentData2{
int rollNo;
String name;
static String college = "VCE";
//constructor to assign value;
StudentData2(int rollNo, String Name){
//As both variables (local and instance) are same
// zero and null value will be printed;
rollNo = rollNo;
name = name;
}

void display(){
System.out.println("Roll no of student is :" + rollNo);
System.out.println("Name of student is :" + name);
System.out.println("Collge of student is :" + college);
}
}
public class TestStudent2{
public static void main(String args[]){
StudentData2 s1 = new StudentData2(123,"Niharika");
StudentData2 s2 = new StudentData2(345,"Kayna");
System.out.println("---------------------------------------------------");
System.out.println("Details of First Student");
s1.display();//calling display method
System.out.println("---------------------------------------------------");
System.out.println("Details of Second Student");
s2.display();//calling display method
System.out.println("---------------------------------------------------");
}
}

Using Static method

 

class StudentData1{
int rollNo;
String name;
static String college = "VCE";
//static method to change the value of college
static void alter(){
college = "MDU"; //changing value of static varible
college = "BBPS";
}
//constructor to assign value;
StudentData1(int r, String s){
rollNo = r;
name = s;
}

void display(){
System.out.println("Roll no of student is :" + rollNo);
System.out.println("Name of student is :" + name);
System.out.println("Collge of student is :" + college);
}
}
public class TestStudent1{
public static void main(String args[]){
StudentData1 s1 = new StudentData1(123,"Niharika");
StudentData1 s2 = new StudentData1(345,"Kayna");
StudentData1.alter(); //Calling static method alter();
System.out.println("------------------------------------------------");
System.out.println("Details of First Student");
s1.display();//calling display method
System.out.println("------------------------------------------------");
System.out.println("Details of Second Student");
s2.display();//calling display method
System.out.println("------------------------------------------------");
}
}

Use of Static Variable with Class and Object


 class StudentData{
    int rollNo;
String name;
static String college = "VCE";

StudentData(int r, String s){
rollNo = r;
name = s;
}

void display(){
System.out.println("Roll no of student is :" + rollNo);
System.out.println("Name of student is :" + name);
System.out.println("Collge of student is :" + college);
}
}
public class TestStudent{
public static void main(String args[]){
StudentData s1 = new StudentData(123,"Niharika");
StudentData s2 = new StudentData(345,"Kayna");
System.out.println("---------------------------------------------------");
System.out.println("Details of First Student");
s1.display();
System.out.println("---------------------------------------------------");
System.out.println("Details of Second Student");
s2.display();
System.out.println("---------------------------------------------------");
}
}

Copying data of a object to another object without using Copy constructor


 public class Pupil1 {
int rollNo;
String name;
int age;

Pupil1(int r, String s, int a) {
rollNo = r;
name = s;
age = a;
}
Pupil1(){}


void display() {
System.out.println("Roll no of student is :" + rollNo);
System.out.println("Name of student is :" + name);
System.out.println("Age of student is :" + age);
}

public static void main(String args[]) {
Pupil1 p1 = new Pupil1(123, "Sudeep", 25);
Pupil1 p2 = new Pupil1();
p2.rollNo = p1.rollNo;
p2.name = p1.name;
p2.age = p1.age;
System.out.println("---------------------------------------------------");
System.out.println("Details of First Student");
p1.display();
System.out.println("---------------------------------------------------");
System.out.println("Details of Second Student copied from first Student");
System.out.println()
p2.display();
System.out.println("---------------------------------------------------");

}
}

Copy Constructor

 

public class Pupil {
int rollNo;
String name;
int age;

Pupil(int r, String s, int a) {
rollNo = r;
name = s;
age = a;
}

Pupil(Pupil p) {
rollNo = p.rollNo;
name = p.name;
age = p.age;
}

void display() {
System.out.println("Roll no of student is :" + rollNo);
System.out.println("Name of student is :" + name);
System.out.println("Age of student is :" + age);
}

public static void main(String args[]) {
Pupil p1 = new Pupil(123, "Sudeep", 25);
Pupil p2 = new Pupil(p1);
System.out.println("---------------------------------------------------");
System.out.println("Details of First Student");
p1.display();
System.out.println("---------------------------------------------------");
System.out.println("Details of Second Student copied from first Student");
p2.display();
System.out.println("---------------------------------------------------");

}
}

Constructor Overloading


 class Worker5 {
    int id;
String name;
int age;
//Constructor with two arguments
Worker5(int i, String n) {
System.out.println("Constructor with two parameter was invoked");
id = i;
name = n;
}
//constructor with three arguments
Worker5(int i, String n, int a) {
System.out.println("Constructor with three parameter was invoked");
id = i;
name = n;
age = a;
}

void display() {
System.out.println("Id of worker is :" + id);
System.out.println("Name of worker is :" + name);
System.out.println("Age of worker is :" + age);
}

public static void main(String args[]) {
Worker5 w1 = new Worker5(123, "John");
Worker5 w2 = new Worker5(198, "Mohmad", 25);

w1.display(); //age will be zero i.e default value
w2.display();
}
}

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...