Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Java Return Methods-------------
- public class main {
- public static void main (String[]arug){
- int Totalmember= ID(12,13);
- System.out.println(Totalmember);
- }
- static int ID(int person1, int person2){ // Return type methods/ void can not be use return type
- return person1+person2;
- }
- }
- // returns an int value 5 -------------------
- static int returnFive() {
- return 5;
- }
- // has a parameter -----------------------
- static void sayHelloTo(String name) {
- System.out.println("Hello " + name);
- }
- // simply prints"Hello World!" --------------------
- static void sayHello() {
- System.out.println("Hello World!");
- }
- 2. Creating class and object ---------------
- public class Animal{
- void berk(){
- System.out.println("Woff Woff");
- }}
- class Dog{
- public static void main( String[]arug ){
- Animal dog = new Animal();
- dog.berk();
- }
- }
- 3.Encapsulation -----------------------------
- import java.util.Scanner;
- public class Main
- {
- public static void main(String[] args) {
- Scanner read = new Scanner(System.in);
- int a = read.nextInt();
- Pupil pupil = new Pupil();
- pupil.setAge(a);
- }
- }
- class Pupil{
- private int age;
- //complete setter method
- public void setAge(int a){
- if(a>6){
- this.age=a;
- System.out.println("Welcome");
- }else{
- System.out.println("Sorry");
- }
- }
- public int getAge(){
- return age;
- }
- }
- 4.Inhertance -----------------------------
- // Recall the protected access modifier, which makes the members visible only to the subclasses.
- //You can access the superclass from the subclass using the super keyword.
- 5. Polymorphism -----------------------------
- class Animal {
- public void makeSound() {
- System.out.println("Grr...");
- }
- }
- class Cat extends Animal {
- public void makeSound() {
- System.out.println("Meow");
- }
- }
- class Dog extends Animal {
- public void makeSound() {
- System.out.println("Woof");
- }
- }
- class Program {
- public static void main(String args[ ]) {
- Animal a = new Dog();
- Animal b = new Cat();
- a.makeSound();
- b.makeSound();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement