java (day 17)practical notes
overriding
package com.company;
class A{
public int a;
public int harry(){
return 4;
}
public void meth2(){
System.out.println("I am method 2 of class A");
}
}
class B extends A{
@Override
public void meth2(){
System.out.println("I am method 2 of class B");
}
public void meth3(){
System.out.println("I am method 3 of class B");
}
}
public class cwh_chapter10_method_overriding {
public static void main(String[] args) {
A a = new A();
a.meth2();
B b = new B();
b.meth2();
}
}
this-super keyword
package com.company;
import javax.print.Doc;
class EkClass{
int a;
public int getA() {
return a;
}
EkClass(int a){
this.a = a;
}
public int returnone(){
return 1;
}
}
class DoClass extends EkClass{
DoClass(int c){
super(c);
System.out.println("I am a constructor");
}
}
public class cwh_chapter10_this_super {
public static void main(String[] args) {
EkClass e = new EkClass(65);
DoClass d = new DoClass(5);
System.out.println(e.getA());
}
}constructor-inheritance
package com.company;
class Base1{
Base1(){
System.out.println("I am a constructor");
}
Base1(int x){
System.out.println("I am an overloaded constructor with value of x as: " + x);
}
}
class Derived1 extends Base1{
Derived1(){
//super(0);
System.out.println("I am a derived class constructor");
}
Derived1(int x, int y){
super(x);
System.out.println("I am an overloaded constructor of Derived with value of y as: " + y);
}
}
class ChildOfDerived extends Derived1{
ChildOfDerived(){
System.out.println("I am a child of derived constructor");
}
ChildOfDerived(int x, int y, int z){
super(x, y);
System.out.println("I am an overloaded constructor of Derived with value of z as: " + z);
}
}
public class cwh_chapter10_constructor_inheritance {
public static void main(String[] args) {
// Base1 b = new Base1();
// Derived1 d = new Derived1();
// Derived1 d = new Derived1(14, 9);
// ChildOfDerived cd = new ChildOfDerived();
ChildOfDerived cd = new ChildOfDerived(12, 13, 15);
}
}inheritance
//check constructor repeat******************
//inheritance-------------child class borrowed from parent class for code reusability,for carry forwrd
package com.company;
class Base{
public int x;
public int getX() {//getter
return x;
}
public void setX(int x) {//setter (ctrl + insert )
System.out.println("I am in base and setting x now");
this.x = x;
}
public void printMe(){
System.out.println("I am a constructor");
}
}
class Derived extends Base{//inheritance used extends keyword(extend class BASE) (NEW CLASS DERIVED)
public int y;
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
public class cwh_chapter10_inheritance {
public static void main(String[] args) {
// Creating an Object of base class
Base b = new Base();
b.setX(4);
System.out.println(b.getX());
// Creating an object of derived class
Derived d = new Derived();
d.setY(43);
System.out.println(d.getY());
}
}
constructorpackage com.company; class MyMainEmployee{ private int id; private String name; public MyMainEmployee(){ id = 0; name = "Your-Name-Here"; } public MyMainEmployee(String myName, int myId){ id = myId; name = myName; } public MyMainEmployee(String myName){ id = 1; name = myName; } public String getName(){ return name; } public void setName(String n){ this.name = n; } public void setId(int i){ this.id = i; } public int getId(){ return id; } } public class cwh_42_constructors { public static void main(String[] args) { //MyMainEmployee harry = new MyMainEmployee("ProgrammingWithHarry", 12); MyMainEmployee harry = new MyMainEmployee(); //harry.setName("CodeWithHarry"); //harry.setId(34); System.out.println(harry.getId()); System.out.println(harry.getName()); } }
acessmodifier-getter-setterpackage com.company; class MyEmployee{ private int id; private String name; public String getName(){ return name; } public void setName(String n){ this.name = n; } public void setId(int i){ this.id = i; } public int getId(){ return id; } } public class cwh_40_ch9 { public static void main(String[] args) { MyEmployee harry = new MyEmployee(); // harry.id = 45; // harry.name = "CodeWithHarry"; --> Throws an error due to private access modifier harry.setName("CodeWithHarry"); System.out.println(harry.getName()); harry.setId(234); System.out.println(harry.getId()); } }oops(create own java class)package com.company; class Employee{ int id; int salary; String name; public void printDetails(){ System.out.println("My id is " + id); System.out.println("and my name is "+ name); } public int getSalary(){ return salary; } } public class cwh_38_custom_class { public static void main(String[] args) { System.out.println("This is our custom class"); Employee harry = new Employee(); // Instantiating a new Employee Object Employee john = new Employee(); // Instantiating a new Employee Object // Setting Attributes for Harry harry.id = 12; harry.salary = 34; harry.name = "CodeWithHarry"; // Setting Attributes for John john.id = 17; john.salary = 12; john.name = "John Khandelwal"; // Printing the Attributes harry.printDetails(); john.printDetails(); int salary = john.getSalary(); System.out.println(salary); // System.out.println(harry.id); // System.out.println(harry.name); } }abstruct classpackage com.company;
abstract class Parents2{
public Parents2(){
System.out.println("this is constructor");
}
public void dayHello(){
System.out.println("hello");
}
abstract public void greet();
abstract public void greet2();
}
class Child2 extends Parents2{
@Override
public void greet(){
System.out.println("good morning");
}@Override
public void greet2(){
System.out.println("good afternoon");
}
}
abstract class Child3 extends Parents2{
//Class 'Child3' must either
// be declared abstract or implement abstract method 'greet()' in 'Parents2'
public void th(){
System.out.println("i am good");
}
}
public class cwh_chapter11_abstruct_class {
public static void main(String[] args) {
//Parents2 p=new Parents2() {};---------------error
//reason //Class 'Anonymous class derived from Parents2' must either be declared
// abstract or implement abstract method 'greet()' in 'Parents2'
Child2 c=new Child2();
//Child3 c3 =new Child3() {---------------error
/*
@Overridex
public void greet() {
}
@Override
public void greet2() {
}
}
*/
}
}
Comments
Post a Comment